mirror of https://github.com/sgoudham/nvim.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
785 B
Lua
31 lines
785 B
Lua
3 years ago
|
local M = {}
|
||
|
|
||
|
local function color_is_bright(r, g, b)
|
||
3 years ago
|
-- Counting the perceptive luminance - human eye favors green color
|
||
|
local luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
|
||
|
if luminance > 0.5 then
|
||
|
return true -- Bright colors, black font
|
||
|
else
|
||
|
return false -- Dark colors, white font
|
||
|
end
|
||
3 years ago
|
end
|
||
|
|
||
|
function M.hex2rgb(hex)
|
||
3 years ago
|
return tonumber("0x" .. hex:sub(1, 2)), tonumber("0x" .. hex:sub(3, 4)), tonumber("0x" .. hex:sub(5, 6))
|
||
3 years ago
|
end
|
||
|
|
||
|
function M.assert_brightness(color)
|
||
3 years ago
|
local hex = color:gsub("#", "")
|
||
|
local r = M.hex2rgb(string.sub(hex, 1, 2))
|
||
|
local g = M.hex2rgb(string.sub(hex, 3, 4))
|
||
|
local b = M.hex2rgb(string.sub(hex, 5, 6))
|
||
3 years ago
|
|
||
3 years ago
|
if color_is_bright(tonumber(r), tonumber(g), tonumber(b)) == true then
|
||
3 years ago
|
return true -- bright
|
||
3 years ago
|
end
|
||
3 years ago
|
|
||
|
return false -- dull
|
||
|
end
|
||
|
|
||
|
return M
|