Last active
May 25, 2022 19:41
-
-
Save knugie/6ebb5d97fa68ea0770f7 to your computer and use it in GitHub Desktop.
Convert RGB to HSL color model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.w3.org/TR/2011/REC-css3-color-20110607/#hsl-color
http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
http://colorizer.org/