Class: RainbowUnicorn::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/rainbow_unicorn/color.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, g, b) ⇒ Color

Returns a new instance of Color.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rainbow_unicorn/color.rb', line 3

def initialize(r, g, b)
	unless Integer === r && Integer === g && Integer === b
		raise TypeError, "rgb values must be integers"
	end

	unless (0..255) === r && (0..255) === g && (0..255) === b
		raise TypeError, "rgb values must be between 0 and 255"
	end

	@rgb = [r, g, b].freeze
	freeze
end

Instance Attribute Details

#rgbObject (readonly) Also known as: to_a, deconstruct

Returns the value of attribute rgb.



79
80
81
# File 'lib/rainbow_unicorn/color.rb', line 79

def rgb
  @rgb
end

Class Method Details

.from_hex(hex) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/rainbow_unicorn/color.rb', line 46

def self.from_hex(hex)
	unless String === hex && hex.length == 6
		raise ArgumentError, "Invalid hex color"
	end

	r, g, b = hex.scan(/../).map { |color| color.to_i(16) }
	new(r, g, b)
end

.from_hsl(h, s, l) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rainbow_unicorn/color.rb', line 16

def self.from_hsl(h, s, l)
  unless (0..360) === h && (0..1) === s && (0..1) === l
		raise TypeError, "hsl values must be between 0 and 360 for h and 0 and 1 for s and l"
  end

  c = (1 - (2 * l - 1).abs) * s
  x = c * (1 - ((h / 60.0) % 2 - 1).abs)
  m = l - c / 2.0

  r1, g1, b1 = if h < 60
      [c, x, 0]
    elsif h < 120
      [x, c, 0]
    elsif h < 180
      [0, c, x]
    elsif h < 240
      [0, x, c]
    elsif h < 300
      [x, 0, c]
    else
      [c, 0, x]
    end

  r = ((r1 + m) * 255).round
  g = ((g1 + m) * 255).round
  b = ((b1 + m) * 255).round

  new(r, g, b)
end

Instance Method Details

#darken(amount) ⇒ Object



120
121
122
123
# File 'lib/rainbow_unicorn/color.rb', line 120

def darken(amount)
	h, s, l = hsl
	self.class.from_hsl(h, s, [(l - amount), 0].max)
end

#deconstruct_keys(keys) ⇒ Object



87
88
89
# File 'lib/rainbow_unicorn/color.rb', line 87

def deconstruct_keys(keys)
	{ r: @rgb[0], g: @rgb[1], b: @rgb[2] }
end

#desaturate(amount) ⇒ Object



130
131
132
133
# File 'lib/rainbow_unicorn/color.rb', line 130

def desaturate(amount)
	h, s, l = hsl
	self.class.from_hsl(h, [(s - amount), 0].max, l)
end

#hexObject



95
96
97
# File 'lib/rainbow_unicorn/color.rb', line 95

def hex
	"##{rgb.map { |c| c.to_s(16).rjust(2, '0') }.join}"
end

#hslObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rainbow_unicorn/color.rb', line 55

def hsl
	rgb = @rgb.map { |c| c / 255.0 }
	max, min = rgb.max, rgb.min

	r, g, b = rgb
	h, s, l = 0, 0, (max + min) / 2.0

	if max != min
		d = max - min
		s = l > 0.5 ? d / (2.0 - max - min) : d / (max + min)
		case max
		when r
			h = (g - b) / d + (g < b ? 6.0 : 0)
		when g
			h = (b - r) / d + 2.0
		when b
			h = (r - g) / d + 4.0
		end
		h /= 6.0
	end

	[h * 360, s, l]
end

#inspectObject



91
92
93
# File 'lib/rainbow_unicorn/color.rb', line 91

def inspect
	"#{self.class.name}(#{@rgb.join(', ')})"
end

#lighten(amount) ⇒ Object



115
116
117
118
# File 'lib/rainbow_unicorn/color.rb', line 115

def lighten(amount)
	h, s, l = hsl
	self.class.from_hsl(h, s, [(l + amount), 1].min)
end

#saturate(amount) ⇒ Object



125
126
127
128
# File 'lib/rainbow_unicorn/color.rb', line 125

def saturate(amount)
	h, s, l = hsl
	self.class.from_hsl(h, [(s + amount), 1].min, l)
end

#to_hObject



83
84
85
# File 'lib/rainbow_unicorn/color.rb', line 83

def to_h
	{ r: @rgb[0], g: @rgb[1], b: @rgb[2] }
end

#to_sObject



99
100
101
# File 'lib/rainbow_unicorn/color.rb', line 99

def to_s
	"rgb(#{rgb.join(', ')})"
end