Skip to content

Instantly share code, notes, and snippets.

@ilya-pi
Created January 13, 2014 22:49
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 ilya-pi/8409654 to your computer and use it in GitHub Desktop.
Save ilya-pi/8409654 to your computer and use it in GitHub Desktop.
Lua function to convert RGB color representation to HSL
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