|
|
|
@ -7,7 +7,9 @@ local M = {}
|
|
|
|
-- @return b: Blue (0-255)
|
|
|
|
-- @return b: Blue (0-255)
|
|
|
|
M.hex2rgb = function(hex)
|
|
|
|
M.hex2rgb = function(hex)
|
|
|
|
local hash = string.sub(hex, 1, 1) == "#"
|
|
|
|
local hash = string.sub(hex, 1, 1) == "#"
|
|
|
|
if string.len(hex) ~= (7 - (hash and 0 or 1)) then return nil end
|
|
|
|
if string.len(hex) ~= (7 - (hash and 0 or 1)) then
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local r = tonumber(hex:sub(2 - (hash and 0 or 1), 3 - (hash and 0 or 1)), 16)
|
|
|
|
local r = tonumber(hex:sub(2 - (hash and 0 or 1), 3 - (hash and 0 or 1)), 16)
|
|
|
|
local g = tonumber(hex:sub(4 - (hash and 0 or 1), 5 - (hash and 0 or 1)), 16)
|
|
|
|
local g = tonumber(hex:sub(4 - (hash and 0 or 1), 5 - (hash and 0 or 1)), 16)
|
|
|
|
@ -27,12 +29,21 @@ end
|
|
|
|
-- Helper function to convert a HSL color value to RGB
|
|
|
|
-- Helper function to convert a HSL color value to RGB
|
|
|
|
-- Not to be used directly, use M.hsl2rgb instead
|
|
|
|
-- Not to be used directly, use M.hsl2rgb instead
|
|
|
|
M.hsl2rgb_helper = function(p, q, a)
|
|
|
|
M.hsl2rgb_helper = function(p, q, a)
|
|
|
|
if (a < 0) then a = a + 6 end
|
|
|
|
if a < 0 then
|
|
|
|
if (a >= 6) then a = a - 6 end
|
|
|
|
a = a + 6
|
|
|
|
if (a < 1) then return (q - p) * a + p
|
|
|
|
end
|
|
|
|
elseif (a < 3) then return q
|
|
|
|
if a >= 6 then
|
|
|
|
elseif (a < 4) then return (q - p) * (4 - a) + p
|
|
|
|
a = a - 6
|
|
|
|
else return p end
|
|
|
|
end
|
|
|
|
|
|
|
|
if a < 1 then
|
|
|
|
|
|
|
|
return (q - p) * a + p
|
|
|
|
|
|
|
|
elseif a < 3 then
|
|
|
|
|
|
|
|
return q
|
|
|
|
|
|
|
|
elseif a < 4 then
|
|
|
|
|
|
|
|
return (q - p) * (4 - a) + p
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
return p
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Convert a HSL color value to RGB
|
|
|
|
-- Convert a HSL color value to RGB
|
|
|
|
@ -46,7 +57,7 @@ M.hsl2rgb = function(h, s, l)
|
|
|
|
local t1, t2, r, g, b
|
|
|
|
local t1, t2, r, g, b
|
|
|
|
|
|
|
|
|
|
|
|
h = h / 60
|
|
|
|
h = h / 60
|
|
|
|
if (l <= 0.5) then
|
|
|
|
if l <= 0.5 then
|
|
|
|
t2 = l * (s + 1)
|
|
|
|
t2 = l * (s + 1)
|
|
|
|
else
|
|
|
|
else
|
|
|
|
t2 = l + s - (l * s)
|
|
|
|
t2 = l + s - (l * s)
|
|
|
|
@ -75,23 +86,34 @@ M.rgb2hsl = function(r, g, b)
|
|
|
|
max = math.max(r, g, b)
|
|
|
|
max = math.max(r, g, b)
|
|
|
|
maxcolor = 1 + (max == b and 2 or (max == g and 1 or 0))
|
|
|
|
maxcolor = 1 + (max == b and 2 or (max == g and 1 or 0))
|
|
|
|
|
|
|
|
|
|
|
|
if maxcolor == 1 then h = (g - b) / (max - min)
|
|
|
|
if maxcolor == 1 then
|
|
|
|
elseif maxcolor == 2 then h = 2 + (b - r) / (max - min)
|
|
|
|
h = (g - b) / (max - min)
|
|
|
|
elseif maxcolor == 3 then h = 4 + (r - g) / (max - min) end
|
|
|
|
elseif maxcolor == 2 then
|
|
|
|
|
|
|
|
h = 2 + (b - r) / (max - min)
|
|
|
|
|
|
|
|
elseif maxcolor == 3 then
|
|
|
|
|
|
|
|
h = 4 + (r - g) / (max - min)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if not rawequal(type(h), 'number') then h = 0 end
|
|
|
|
if not rawequal(type(h), "number") then
|
|
|
|
|
|
|
|
h = 0
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
h = h * 60
|
|
|
|
h = h * 60
|
|
|
|
|
|
|
|
|
|
|
|
if h < 0 then h = h + 360 end
|
|
|
|
if h < 0 then
|
|
|
|
|
|
|
|
h = h + 360
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
l = (min + max) / 2
|
|
|
|
l = (min + max) / 2
|
|
|
|
|
|
|
|
|
|
|
|
if min == max then
|
|
|
|
if min == max then
|
|
|
|
s = 0
|
|
|
|
s = 0
|
|
|
|
else
|
|
|
|
else
|
|
|
|
if l < 0.5 then s = (max - min) / (max + min)
|
|
|
|
if l < 0.5 then
|
|
|
|
else s = (max - min) / (2 - max - min) end
|
|
|
|
s = (max - min) / (max + min)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
s = (max - min) / (2 - max - min)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return h, s, l
|
|
|
|
return h, s, l
|
|
|
|
@ -125,8 +147,12 @@ end
|
|
|
|
M.change_hex_hue = function(hex, percent)
|
|
|
|
M.change_hex_hue = function(hex, percent)
|
|
|
|
local h, s, l = M.hex2hsl(hex)
|
|
|
|
local h, s, l = M.hex2hsl(hex)
|
|
|
|
h = h + (percent / 100)
|
|
|
|
h = h + (percent / 100)
|
|
|
|
if h > 360 then h = 360 end
|
|
|
|
if h > 360 then
|
|
|
|
if h < 0 then h = 0 end
|
|
|
|
h = 360
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if h < 0 then
|
|
|
|
|
|
|
|
h = 0
|
|
|
|
|
|
|
|
end
|
|
|
|
return M.hsl2hex(h, s, l)
|
|
|
|
return M.hsl2hex(h, s, l)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@ -138,8 +164,12 @@ end
|
|
|
|
M.change_hex_saturation = function(hex, percent)
|
|
|
|
M.change_hex_saturation = function(hex, percent)
|
|
|
|
local h, s, l = M.hex2hsl(hex)
|
|
|
|
local h, s, l = M.hex2hsl(hex)
|
|
|
|
s = s + (percent / 100)
|
|
|
|
s = s + (percent / 100)
|
|
|
|
if s > 1 then s = 1 end
|
|
|
|
if s > 1 then
|
|
|
|
if s < 0 then s = 0 end
|
|
|
|
s = 1
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if s < 0 then
|
|
|
|
|
|
|
|
s = 0
|
|
|
|
|
|
|
|
end
|
|
|
|
return M.hsl2hex(h, s, l)
|
|
|
|
return M.hsl2hex(h, s, l)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@ -151,8 +181,12 @@ end
|
|
|
|
M.change_hex_lightness = function(hex, percent)
|
|
|
|
M.change_hex_lightness = function(hex, percent)
|
|
|
|
local h, s, l = M.hex2hsl(hex)
|
|
|
|
local h, s, l = M.hex2hsl(hex)
|
|
|
|
l = l + (percent / 100)
|
|
|
|
l = l + (percent / 100)
|
|
|
|
if l > 1 then l = 1 end
|
|
|
|
if l > 1 then
|
|
|
|
if l < 0 then l = 0 end
|
|
|
|
l = 1
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if l < 0 then
|
|
|
|
|
|
|
|
l = 0
|
|
|
|
|
|
|
|
end
|
|
|
|
return M.hsl2hex(h, s, l)
|
|
|
|
return M.hsl2hex(h, s, l)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|