class Colors::HSL

Attributes

h[R]
hue[R]
l[R]
lightness[R]
s[R]
saturation[R]

Public Class Methods

new(h, s, l) click to toggle source
# File lib/colors/hsl.rb, line 5
def initialize(h, s, l)
  @h, @s, @l = canonicalize(h, s, l)
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/colors/hsl.rb, line 45
def ==(other)
  case other
  when HSLA
    other == self
  when HSL
    h == other.h && s == other.s && l == other.l
  else
    super
  end
end
components() click to toggle source
# File lib/colors/hsl.rb, line 11
def components
  [h, s, l]
end
Also aliased as: hsl_components
desaturate(factor) click to toggle source
# File lib/colors/hsl.rb, line 56
def desaturate(factor)
  HSL.new(h, s*factor, l)
end
h=(h) click to toggle source
# File lib/colors/hsl.rb, line 17
def h=(h)
  @h = Rational(h) % 360
end
Also aliased as: hue=
hsl_components()
Alias for: components
hue=(h)
Alias for: h=
l=(l) click to toggle source
# File lib/colors/hsl.rb, line 29
def l=(l)
  @l = if l.instance_of?(Integer)
         check_range(l, 0..255, :l) / 255r
       else
         Rational(check_range(l, 0..1, :l))
       end
end
Also aliased as: lightness=
lightness=(l)
Alias for: l=
rgb_components() click to toggle source
# File lib/colors/hsl.rb, line 78
def rgb_components
  t2 = if l <= 0.5r
         l * (s + 1r)
       else
         l + s - l * s
       end
  t1 = l * 2r - t2
  hh = h/60r
  r = hue_to_rgb(t1, t2, hh + 2)
  g = hue_to_rgb(t1, t2, hh)
  b = hue_to_rgb(t1, t2, hh - 2)
  [r, g, b]
end
s=(s) click to toggle source
# File lib/colors/hsl.rb, line 21
def s=(s)
  @s = if s.instance_of?(Integer)
         check_range(s, 0..255, :s) / 255r
       else
         Rational(check_range(s, 0..1, :s))
       end
end
Also aliased as: saturation=
saturation=(s)
Alias for: s=
to_hsl() click to toggle source
# File lib/colors/hsl.rb, line 60
def to_hsl
  self
end
to_hsla(alpha: 1.0) click to toggle source
# File lib/colors/hsl.rb, line 64
def to_hsla(alpha: 1.0)
  alpha = canonicalize_component(alpha, :alpha)
  HSLA.new(h, s, l, alpha)
end
to_rgb() click to toggle source
# File lib/colors/hsl.rb, line 69
def to_rgb
  RGB.new(*rgb_components)
end
to_rgba(alpha: 1.0) click to toggle source
# File lib/colors/hsl.rb, line 73
def to_rgba(alpha: 1.0)
  alpha = canonicalize_component(alpha, :alpha)
  RGBA.new(*rgb_components, alpha)
end

Private Instance Methods

canonicalize(h, s, l) click to toggle source
# File lib/colors/hsl.rb, line 106
        def canonicalize(h, s, l)
  if [s, l].map(&:class) == [Integer, Integer]
    canonicalize_from_integer(h, s, l)
  else
    [
      Rational(h) % 360,
      canonicalize_component_to_rational(s, :s),
      canonicalize_component_to_rational(l, :l)
    ]
  end
end
canonicalize_from_integer(h, s, l) click to toggle source
# File lib/colors/hsl.rb, line 118
        def canonicalize_from_integer(h, s, l)
  check_type(s, Integer, :s)
  check_type(l, Integer, :l)
  [
    Rational(h) % 360,
    canonicalize_component_from_integer(s, :s),
    canonicalize_component_from_integer(l, :l)
  ]
end
hue_to_rgb(t1, t2, h) click to toggle source
# File lib/colors/hsl.rb, line 92
        def hue_to_rgb(t1, t2, h)
  h += 6r if h < 0
  h -= 6r if h >= 6
  if h < 1
    (t2 - t1) * h + t1
  elsif h < 3
    t2
  elsif h < 4
    (t2 - t1) * (4r - h) + t1
  else
    t1
  end
end