Skip to content

Instantly share code, notes, and snippets.

@knugie
Last active May 25, 2022 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knugie/6ebb5d97fa68ea0770f7 to your computer and use it in GitHub Desktop.
Save knugie/6ebb5d97fa68ea0770f7 to your computer and use it in GitHub Desktop.
Convert RGB to HSL color model
def rgb2hsl(r,g,b)
r´ = r.to_f / 255
g´ = g.to_f / 255
b´ = b.to_f / 255
c_max = [r´, g´, b´].max
c_min = [r´, g´, b´].min
Δ = c_max - c_min
h = Δ.zero? ? 0 : (60 * case c_max
when r´ then ((g´ - b´) / Δ) % 6
when g´ then ((b´ - r´) / Δ) + 2
when b´ then ((r´ - g´) / Δ) + 4
end)
l = (c_max + c_min) / 2
s = Δ.zero? ? 0 : (Δ / (1 - (2 * l - 1).abs))
[h.round(0), s.round(2), l.round(2)]
end