From 89f68dc827294ef8d2e2e27c1a263f5f3914277d Mon Sep 17 00:00:00 2001 From: Stefan Bartl Date: Tue, 4 Nov 2025 17:35:11 +0100 Subject: [PATCH] fix(colorify): clamp RGBA values to prevent invalid hex strings Introduced a helper function `to_hex(r, g, b, a)` that safely converts RGBA values into valid #rrggbb hex strings by: - Applying alpha to RGB components - Clamping each component to the 01 range - Rounding correctly to 0255 integers Replaced the previous direct string.format(...) call in the LSP color handler with this safe method. This prevents runtime errors in nvim_set_hl caused by malformed hex strings from certain LSP servers (e.g., CSS, SCSS, Tailwind, Lua LSP). No visual or functional changes for end users. --- lua/nvchad/colorify/methods.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lua/nvchad/colorify/methods.lua b/lua/nvchad/colorify/methods.lua index 1679def2..4330f1cf 100644 --- a/lua/nvchad/colorify/methods.lua +++ b/lua/nvchad/colorify/methods.lua @@ -7,6 +7,14 @@ local needs_hl = utils.not_colored local M = {} +local function to_hex(r, g, b, a) + local clamp = function(x) + x = math.floor((x or 0) * (a or 1) * 255 + 0.5) + return math.max(0, math.min(x, 255)) + end + return string.format("#%02x%02x%02x", clamp(r), clamp(g), clamp(b)) +end + M.hex = function(buf, line, str) for col, hex in str:gmatch "()(#%x%x%x%x%x%x)" do col = col - 1 @@ -53,7 +61,7 @@ M.lsp_var = function(buf, line, min, max) a = a / 255 end - local hex = string.format("#%02x%02x%02x", r * a * 255, g * a * 255, b * a * 255) + local hex = to_hex(r, g, b, a) local hl_group = utils.add_hl(hex)