How to make a hsl highlighter using mini.hipatterns? #500
-
|
It can easily highlight hex colors by using local hi = require("mini.hipatterns")
local hsl_pattern = "hsl.()%d+(), ()%d+(), ()%d+()"
return {
highlighters = {
hex_color = hi.gen_highlighter.hex_color({ priority = 2000 }),
hsl_color = {
pattern = function()
return hsl_pattern
end,
group = function(_, _, data)
print(vim.inspect(data))
return MiniHipatterns.compute_hex_color_group(data.full_match, "full")
end,
priority = 2000,
},
},Any help would be appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
Basically, this boils down to programming two parts: In case of HSL the real problem is to convert matched color representation in HSL to its hex string (which is the only big color space Neovim highlight groups support). Here is an example of how I would do it to highlight one of the CSS formats like local hsl_to_hex = function(h, s, l)
-- Actually convert h, s, l numbers into hex color in '#RRGGBB' format
return '#111111'
end
require('mini.hipatterns').setup({
highlighters = {
hsl_color = {
pattern = 'hsl%(%d+ %d+%% %d+%%%)',
group = function(_, match)
local h, s, l = match:match('hsl%((%d+) (%d+)%% (%d+)%%%)')
h, s, l = tonumber(h), tonumber(s), tonumber(l)
local hex_color = hsl_to_hex(h, s, l)
return MiniHipatterns.compute_hex_color_group(hex_color, 'bg')
end,
},
},
}) |
Beta Was this translation helpful? Give feedback.
-
|
Here is my config: https://gist.github.com/craftzdog/8012c78715414de7b5608bda08c77105 Result:
Hope it's helpful for someone :) |
Beta Was this translation helpful? Give feedback.
-
|
Another variant like https://www.w3.org/TR/css-color-3/#hsl-color: -- Returns hex color group for matching hsl() color.
--
---@param match string
---@return string
local hsl_color = function(_, match)
local style = 'bg' -- 'fg' or 'bg'
local hue, saturation, lightness = match:match('hsl%((%d+), ?(%d+)%%, ?(%d+)%%%)')
-- Converts HSL to RGB.
-- https://www.w3.org/TR/css-color-3/#hsl-color
--
---@param h string The hue value in degrees.
---@param s string The saturation value in percent.
---@param l string The lightness value in percent.
---@return integer, integer, integer
local function hsl_to_rgb(h, s, l)
h, s, l = h % 360, s / 100, l / 100
if h < 0 then h = h + 360 end
local function f(n)
local k = (n + h / 30) % 12
local a = s * math.min(l, 1 - l)
return l - a * math.max(-1, math.min(k - 3, 9 - k, 1))
end
return f(0) * 255, f(8) * 255, f(4) * 255
end
local red, green, blue = hsl_to_rgb(hue, saturation, lightness)
local hex = string.format('#%02x%02x%02x', red, green, blue)
return hipatterns.compute_hex_color_group(hex, style)
end hipatterns.setup({
highlighters = {
hsl_color = {
pattern = 'hsl%(%d+, ?%d+%%, ?%d+%%%)',
group = hsl_color,
},
},
}) |
Beta Was this translation helpful? Give feedback.

Basically, this boils down to programming two parts:
In case of HSL the real problem is to convert matched color representation in HSL to its hex string (which is the only big color space Neovim highlight groups support).
Here is an example of how I would do it to highlight one of the CSS formats like
hsl(50 80% 40%)(taken from here):