Method: ColorLib::HSL#to_rgb

Defined in:
lib/color_lib/hsl.rb

#to_rgb(ignored = nil) ⇒ Object

Converting to HSL as adapted from Foley and Van-Dam from www.bobpowell.net/RGBHSB.htm.

NOTE:

  • If the colour’s luminosity is near zero, the colour is always black.

  • If the colour’s luminosity is near one, the colour is always white.

  • If the colour’s saturation is near zero, the colour is always a shade of grey and is based only on the luminosity of the colour.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/color_lib/hsl.rb', line 79

def to_rgb(ignored = nil)
  return ColorLib::RGB.new if ColorLib.near_zero_or_less?(@l)
  return ColorLib::RGB.new(0xff, 0xff, 0xff) if ColorLib.near_one_or_more?(@l)
  return ColorLib::RGB.from_fraction(@l, @l, @l) if ColorLib.near_zero?(@s)

  # Is the value less than 0.5?
  if ColorLib.near_zero_or_less?(@l - 0.5)
    tmp2 = @l * (1.0 + @s.to_f)
  else
    tmp2 = @l + @s - (@l * @s.to_f)
  end
  tmp1 = 2.0 * @l - tmp2

  tmp3 = [@h + (1.0 / 3.0), @h, @h - (1.0 / 3.0)]

  rgb = tmp3.map { |hue|
    hue += 1.0 if ColorLib.near_zero_or_less?(hue)
    hue -= 1.0 if ColorLib.near_one_or_more?(hue)

    if ColorLib.near_zero_or_less?((6.0 * hue) - 1.0)
      tmp1 + ((tmp2 - tmp1) * hue * 6.0)
    elsif ColorLib.near_zero_or_less?((2.0 * hue) - 1.0)
      tmp2
    elsif ColorLib.near_zero_or_less?((3.0 * hue) - 2.0)
      tmp1 + (tmp2 - tmp1) * ((2 / 3.0) - hue) * 6.0
    else
      tmp1
    end
  }

  ColorLib::RGB.from_fraction(*rgb)
end