Class: Colors::HSL

Inherits:
AbstractColor show all
Includes:
Helper
Defined in:
lib/colors/hsl.rb

Direct Known Subclasses

HSLA, HUSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractColor

#inspect

Constructor Details

#initialize(h, s, l) ⇒ HSL

Returns a new instance of HSL.



5
6
7
# File 'lib/colors/hsl.rb', line 5

def initialize(h, s, l)
  @h, @s, @l = canonicalize(h, s, l)
end

Instance Attribute Details

#hObject Also known as: hue

Returns the value of attribute h.



9
10
11
# File 'lib/colors/hsl.rb', line 9

def h
  @h
end

#lObject Also known as: lightness

Returns the value of attribute l.



9
10
11
# File 'lib/colors/hsl.rb', line 9

def l
  @l
end

#sObject Also known as: saturation

Returns the value of attribute s.



9
10
11
# File 'lib/colors/hsl.rb', line 9

def s
  @s
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# 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

#componentsObject Also known as: hsl_components



11
12
13
# File 'lib/colors/hsl.rb', line 11

def components
  [h, s, l]
end

#desaturate(factor) ⇒ Object



56
57
58
# File 'lib/colors/hsl.rb', line 56

def desaturate(factor)
  HSL.new(h, s*factor, l)
end

#rgb_componentsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# 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

#to_hslObject



60
61
62
# File 'lib/colors/hsl.rb', line 60

def to_hsl
  self
end

#to_hsla(alpha: 1.0) ⇒ Object



64
65
66
67
# 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_rgbObject



69
70
71
# File 'lib/colors/hsl.rb', line 69

def to_rgb
  RGB.new(*rgb_components)
end

#to_rgba(alpha: 1.0) ⇒ Object



73
74
75
76
# File 'lib/colors/hsl.rb', line 73

def to_rgba(alpha: 1.0)
  alpha = canonicalize_component(alpha, :alpha)
  RGBA.new(*rgb_components, alpha)
end