From 340cf5ed5e75673d34055f7f91727ce79965b703 Mon Sep 17 00:00:00 2001 From: mvllow Date: Wed, 5 Feb 2025 19:35:55 -0600 Subject: [PATCH] feat(perf): cache colors for up to 3x speed improvements --- lua/rose-pine/utilities.lua | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lua/rose-pine/utilities.lua b/lua/rose-pine/utilities.lua index 26e019e..32a8575 100644 --- a/lua/rose-pine/utilities.lua +++ b/lua/rose-pine/utilities.lua @@ -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