Class Color::HSL
In: lib/color/hsl.rb
Parent: Object
Enumerable Gimp CMYK\n[lib/color.rb\nlib/color/cmyk.rb] GrayScale\n[lib/color.rb\nlib/color/grayscale.rb] RGB\n[lib/color.rb\nlib/color/rgb-colors.rb\nlib/color/rgb.rb] YIQ\n[lib/color.rb\nlib/color/yiq.rb] HSL MonoContrast lib/color/cmyk.rb lib/color/grayscale.rb lib/color/rgb.rb lib/color/yiq.rb lib/color/hsl.rb CSS lib/color/palette/gimp.rb lib/color/palette/monocontrast.rb Palette Color dot/m_9_0.png

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.

Methods

==   brightness   from_fraction   html   new   to_cmyk   to_grayscale   to_greyscale   to_rgb   to_yiq  

Attributes

h  [RW] 
l  [RW] 
s  [RW] 

Public Class methods

Creates an HSL colour object from fractional values 0..1.

[Source]

    # File lib/color/hsl.rb, line 18
18:     def from_fraction(h = 0.0, s = 0.0, l = 0.0)
19:       colour = Color::HSL.new
20:       colour.h = h
21:       colour.s = s
22:       colour.l = l
23:       colour
24:     end

Creates an HSL colour object from the standard values of degrees and percentages (e.g., 145º, 30%, 50%).

[Source]

    # File lib/color/hsl.rb, line 44
44:   def initialize(h = 0, s = 0, l = 0)
45:     @h = h / 360.0
46:     @s = s / 100.0
47:     @l = l / 100.0
48:   end

Public Instance methods

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.

[Source]

    # 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.

[Source]

     # File lib/color/hsl.rb, line 105
105:   def brightness
106:     @l
107:   end

Present the colour as an HTML/CSS colour string.

[Source]

    # File lib/color/hsl.rb, line 51
51:   def html
52:     to_rgb.html
53:   end

Converts to RGB then CMYK.

[Source]

     # File lib/color/hsl.rb, line 100
100:   def to_cmyk
101:     to_rgb.to_cmyk
102:   end
to_grayscale()

Alias for to_greyscale

[Source]

     # 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.

[Source]

    # 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

Converts to RGB then YIQ.

[Source]

    # File lib/color/hsl.rb, line 95
95:   def to_yiq
96:     to_rgb.to_yiq
97:   end

[Validate]