Created
January 13, 2014 22:49
-
-
Save ilya-pi/8409654 to your computer and use it in GitHub Desktop.
Lua function to convert RGB color representation to HSL
This file contains 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
local function rgb2hsl(r, g, b) | |
local max, min = math.max(r, g, b), math.min(r, g, b) | |
local h, s, l = (max + min) / 2, (max + min) / 2, (max + min) / 2 | |
if max == min then | |
h, s = 0, 0 -- achromatic | |
else | |
local d = max - min | |
local s = nil | |
if l > 0.5 then | |
s = d / (2 - max - min) | |
else | |
s = d / (max + min) | |
end | |
if max == r then | |
local wtf = nil | |
if g < b then | |
wtf = 6 | |
else | |
wtf = 0 | |
end | |
h = (g - b) / d + wtf | |
elseif max == g then | |
h = (b - r) / d + 2 | |
elseif max == b then | |
h = (r - g) / d + 4 | |
end | |
h = h / 6 | |
end | |
print("color is: " .. h .. " " .. " " .. s .. " " .. l) | |
return h, s, l | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment