Skip to content

Instantly share code, notes, and snippets.

@ilya-pi
Created January 13, 2014 22:48
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/8409645 to your computer and use it in GitHub Desktop.
Save ilya-pi/8409645 to your computer and use it in GitHub Desktop.
Lua function to convert HSL color representation to RGB
local function hsl2rgb(h, s, l)
local r, g, b = nil, nil, nil
if s == 0 then
r, g, b = l, l ,l -- achromatic
else
local hue2rgb = function(p, q, t)
if t < 0 then
t = t + 1
end
if t > 1 then
t = t - 1
end
if t < 1/6 then
return p + (q - p) * 6 * t
end
if t < 1/2 then
return q
end
if t < 2/3 then
return p + (q - p) * (2/3 - t) * 6
end
return p
end
local q = nil
if l < 0.5 then
q = l * (1 + s)
else
q = l + s - l * s
end
local p = 2 * l - q
r = hue2rgb(p, q, h + 1/3)
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3)
end
print("color is: " .. r .. " " .. " " .. g .. " " .. b)
return r * 255, g * 255, b * 255
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment