Class: AutoColors::Color
- Inherits:
-
Object
- Object
- AutoColors::Color
- Defined in:
- lib/autocolors/color.rb,
lib/autocolors/color.rb
Overview
Instance Attribute Summary collapse
-
#rgb_approx ⇒ Object
readonly
Returns the value of attribute rgb_approx.
Instance Method Summary collapse
- #-(c) ⇒ Object
- #b ⇒ Object
- #b=(v) ⇒ Object
- #ca ⇒ Object
- #ca=(v) ⇒ Object
- #cb ⇒ Object
- #cb=(v) ⇒ Object
- #chroma ⇒ Object
- #chroma=(v) ⇒ Object
- #cl ⇒ Object
- #cl=(v) ⇒ Object
- #g ⇒ Object
- #g=(v) ⇒ Object
-
#hue ⇒ Object
Hue & chroma.
- #hue=(v) ⇒ Object
- #hue_index=(v) ⇒ Object
-
#initialize(lab, rgb = nil) ⇒ Color
constructor
A new instance of Color.
-
#lab ⇒ Object
CIELAB colorspace.
- #lab=(v) ⇒ Object
- #lab_propagate ⇒ Object protected
- #labf(t) ⇒ Object protected
- #lb ⇒ Object
- #lg ⇒ Object
-
#lr ⇒ Object
Intermediate spaces.
- #lrgbc_to_rgbc(c) ⇒ Object protected
- #r ⇒ Object
- #r=(v) ⇒ Object
- #rad2turns(r) ⇒ Object protected
-
#rgb ⇒ Object
RGB colorspace.
- #rgb=(v) ⇒ Object
- #rgb_propagate ⇒ Object protected
- #rgbc_to_lrgbc(c) ⇒ Object protected
- #s_cterm ⇒ Object
- #s_high ⇒ Object
- #s_lcterm ⇒ Object
- #s_term ⇒ Object
- #to_16 ⇒ Object
- #to_256 ⇒ Object
- #to_s ⇒ Object
- #turns2rad(t) ⇒ Object protected
- #x ⇒ Object
- #y ⇒ Object
- #z ⇒ Object
Constructor Details
#initialize(lab, rgb = nil) ⇒ Color
Returns a new instance of Color.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/autocolors/color.rb', line 12 def initialize(lab,rgb=nil) if rgb.nil? @cl,@ca,@cb = lab.map{|v| v.to_f} @lab_dirty = true @rgb_dirty = false else @r,@g,@b = rgb.map{|v| v.to_f} @lab_dirty = false @rgb_dirty = true end end |
Instance Attribute Details
#rgb_approx ⇒ Object (readonly)
Returns the value of attribute rgb_approx.
10 11 12 |
# File 'lib/autocolors/color.rb', line 10 def rgb_approx @rgb_approx end |
Instance Method Details
#-(c) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/autocolors/color.rb', line 94 def -(c) if c.is_a?(Color) # Euclidean distance in 3 dimensions, but emphasize brightness a bit # more than the color (which is more how the human eye works) Math.sqrt((cl*2.5 - c.cl*2.5)**2 + (ca - c.ca)**2 + (cb - c.cb)**2) end end |
#b ⇒ Object
46 |
# File 'lib/autocolors/color.rb', line 46 def b; lab_propagate if @lab_dirty; @b end |
#b=(v) ⇒ Object
50 |
# File 'lib/autocolors/color.rb', line 50 def b=(v) @rgb_dirty=true; @b=v.to_f end |
#ca ⇒ Object
35 |
# File 'lib/autocolors/color.rb', line 35 def ca; rgb_propagate if @rgb_dirty; @ca end |
#ca=(v) ⇒ Object
39 |
# File 'lib/autocolors/color.rb', line 39 def ca=(v) @lab_dirty=true; @ca=v.to_f end |
#cb ⇒ Object
36 |
# File 'lib/autocolors/color.rb', line 36 def cb; rgb_propagate if @rgb_dirty; @cb end |
#cb=(v) ⇒ Object
40 |
# File 'lib/autocolors/color.rb', line 40 def cb=(v) @lab_dirty=true; @cb=v.to_f end |
#chroma ⇒ Object
54 |
# File 'lib/autocolors/color.rb', line 54 def chroma; Math::sqrt((ca * ca) + (cb * cb)) end |
#chroma=(v) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/autocolors/color.rb', line 83 def chroma=(v) q = Math.atan2(ca, cb) v = v.to_f self.ca = Math::sin(q) * v self.cb = Math::cos(q) * v end |
#cl ⇒ Object
34 |
# File 'lib/autocolors/color.rb', line 34 def cl; rgb_propagate if @rgb_dirty; @cl end |
#cl=(v) ⇒ Object
38 |
# File 'lib/autocolors/color.rb', line 38 def cl=(v) @lab_dirty=true; @cl=v.to_f end |
#g ⇒ Object
45 |
# File 'lib/autocolors/color.rb', line 45 def g; lab_propagate if @lab_dirty; @g end |
#g=(v) ⇒ Object
49 |
# File 'lib/autocolors/color.rb', line 49 def g=(v) @rgb_dirty=true; @g=v.to_f end |
#hue ⇒ Object
Hue & chroma
53 |
# File 'lib/autocolors/color.rb', line 53 def hue; rad2turns(Math.atan2(ca, cb)) end |
#hue=(v) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/autocolors/color.rb', line 76 def hue=(v) r = turns2rad(v.to_f) c = chroma self.ca = Math::sin(r) * c self.cb = Math::cos(r) * c end |
#hue_index=(v) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/autocolors/color.rb', line 56 def hue_index=(v) self.hue = [ 0.0, # 0 | Blue 0.15, # 1 | Cyan 0.225, # 2 | Teal 0.33, # 3 | Green 0.4, # 4 | Forest green 0.45, # 5 | Puke green 0.5, # 6 | Yellow 0.565, # 7 | Orange 0.625, # 8 | Orange-red 0.6575, # 9 | Red 0.75, # 10 | Magenta 0.825, # 11 | Fuchsia 0.8875, # 12 | Pink 0.925, # 13 | Violet 0.95 # 14 | Indigo ][v.to_i] end |
#lab ⇒ Object
CIELAB colorspace
33 |
# File 'lib/autocolors/color.rb', line 33 def lab; rgb_propagate if @rgb_dirty; [@cl,@ca,@cb] end |
#lab=(v) ⇒ Object
37 |
# File 'lib/autocolors/color.rb', line 37 def lab=(v) @lab_dirty=true; @cl,@ca,@cb=v.map{|a|a.to_f} end |
#lab_propagate ⇒ Object (protected)
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/autocolors/color.rb', line 113 def lab_propagate p = (@cl + 16.0) / 116.0 @x = (p + @ca / 500.0) ** 3.0 @y = p ** 3.0 @z = (p - @cb / 200.0) ** 3.0 @lr,@lg,@lb = (XYZ2LRGB*Matrix[[@x],[@y],[@z]]).to_a.flatten @rgb_approx = false @r,@g,@b = [@lr,@lg,@lb].map{|c|lrgbc_to_rgbc(c)} @lab_dirty = false end |
#labf(t) ⇒ Object (protected)
124 125 126 |
# File 'lib/autocolors/color.rb', line 124 def labf(t) t > 0.008856 ? t**(1.0/3.0) : 7.787 * t + (16.0/116.0) end |
#lb ⇒ Object
27 |
# File 'lib/autocolors/color.rb', line 27 def lb; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @lb end |
#lg ⇒ Object
26 |
# File 'lib/autocolors/color.rb', line 26 def lg; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @lg end |
#lr ⇒ Object
Intermediate spaces
25 |
# File 'lib/autocolors/color.rb', line 25 def lr; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @lr end |
#lrgbc_to_rgbc(c) ⇒ Object (protected)
135 136 137 138 139 140 141 |
# File 'lib/autocolors/color.rb', line 135 def lrgbc_to_rgbc(c) if c <= 0.0031308 then v = (12.92 * c * 255.0).round else v = ((1.055 * (c ** (1.0/2.4)) - 0.055) * 255.0).round end if v < 0 then @rgb_approx = true; 0 elsif v > 255 then @rgb_approx = true; 255 else v end end |
#r ⇒ Object
44 |
# File 'lib/autocolors/color.rb', line 44 def r; lab_propagate if @lab_dirty; @r end |
#r=(v) ⇒ Object
48 |
# File 'lib/autocolors/color.rb', line 48 def r=(v) @rgb_dirty=true; @r=v.to_f end |
#rad2turns(r) ⇒ Object (protected)
143 |
# File 'lib/autocolors/color.rb', line 143 def rad2turns(r) (r + Math::PI) / (2.0 * Math::PI) end |
#rgb ⇒ Object
RGB colorspace
43 |
# File 'lib/autocolors/color.rb', line 43 def rgb; lab_propagate if @lab_dirty; [@r,@g,@b] end |
#rgb=(v) ⇒ Object
47 |
# File 'lib/autocolors/color.rb', line 47 def rgb=(v) @rgb_dirty=true; @r,@g,@b=v.map{|a|a.to_f} end |
#rgb_propagate ⇒ Object (protected)
104 105 106 107 108 109 110 111 |
# File 'lib/autocolors/color.rb', line 104 def rgb_propagate @lr,@lg,@lb = [@r,@g,@b].map{|c|rgbc_to_lrgbc(c)} @x,@y,@z = (LRGB2XYZ*Matrix[[@lr],[@lg],[@lb]]).to_a.flatten @cl = (@y > 0.008856) ? 116.0*(@y ** (1.0/3.0)) - 16.0 : 903.3 * @y @ca = 500.0 * (labf(@x) - labf(@y)) @cb = 200.0 * (labf(@y) - labf(@z)) @rgb_dirty = false end |
#rgbc_to_lrgbc(c) ⇒ Object (protected)
129 130 131 132 133 |
# File 'lib/autocolors/color.rb', line 129 def rgbc_to_lrgbc(c) cf = c.to_f / 255.0 if cf <= 0.04045 then cf / 12.92 else ((cf + 0.055) / 1.055) ** 2.4 end end |
#s_cterm ⇒ Object
192 193 194 |
# File 'lib/autocolors/color.rb', line 192 def s_cterm 'NONE' end |
#s_high ⇒ Object
196 197 198 |
# File 'lib/autocolors/color.rb', line 196 def s_high 'NONE' end |
#s_lcterm ⇒ Object
188 189 190 |
# File 'lib/autocolors/color.rb', line 188 def s_lcterm 'NONE' end |
#s_term ⇒ Object
184 185 186 |
# File 'lib/autocolors/color.rb', line 184 def s_term 'NONE' end |
#to_16 ⇒ Object
180 181 182 |
# File 'lib/autocolors/color.rb', line 180 def to_16 CUBE256[0..15].each_with_index.map{|c,i| [i,c-self]}.sort_by{|i,diff| diff}.first[0] end |
#to_256 ⇒ Object
175 176 177 178 |
# File 'lib/autocolors/color.rb', line 175 def to_256 CUBE256.each_with_index.map{|c,i| [i,c-self]}.sort_by{|i,diff| diff}.first[0] #CUBE256.sort_by{|c| c - self}.first end |
#to_s ⇒ Object
90 91 92 |
# File 'lib/autocolors/color.rb', line 90 def to_s '#' + rgb.map{|v|v.to_s(16).rjust(2,'0')}.join('') end |
#turns2rad(t) ⇒ Object (protected)
144 |
# File 'lib/autocolors/color.rb', line 144 def turns2rad(t) (t * 2.0 * Math::PI) - Math::PI end |
#x ⇒ Object
28 |
# File 'lib/autocolors/color.rb', line 28 def x; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @x end |
#y ⇒ Object
29 |
# File 'lib/autocolors/color.rb', line 29 def y; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @y end |
#z ⇒ Object
30 |
# File 'lib/autocolors/color.rb', line 30 def z; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @z end |