Skip to content

Commit df1c315

Browse files
committed
feat(utils): add more color utilities for upcoming plugin change
Prepping for a future switch from local-highlight to mini.cursorword. Will detail the reasons in that PR lol Signed-off-by: Jint-lzxy <[email protected]>
1 parent b0d8b4d commit df1c315

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lua/modules/utils/init.lua

+20-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ end
104104
-- NOTE: If the active colorscheme isn't `catppuccin`, this function won't overwrite existing definitions
105105
---Sets a global highlight group.
106106
---@param name string @Highlight group name, e.g. "ErrorMsg"
107-
---@param foreground string @The foreground color
107+
---@param foreground? string @The foreground color
108108
---@param background? string @The background color
109109
---@param italic? boolean
110110
local function set_global_hl(name, foreground, background, italic)
@@ -133,6 +133,24 @@ function M.blend(foreground, background, alpha)
133133
return string.format("#%02x%02x%02x", blend_channel(1), blend_channel(2), blend_channel(3))
134134
end
135135

136+
---Darken a color by blending it with the background color.
137+
---@param hex string @The color in hex to darken
138+
---@param amount number @The amount to darken the color
139+
---@param bg string @The background color to blend with
140+
---@return string @The darkened color as a hex string
141+
function M.darken(hex, amount, bg)
142+
return M.blend(hex, bg or "#000000", math.abs(amount))
143+
end
144+
145+
---Lighten a color by blending it with the foreground color.
146+
---@param hex string @The color in hex to lighten
147+
---@param amount number @The amount to lighten the color
148+
---@param fg string @The foreground color to blend with
149+
---@return string @The lightened color as a hex string
150+
function M.lighten(hex, amount, fg)
151+
return M.blend(hex, fg or "#FFFFFF", math.abs(amount))
152+
end
153+
136154
---Get RGB highlight by highlight group
137155
---@param hl_group string @Highlight group name
138156
---@param use_bg boolean @Returns background or not
@@ -169,6 +187,7 @@ function M.extend_hl(name, def)
169187
local current_def = vim.api.nvim_get_hl(0, { name = name, link = false })
170188
local combined_def = vim.tbl_deep_extend("force", current_def, def)
171189

190+
---@diagnostic disable-next-line: param-type-mismatch
172191
vim.api.nvim_set_hl(0, name, combined_def)
173192
end
174193

0 commit comments

Comments
 (0)