| Class | Color::HSL |
| In: |
lib/color/hsl.rb
|
| Parent: | Object |
Compares the other colour to this one. The other colour will be converted to HSL before comparison, so the comparison between a HSL colour and a non-HSL colour will be approximate and based on the other colour‘s to_hsl conversion. If there is no to_hsl conversion, this will raise an exception. This will report that two HSL values are equivalent if all component values are within Color::COLOR_TOLERANCE of each other.
# File lib/color/hsl.rb, line 37
37: def ==(other)
38: other = other.to_hsl
39: other.kind_of?(Color::HSL) and
40: ((@h - other.h).abs <= Color::COLOR_TOLERANCE) and
41: ((@s - other.s).abs <= Color::COLOR_TOLERANCE) and
42: ((@l - other.l).abs <= Color::COLOR_TOLERANCE)
43: end
Returns the luminosity (l) of the colour.
# File lib/color/hsl.rb, line 137
137: def brightness
138: @l
139: end
Sets the hue of the colour in degrees. Colour is perceived as a wheel, so values should be set properly even with negative degree values.
# File lib/color/hsl.rb, line 155
155: def hue=(hh)
156: hh = hh / 360.0
157:
158: hh += 1.0 if hh < 0.0
159: hh -= 1.0 if hh > 1.0
160:
161: @h = Color.normalize(hh)
162: end
# File lib/color/hsl.rb, line 207
207: def inspect
208: "HSL [%.2f deg, %.2f%%, %.2f%%]" % [ hue, saturation, luminosity ]
209: end
Returns the luminosity of the colour in the range 0.0 .. 1.0.
# File lib/color/hsl.rb, line 190
190: def l
191: @l
192: end
Sets the luminosity of the colour in the ragne 0.0 .. 1.0.
# File lib/color/hsl.rb, line 199
199: def l=(ll)
200: @l = Color.normalize(ll)
201: end
Returns the percentage of luminosity of the colour.
# File lib/color/hsl.rb, line 185
185: def luminosity
186: @l * 100.0
187: end
Sets the percentage of luminosity of the colour.
# File lib/color/hsl.rb, line 194
194: def luminosity=(ll)
195: @l = Color.normalize(ll / 100.0)
196: end
Mix the mask colour (which will be converted to an HSL colour) with the current colour at the stated mix percentage as a decimal value.
| NOTE: | This differs from Color::RGB#mix_with. |
# File lib/color/hsl.rb, line 215
215: def mix_with(color, mix_percent = 0.5)
216: color = color.to_hsl
217: _h = ((color.h - self.h) * mix_percent) + self.h
218: _s = ((color.s - self.s) * mix_percent) + self.s
219: _l = ((color.l - self.l) * mix_percent) + self.l
220:
221: self.class.from_fraction(_h, _s, _l)
222: end
Returns the saturation of the colour in the range 0.0 .. 1.0.
# File lib/color/hsl.rb, line 172
172: def s
173: @s
174: end
Sets the saturation of the colour in the ragne 0.0 .. 1.0.
# File lib/color/hsl.rb, line 180
180: def s=(ss)
181: @s = Color.normalize(ss)
182: end
Returns the percentage of saturation of the colour.
# File lib/color/hsl.rb, line 168
168: def saturation
169: @s * 100.0
170: end
Sets the percentage of saturation of the colour.
# File lib/color/hsl.rb, line 176
176: def saturation=(ss)
177: @s = Color.normalize(ss / 100.0)
178: end
# File lib/color/hsl.rb, line 140
140: def to_greyscale
141: Color::GrayScale.from_fraction(@l)
142: end
Converting to HSL as adapted from Foley and Van-Dam from www.bobpowell.net/RGBHSB.htm.
NOTE:
# File lib/color/hsl.rb, line 93
93: def to_rgb(ignored = nil)
94: return Color::RGB.new if Color.near_zero_or_less?(@l)
95: return Color::RGB.new(0xff, 0xff, 0xff) if Color.near_one_or_more?(@l)
96: return Color::RGB.from_fraction(@l, @l, @l) if Color.near_zero?(@s)
97:
98: # Is the value less than 0.5?
99: if Color.near_zero_or_less?(@l - 0.5)
100: tmp2 = @l * (1.0 + @s.to_f)
101: else
102: tmp2 = @l + @s - (@l * @s.to_f)
103: end
104: tmp1 = 2.0 * @l - tmp2
105:
106: tmp3 = [ @h + (1.0 / 3.0), @h, @h - (1.0 / 3.0) ]
107:
108: rgb = tmp3.map { |hue|
109: hue += 1.0 if Color.near_zero_or_less?(hue)
110: hue -= 1.0 if Color.near_one_or_more?(hue)
111:
112: if Color.near_zero_or_less?((6.0 * hue) - 1.0)
113: tmp1 + ((tmp2 - tmp1) * hue * 6.0)
114: elsif Color.near_zero_or_less?((2.0 * hue) - 1.0)
115: tmp2
116: elsif Color.near_zero_or_less?((3.0 * hue) - 2.0)
117: tmp1 + (tmp2 - tmp1) * ((2 / 3.0) - hue) * 6.0
118: else
119: tmp1
120: end
121: }
122:
123: Color::RGB.from_fraction(*rgb)
124: end