feat(nvim): new util functions

This commit is contained in:
Price Hiller 2023-02-12 19:21:43 -06:00
parent bb2b68557d
commit aeece242aa

View File

@ -1,15 +1,15 @@
local U = {} local U = {}
U.rgbToHex = function(rgb) U.rgbToHex = function(rgb)
return string.format('#%06x', rgb) return string.format("#%06x", rgb)
end end
U.hexToRgb = function(hex_str) U.hexToRgb = function(hex_str)
local hex = '[abcdef0-9][abcdef0-9]' local hex = "[abcdef0-9][abcdef0-9]"
local pat = '^#(' .. hex .. ')(' .. hex .. ')(' .. hex .. ')$' local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$"
hex_str = string.lower(hex_str) hex_str = string.lower(hex_str)
assert(string.find(hex_str, pat) ~= nil, 'hex_to_rgb: invalid hex_str: ' .. tostring(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) local r, g, b = string.match(hex_str, pat)
return { tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) } return { tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) }
@ -24,15 +24,20 @@ U.blend = function(fg, bg, alpha)
return math.floor(math.min(math.max(0, ret), 255) + 0.5) return math.floor(math.min(math.max(0, ret), 255) + 0.5)
end 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 end
U.darken = function(hex, amount, bg) U.darken = function(hex, amount, bg)
return U.blend(hex, bg or '#000000', math.abs(amount)) return U.blend(hex, bg or "#000000", math.abs(amount))
end end
U.lighten = function(hex, amount, fg) U.lighten = function(hex, amount, fg)
return U.blend(hex, fg or '#ffffff', math.abs(amount)) return U.blend(hex, fg or "#ffffff", math.abs(amount))
end
U.get_color = function(group, attr)
local fn = vim.fn
return fn.synIDattr(fn.synIDtrans(fn.hlID(group)), attr)
end end
return U return U