class Rubyvis::Color::Hsl

Represents a color in HSL space.

Attributes

a[RW]

The opacity, a float in [0, 1].

h[RW]

The hue, an integer in [0, 360].

l[RW]

The lightness, a float in [0, 1].

s[RW]

The saturation, a float in [0, 1].

Public Class Methods

new(h,s,l,a) click to toggle source
# File lib/rubyvis/color/color.rb, line 377
def initialize(h,s,l,a)
  c="hsl(#{h},#{s * 100}%,#{l * 100}%)"
  super(c,a)
  @h=h
  @s=s
  @l=l
  @a=a
end

Public Instance Methods

==(v) click to toggle source
# File lib/rubyvis/color/color.rb, line 385
def ==(v)
  self.class==v.class and @h==v.h and @s==v.s and @l==v.l and @a==v.a
end
rgb() click to toggle source

Returns the RGB color equivalent to this HSL color.

# File lib/rubyvis/color/color.rb, line 390
def rgb
  h = self.h
  s = self.s
  l = self.l
  # Some simple corrections for h, s and l. */
  h = h % 360
  h += 360 if (h < 0)
  s = [0, [s, 1].min].max
  l = [0, [l, 1].min].max
  
  # From FvD 13.37, CSS Color Module Level 3 
  m2 = (l <= 0.5) ? (l * (1 + s)) : (l + s - l * s)
  m1 = 2 * l - m2
  v=lambda {|h1|
    if (h1 > 360)
      h1 -= 360
    elsif (h1 < 0)
       h1 += 360
    end
    
    
    return m1 + (m2 - m1) * h1 / 60.0 if (h1 < 60.0)
    return m2 if (h1 < 180.0) 
    return m1 + (m2 - m1) * (240.0 - h1) / 60.0 if (h1 < 240.0)
    return m1
  }
  
  vv=lambda {|h1| (v.call(h1) * 255).round}
  Rubyvis.rgb(vv.call(h + 120), vv.call(h), vv.call(h - 120), a)
 
end