|
|
|
@ -2,14 +2,12 @@ local M = {}
|
|
|
|
|
|
|
|
|
|
|
|
-- Convert a hex color value to RGB
|
|
|
|
-- Convert a hex color value to RGB
|
|
|
|
-- @param hex: The hex color value
|
|
|
|
-- @param hex: The hex color value
|
|
|
|
-- @return r: red (0-255)
|
|
|
|
-- @return r: Red (0-255)
|
|
|
|
-- @return g: green (0-255)
|
|
|
|
-- @return g: Green (0-255)
|
|
|
|
-- @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
|
|
|
|
if string.len(hex) ~= (7 - (hash and 0 or 1)) then return nil end
|
|
|
|
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)
|
|
|
|
@ -18,9 +16,9 @@ M.hex2rgb = function(hex)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Convert an RGB color value to hex
|
|
|
|
-- Convert an RGB color value to hex
|
|
|
|
-- @param r The red value (0-255)
|
|
|
|
-- @param r: Red (0-255)
|
|
|
|
-- @param g The green value (0-255)
|
|
|
|
-- @param g: Green (0-255)
|
|
|
|
-- @param b The blue value (0-255)
|
|
|
|
-- @param b: Blue (0-255)
|
|
|
|
-- @return The hexadecimal string representation of the color
|
|
|
|
-- @return The hexadecimal string representation of the color
|
|
|
|
M.rgb2hex = function(r, g, b)
|
|
|
|
M.rgb2hex = function(r, g, b)
|
|
|
|
return string.format("#%02x%02x%02x", math.floor(r), math.floor(g), math.floor(b))
|
|
|
|
return string.format("#%02x%02x%02x", math.floor(r), math.floor(g), math.floor(b))
|
|
|
|
@ -38,12 +36,12 @@ M.hsl2rgb_helper = function(p, q, a)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Convert a HSL color value to RGB
|
|
|
|
-- Convert a HSL color value to RGB
|
|
|
|
-- @param h: hue (0-360)
|
|
|
|
-- @param h: Hue (0-360)
|
|
|
|
-- @param s: saturation (0-1)
|
|
|
|
-- @param s: Saturation (0-1)
|
|
|
|
-- @param l: lightness (0-1)
|
|
|
|
-- @param l: Lightness (0-1)
|
|
|
|
-- @return r: red (0-255)
|
|
|
|
-- @return r: Red (0-255)
|
|
|
|
-- @return g: green (0-255)
|
|
|
|
-- @return g: Green (0-255)
|
|
|
|
-- @return b: blue (0-255)
|
|
|
|
-- @return b: Blue (0-255)
|
|
|
|
M.hsl2rgb = function(h, s, l)
|
|
|
|
M.hsl2rgb = function(h, s, l)
|
|
|
|
local t1, t2, r, g, b
|
|
|
|
local t1, t2, r, g, b
|
|
|
|
|
|
|
|
|
|
|
|
@ -58,6 +56,7 @@ M.hsl2rgb = function(h, s, l)
|
|
|
|
r = M.hsl2rgb_helper(t1, t2, h + 2) * 255
|
|
|
|
r = M.hsl2rgb_helper(t1, t2, h + 2) * 255
|
|
|
|
g = M.hsl2rgb_helper(t1, t2, h) * 255
|
|
|
|
g = M.hsl2rgb_helper(t1, t2, h) * 255
|
|
|
|
b = M.hsl2rgb_helper(t1, t2, h - 2) * 255
|
|
|
|
b = M.hsl2rgb_helper(t1, t2, h - 2) * 255
|
|
|
|
|
|
|
|
|
|
|
|
return r, g, b
|
|
|
|
return r, g, b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@ -76,9 +75,9 @@ 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) end
|
|
|
|
if maxcolor == 1 then h = (g - b) / (max - min)
|
|
|
|
if maxcolor == 2 then h = 2 + (b - r) / (max - min) end
|
|
|
|
elseif maxcolor == 2 then h = 2 + (b - r) / (max - min)
|
|
|
|
if maxcolor == 3 then h = 4 + (r - g) / (max - min) end
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
@ -91,11 +90,8 @@ M.rgb2hsl = function(r, g, b)
|
|
|
|
if min == max then
|
|
|
|
if min == max then
|
|
|
|
s = 0
|
|
|
|
s = 0
|
|
|
|
else
|
|
|
|
else
|
|
|
|
if l < 0.5 then
|
|
|
|
if l < 0.5 then s = (max - min) / (max + min)
|
|
|
|
s = (max - min) / (max + min)
|
|
|
|
else s = (max - min) / (2 - max - min) end
|
|
|
|
else
|
|
|
|
|
|
|
|
s = (max - min) / (2 - max - min)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return h, s, l
|
|
|
|
return h, s, l
|
|
|
|
@ -103,18 +99,18 @@ end
|
|
|
|
|
|
|
|
|
|
|
|
-- Convert a hex color value to HSL
|
|
|
|
-- Convert a hex color value to HSL
|
|
|
|
-- @param hex: The hex color value
|
|
|
|
-- @param hex: The hex color value
|
|
|
|
-- @return h: Hue
|
|
|
|
-- @param h: Hue (0-360)
|
|
|
|
-- @return s: Saturation
|
|
|
|
-- @param s: Saturation (0-1)
|
|
|
|
-- @return l: Lightness
|
|
|
|
-- @param l: Lightness (0-1)
|
|
|
|
M.hex2hsl = function(hex)
|
|
|
|
M.hex2hsl = function(hex)
|
|
|
|
local r, g, b = M.hex2rgb(hex)
|
|
|
|
local r, g, b = M.hex2rgb(hex)
|
|
|
|
return M.rgb2hsl(r, g, b)
|
|
|
|
return M.rgb2hsl(r, g, b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Convert a HSL color value to hex
|
|
|
|
-- Convert a HSL color value to hex
|
|
|
|
-- @param h Hue
|
|
|
|
-- @param h: Hue (0-360)
|
|
|
|
-- @param s Saturation
|
|
|
|
-- @param s: Saturation (0-1)
|
|
|
|
-- @param l Lightness
|
|
|
|
-- @param l: Lightness (0-1)
|
|
|
|
-- @returns hex color value
|
|
|
|
-- @returns hex color value
|
|
|
|
M.hsl2hex = function(h, s, l)
|
|
|
|
M.hsl2hex = function(h, s, l)
|
|
|
|
local r, g, b = M.hsl2rgb(h, s, l)
|
|
|
|
local r, g, b = M.hsl2rgb(h, s, l)
|
|
|
|
|