mirror of
https://github.com/gabehf/base46.git
synced 2026-03-09 07:28:50 -07:00
minor cleanup
This commit is contained in:
parent
2bdae282fa
commit
f39d712a91
1 changed files with 25 additions and 29 deletions
|
|
@ -2,14 +2,12 @@ local M = {}
|
|||
|
||||
-- Convert a hex color value to RGB
|
||||
-- @param hex: The hex color value
|
||||
-- @return r: red (0-255)
|
||||
-- @return g: green (0-255)
|
||||
-- @return b: blue (0-255)
|
||||
-- @return r: Red (0-255)
|
||||
-- @return g: Green (0-255)
|
||||
-- @return b: Blue (0-255)
|
||||
M.hex2rgb = function(hex)
|
||||
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 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
|
||||
|
||||
-- Convert an RGB color value to hex
|
||||
-- @param r The red value (0-255)
|
||||
-- @param g The green value (0-255)
|
||||
-- @param b The blue value (0-255)
|
||||
-- @param r: Red (0-255)
|
||||
-- @param g: Green (0-255)
|
||||
-- @param b: Blue (0-255)
|
||||
-- @return The hexadecimal string representation of the color
|
||||
M.rgb2hex = function(r, g, 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
|
||||
|
||||
-- Convert a HSL color value to RGB
|
||||
-- @param h: hue (0-360)
|
||||
-- @param s: saturation (0-1)
|
||||
-- @param l: lightness (0-1)
|
||||
-- @return r: red (0-255)
|
||||
-- @return g: green (0-255)
|
||||
-- @return b: blue (0-255)
|
||||
-- @param h: Hue (0-360)
|
||||
-- @param s: Saturation (0-1)
|
||||
-- @param l: Lightness (0-1)
|
||||
-- @return r: Red (0-255)
|
||||
-- @return g: Green (0-255)
|
||||
-- @return b: Blue (0-255)
|
||||
M.hsl2rgb = function(h, s, l)
|
||||
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
|
||||
g = M.hsl2rgb_helper(t1, t2, h) * 255
|
||||
b = M.hsl2rgb_helper(t1, t2, h - 2) * 255
|
||||
|
||||
return r, g, b
|
||||
end
|
||||
|
||||
|
|
@ -76,9 +75,9 @@ M.rgb2hsl = function(r, g, b)
|
|||
max = math.max(r, g, b)
|
||||
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 == 2 then h = 2 + (b - r) / (max - min) end
|
||||
if maxcolor == 3 then h = 4 + (r - g) / (max - min) end
|
||||
if maxcolor == 1 then h = (g - b) / (max - min)
|
||||
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
|
||||
|
||||
|
|
@ -91,11 +90,8 @@ M.rgb2hsl = function(r, g, b)
|
|||
if min == max then
|
||||
s = 0
|
||||
else
|
||||
if l < 0.5 then
|
||||
s = (max - min) / (max + min)
|
||||
else
|
||||
s = (max - min) / (2 - max - min)
|
||||
end
|
||||
if l < 0.5 then s = (max - min) / (max + min)
|
||||
else s = (max - min) / (2 - max - min) end
|
||||
end
|
||||
|
||||
return h, s, l
|
||||
|
|
@ -103,18 +99,18 @@ end
|
|||
|
||||
-- Convert a hex color value to HSL
|
||||
-- @param hex: The hex color value
|
||||
-- @return h: Hue
|
||||
-- @return s: Saturation
|
||||
-- @return l: Lightness
|
||||
-- @param h: Hue (0-360)
|
||||
-- @param s: Saturation (0-1)
|
||||
-- @param l: Lightness (0-1)
|
||||
M.hex2hsl = function(hex)
|
||||
local r, g, b = M.hex2rgb(hex)
|
||||
return M.rgb2hsl(r, g, b)
|
||||
end
|
||||
|
||||
-- Convert a HSL color value to hex
|
||||
-- @param h Hue
|
||||
-- @param s Saturation
|
||||
-- @param l Lightness
|
||||
-- @param h: Hue (0-360)
|
||||
-- @param s: Saturation (0-1)
|
||||
-- @param l: Lightness (0-1)
|
||||
-- @returns hex color value
|
||||
M.hsl2hex = function(h, s, l)
|
||||
local r, g, b = M.hsl2rgb(h, s, l)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue