Method: ColorFun::HSL.from_rbg

Defined in:
lib/color_fun/hsl.rb

.from_rbg(r, g, b) ⇒ Object

Create a new HSL object based on the RGB values



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/color_fun/hsl.rb', line 40

def self.from_rbg(r,g,b)
  colors    = [r / 255.0, g / 255.0, b / 255.0]
  max       = colors.max
  min       = colors.min
  d         = max - min
  v         = max * 100

  s = max != 0.0 ? d / max *100 : 0.0

  if s == 0.0
    h = 0.0
  else
    case max
    when colors[0]
      h = (colors[1] - colors[2]) / d
    when colors[1]
      h = 2 + (colors[2] - colors[0]) / d
    when colors[2]
      h = 4 + (colors[0] - colors[1]) / d
    end
    h *= 60.0
    h += 360.0 if (h < 0)
  end
  self.new(h, s, v)
end