Class: Falu::Colors::RGB
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #absolute ⇒ Object
- #blue ⇒ Object
- #chroma ⇒ Object
- #colors ⇒ Object
- #green ⇒ Object
- #hue ⇒ Object
-
#initialize(*color) ⇒ RGB
constructor
A new instance of RGB.
- #inverse ⇒ Object
- #lightness ⇒ Object
- #red ⇒ Object
- #saturation ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_hex ⇒ Object
- #to_hsl ⇒ Object
- #to_rgb ⇒ Object
- #to_s ⇒ Object
Methods inherited from Base
Constructor Details
Instance Method Details
#absolute ⇒ Object
28 29 30 |
# File 'lib/falu/colors/rgb.rb', line 28 def absolute colors.sum end |
#blue ⇒ Object
24 25 26 |
# File 'lib/falu/colors/rgb.rb', line 24 def blue colors[2] end |
#chroma ⇒ Object
36 37 38 |
# File 'lib/falu/colors/rgb.rb', line 36 def chroma @chroma ||= (inverse.max - inverse.min) end |
#colors ⇒ Object
12 13 14 |
# File 'lib/falu/colors/rgb.rb', line 12 def colors @colors ||= color.to_s.split(',').map { |c| c.strip.to_i } end |
#green ⇒ Object
20 21 22 |
# File 'lib/falu/colors/rgb.rb', line 20 def green colors[1] end |
#hue ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/falu/colors/rgb.rb', line 58 def hue @hue ||= begin if inverse.max == inverse.min 0 else rd,gr,bl = *inverse if rd == inverse.max hue = ((gr - bl) / chroma % 6) * 60 elsif gr == inverse.max hue = ((bl - rd) / chroma + 2) * 60 elsif bl == inverse.max hue = ((rd - gr) / chroma + 4) * 60 end end end end |
#inverse ⇒ Object
32 33 34 |
# File 'lib/falu/colors/rgb.rb', line 32 def inverse @inverse ||= colors.map { |c| c / 255.0 } end |
#lightness ⇒ Object
40 41 42 |
# File 'lib/falu/colors/rgb.rb', line 40 def lightness @lightness ||= ((inverse.max + inverse.min) / 2) end |
#red ⇒ Object
16 17 18 |
# File 'lib/falu/colors/rgb.rb', line 16 def red colors[0] end |
#saturation ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/falu/colors/rgb.rb', line 44 def saturation @saturation ||= begin if inverse.max == inverse.min 0 else if lightness < 0.5 (chroma / (inverse.max + inverse.min)) else (chroma / (2 - inverse.max - inverse.min)) end end end end |
#to_a ⇒ Object
80 81 82 |
# File 'lib/falu/colors/rgb.rb', line 80 def to_a colors end |
#to_h ⇒ Object
84 85 86 |
# File 'lib/falu/colors/rgb.rb', line 84 def to_h {red: red, green: green, blue: blue} end |
#to_hex ⇒ Object
92 93 94 |
# File 'lib/falu/colors/rgb.rb', line 92 def to_hex Falu::Colors::HEX.new("##{colors.map { |c| c.to_s(16).rjust(2, '0') }.join}") end |
#to_hsl ⇒ Object
96 97 98 |
# File 'lib/falu/colors/rgb.rb', line 96 def to_hsl Falu::Colors::HSL.new([hue, (saturation * 100), (lightness * 100)]) end |
#to_rgb ⇒ Object
88 89 90 |
# File 'lib/falu/colors/rgb.rb', line 88 def to_rgb self end |
#to_s ⇒ Object
76 77 78 |
# File 'lib/falu/colors/rgb.rb', line 76 def to_s colors.map { |c| c.round.to_s.rjust(3, '0') }.join(',') end |