optim: refactored color methods

This commit is contained in:
Folke Lemaitre
2022-09-05 16:55:26 +02:00
parent 70ca40cfe7
commit 28b790dbd6

View File

@@ -8,21 +8,16 @@ util.bg = "#000000"
util.fg = "#ffffff"
util.day_brightness = 0.3
local function hexToRgb(hex_str)
local hex = "[abcdef0-9][abcdef0-9]"
local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$"
hex_str = string.lower(hex_str)
assert(string.find(hex_str, pat) ~= nil, "hex_to_rgb: invalid hex_str: " .. tostring(hex_str))
local r, g, b = string.match(hex_str, pat)
return { tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) }
---@param c string
local function hexToRgb(c)
c = string.lower(c)
return { tonumber(c:sub(2, 3), 16), tonumber(c:sub(4, 5), 16), tonumber(c:sub(6, 7), 16) }
end
---@param fg string foreground color
---@param bg string background color
---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg
function util.blend(fg, bg, alpha)
function M.blend(fg, bg, alpha)
bg = hexToRgb(bg)
fg = hexToRgb(fg)
@@ -31,7 +26,7 @@ function util.blend(fg, bg, alpha)
return math.floor(math.min(math.max(0, ret), 255) + 0.5)
end
return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3))
return string.format("#%02x%02x%02x", blendChannel(1), blendChannel(2), blendChannel(3))
end
function util.darken(hex, amount, bg)