Class | Color::HSL |
In: |
lib/color/hsl.rb
|
Parent: | Object |
An HSL colour object. Internally, the hue (h), saturation (s), and luminosity (l) values are dealt with as fractional values in the range 0..1.
h | [RW] | |
l | [RW] | |
s | [RW] |
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 1e-4 (0.0001) of each other.
# File lib/color/hsl.rb, line 34 34: def ==(other) 35: other = other.to_hsl 36: other.kind_of?(Color::HSL) and 37: ((@h - other.h).abs <= 1e-4) and 38: ((@s - other.s).abs <= 1e-4) and 39: ((@l - other.l).abs <= 1e-4) 40: end
Returns the luminosity (l) of the colour.
# File lib/color/hsl.rb, line 105 105: def brightness 106: @l 107: end
# File lib/color/hsl.rb, line 108 108: def to_greyscale 109: Color::GrayScale.from_fraction(@l) 110: end
Converting to HSL as adapted from Foley and Van-Dam from www.bobpowell.net/RGBHSB.htm.
# File lib/color/hsl.rb, line 57 57: def to_rgb(ignored = nil) 58: # If luminosity is zero, the colour is always black. 59: return Color::RGB.new if @l == 0 60: # If luminosity is one, the colour is always white. 61: return Color::RGB.new(0xff, 0xff, 0xff) if @l == 1 62: # If saturation is zero, the colour is always a greyscale colour. 63: return Color::RGB.new(@l, @l, @l) if @s <= 1e-5 64: 65: if (@l - 0.5) < 1e-5 66: tmp2 = @l * (1.0 + @s.to_f) 67: else 68: tmp2 = @l + @s - (@l * @s.to_f) 69: end 70: tmp1 = 2.0 * @l - tmp2 71: 72: t3 = [ @h + 1.0 / 3.0, @h, @h - 1.0 / 3.0 ] 73: t3 = t3.map { |tmp3| 74: tmp3 += 1.0 if tmp3 < 1e-5 75: tmp3 -= 1.0 if (tmp3 - 1.0) > 1e-5 76: tmp3 77: } 78: 79: rgb = t3.map do |tmp3| 80: if ((6.0 * tmp3) - 1.0) < 1e-5 81: tmp1 + ((tmp2 - tmp1) * tmp3 * 6.0) 82: elsif ((2.0 * tmp3) - 1.0) < 1e-5 83: tmp2 84: elsif ((3.0 * tmp3) - 2.0) < 1e-5 85: tmp1 + (tmp2 - tmp1) * ((2 / 3.0) - tmp3) * 6.0 86: else 87: tmp1 88: end 89: end 90: 91: Color::RGB.from_fraction(*rgb) 92: end