Skip to content

Conversation

@StefanBartl
Copy link

Summary

This PR fixes a critical bug in nvchad.colorify.methods.lua where colors received from LSP via textDocument/documentColor were incorrectly converted from RGBA to hex strings.

The original implementation directly multiplied floating-point values without clamping or rounding, producing invalid hex strings such as #fe01fe01fe01, which caused runtime crashes when passed to nvim_set_hl() (see colorify/utils.lua: line #31).

What was changed

  • Introduced a safe to_hex(r, g, b, a) function that:

    • Multiplies RGBA values with proper rounding (+0.5)
    • Clamps each component to the 0–255 range
    • Constructs valid 7-character hex strings of the form #rrggbb
  • Replaced the direct string.format(...) call with this safe method inside the LSP color handler.

Why this matters

Without this fix, certain LSP servers (e.g. CSS, SCSS, Tailwind) may cause hard crashes when they return slightly invalid or unnormalized RGBA values. This bug is reproducible with color-aware LSPs (e.g. for CSS, SCSS, TailwindCSS, or Vue via Volar) that return fractional RGBA values.

Example Error

Error executing vim.schedule lua callback:
.../colorify/utils.lua:31:
Invalid highlight color: '#fe01fe01fe01'

Additional Notes

No visual or functional changes for users.

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.
@StefanBartl
Copy link
Author

StefanBartl commented Nov 4, 2025

@siduck: reopened PR #500

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant