Skip to content

colorify error when using oklch colors in color-mix() css functions #541

Description

@nino-mau

In a CSS file with the css_variables LSP active, oklch() colors used inside color-mix() functions cause the following error:

Error executing vim.schedule lua callback: ...local/share/nvchad/lazy/ui/lua/nvchad/colorify/utils.lua:31: Invalid highlight color: '#e7ffffffffffffffe80a'
stack traceback:
        [C]: in function 'nvim_set_hl'
        ...local/share/nvchad/lazy/ui/lua/nvchad/colorify/utils.lua:31: in function 'add_hl'
        ...cal/share/nvchad/lazy/ui/lua/nvchad/colorify/methods.lua:58: in function 'handler'
        /usr/share/nvim/runtime/lua/vim/lsp/client.lua:682: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

I can reproduce the error consistently with this snippet:

:root {
  --bg: oklch(1 0 0);
  --fg: oklch(0.577 0.245 27.325);
  --mixed: color-mix(in oklab, var(--fg) 10%, var(--bg));
}

The issue is with the way you build the hex string there

local hex = string.format("#%02x%02x%02x", r * a * 255, g * a * 255, b * a * 255)

This fix worked for me (I took what AI suggested might not be the best fix):

local function clamp(v) return math.max(0, math.min(255, math.floor(v + 0.5))) end
local hex = string.format("#%02x%02x%02x", clamp(r * a * 255), clamp(g * a * 255), clamp(b * a * 255))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions