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