mirror of
https://github.com/rose-pine/neovim.git
synced 2025-10-15 12:38:53 +02:00
feat(perf): cache colors for up to 3x speed improvements
This commit is contained in:
parent
f01eac6eed
commit
340cf5ed5e
1 changed files with 20 additions and 2 deletions
|
|
@ -14,10 +14,17 @@ local function color_to_rgb(color)
|
|||
return { byte(new_color, 16), byte(new_color, 8), byte(new_color, 0) }
|
||||
end
|
||||
|
||||
local color_cache = {}
|
||||
|
||||
---@param color string Palette key or hex value
|
||||
function utilities.parse_color(color)
|
||||
if color_cache[color] then
|
||||
return color_cache[color]
|
||||
end
|
||||
|
||||
if color == nil then
|
||||
return print("Invalid color: " .. color)
|
||||
print("Invalid color: " .. color)
|
||||
return nil
|
||||
end
|
||||
|
||||
color = color:lower()
|
||||
|
|
@ -26,13 +33,21 @@ function utilities.parse_color(color)
|
|||
color = require("rose-pine.palette")[color] or vim.api.nvim_get_color_by_name(color)
|
||||
end
|
||||
|
||||
color_cache[color] = color
|
||||
return color
|
||||
end
|
||||
|
||||
local blend_cache = {}
|
||||
|
||||
---@param fg string Foreground color
|
||||
---@param bg string Background color
|
||||
---@param alpha number Between 0 (background) and 1 (foreground)
|
||||
function utilities.blend(fg, bg, alpha)
|
||||
local cache_key = fg .. bg .. alpha
|
||||
if blend_cache[cache_key] then
|
||||
return blend_cache[cache_key]
|
||||
end
|
||||
|
||||
local fg_rgb = color_to_rgb(fg)
|
||||
local bg_rgb = color_to_rgb(bg)
|
||||
|
||||
|
|
@ -41,7 +56,10 @@ function utilities.blend(fg, bg, alpha)
|
|||
return math.floor(math.min(math.max(0, ret), 255) + 0.5)
|
||||
end
|
||||
|
||||
return string.format("#%02X%02X%02X", blend_channel(1), blend_channel(2), blend_channel(3))
|
||||
local result = string.format("#%02X%02X%02X", blend_channel(1), blend_channel(2), blend_channel(3))
|
||||
|
||||
blend_cache[cache_key] = result
|
||||
return result
|
||||
end
|
||||
|
||||
return utilities
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue