From dc217296eb661c9a7b425c454dc4ab8294262ea3 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Tue, 24 Aug 2021 22:35:35 -0500 Subject: [PATCH] dev: added colors util --- lua/catppuccino/utils/colors.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lua/catppuccino/utils/colors.lua diff --git a/lua/catppuccino/utils/colors.lua b/lua/catppuccino/utils/colors.lua new file mode 100644 index 0000000..fd55914 --- /dev/null +++ b/lua/catppuccino/utils/colors.lua @@ -0,0 +1,30 @@ +local M = {} + +local function color_is_bright(r, g, b) + -- 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 +end + +function M.hex2rgb(hex) + return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6)) +end + +function M.assert_brightness(color) + 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)) + + if (color_is_bright(tonumber(r), tonumber(g), tonumber(b)) == true) then + return true -- bright + end + + return false -- dull +end + +return M