Skip to content

Commit 81e676d

Browse files
authored
feat oklch (#160)
* feat(oklch): add support for this css function You can now see and enable oklch highlighting. * docs(oklch): add Update documentation with script.
1 parent 51cf7c9 commit 81e676d

26 files changed

+605
-68
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ library to do custom highlighting themselves.
146146
AARRGGBB = false, -- 0xAARRGGBB hex codes
147147
rgb_fn = false, -- CSS rgb() and rgba() functions
148148
hsl_fn = false, -- CSS hsl() and hsla() functions
149+
oklch_fn = false, -- CSS oklch() function
149150
css = false, -- Enable all CSS *features*:
150-
-- names, RGB, RGBA, RRGGBB, RRGGBBAA, AARRGGBB, rgb_fn, hsl_fn
151-
css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn
151+
-- names, RGB, RGBA, RRGGBB, RRGGBBAA, AARRGGBB, rgb_fn, hsl_fn, oklch_fn
152+
css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn, oklch_fn
152153
-- Tailwind colors. boolean|'normal'|'lsp'|'both'. True sets to 'normal'
153154
tailwind = false, -- Enable tailwind colors
154155
tailwind_opts = { -- Options for highlighting tailwind names
@@ -227,7 +228,7 @@ require("colorizer").setup({
227228
require("colorizer").setup({
228229
filetypes = {
229230
"*", -- Highlight all files, but customize some others.
230-
css = { rgb_fn = true }, -- Enable parsing rgb(...) functions in css.
231+
css = { rgb_fn = true, oklch_fn = true }, -- Enable parsing rgb(...) and oklch(...) functions in css.
231232
html = { names = false }, -- Disable parsing "names" like Blue or Gray
232233
},
233234
})
@@ -284,22 +285,22 @@ require("colorizer").setup({
284285
In `user_default_options`, there are 2 types of options
285286

286287
1. Individual options - `names`, `names_opts`, `names_custom`, `RGB`, `RGBA`,
287-
`RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`, `RRGGBBAA`, `AARRGGBB`, `tailwind`,
288+
`RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`, `oklch_fn`, `RRGGBBAA`, `AARRGGBB`, `tailwind`,
288289
`sass`, `xterm`
289290

290291
1. Alias options - `css`, `css_fn`
291292

292-
If `css_fn` is true `hsl_fn`, `rgb_fn` becomes `true`
293+
If `css_fn` is true `hsl_fn`, `rgb_fn`, `oklch_fn` becomes `true`
293294

294-
If `css` is true `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
295+
If `css` is true `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`, `oklch_fn`
295296
becomes `true`
296297

297298
These options have a priority, Individual options have the highest priority,
298299
then alias options
299300

300301
For alias, `css_fn` has more priority over `css`
301302

302-
e.g: Here `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB`, `hsl_fn`, `rgb_fn` is
303+
e.g: Here `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB`, `hsl_fn`, `rgb_fn`, `oklch_fn` is
303304
enabled but not `names`
304305

305306
```lua
@@ -312,7 +313,7 @@ require("colorizer").setup({
312313
```
313314

314315
e.g: Here `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB` is enabled but
315-
not `rgb_fn` and `hsl_fn`
316+
not `rgb_fn`, `hsl_fn`, and `oklch_fn`
316317

317318
```lua
318319
require("colorizer").setup({

doc/colorizer.txt

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ Functions: ~
342342
|is_bright| - Determines whether a color is bright, helping decide text
343343
color.
344344

345+
|oklch_to_rgb| - Converts an OKLCH color value to RGB.
346+
345347

346348
hsl_to_rgb({h}, {s}, {l}) *colorizer.color.hsl_to_rgb*
347349
Converts an HSL color value to RGB.
@@ -405,6 +407,33 @@ is_bright({r}, {g}, {b}) *colorizer.color.is_bright*
405407

406408

407409

410+
oklch_to_rgb({L}, {C}, {H}) *colorizer.color.oklch_to_rgb*
411+
Converts an OKLCH color value to RGB.
412+
413+
OKLCH is a perceptual color space that provides better uniformity than HSL.
414+
Accepts lightness, chroma, and hue values and converts them to RGB.
415+
416+
References:
417+
- OKLCH/OKLab specification: https://bottosson.github.io/posts/oklab/
418+
- W3C CSS Color Module Level 4: https://www.w3.org/TR/css-color-4/#ok-lab
419+
- Conversion algorithms:
420+
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/oklch
421+
422+
423+
424+
Parameters: ~
425+
{L} - number: Lightness, in the range [0, 1].
426+
{C} - number: Chroma, typically in the range [0, 0.4] but can be
427+
higher.
428+
{H} - number: Hue, in degrees [0, 360].
429+
430+
returns:~
431+
number or nil,number or nil,number or nil: Returns red, green, and blue
432+
values
433+
scaled to [0, 255], or nil if any input value is out of range.
434+
435+
436+
408437
==============================================================================
409438
CONFIG *colorizer.config-introduction*
410439

@@ -508,12 +537,13 @@ user_default_options *colorizer.config.user_default_options*
508537

509538
Individual Options: Options like `names`, `RGB`, `RRGGBB`, `RRGGBBAA`,
510539
`hsl_fn`, `rgb_fn`,
511-
`AARRGGBB`, `tailwind`, and `sass` can be enabled or disabled independently.
540+
`oklch_fn`, `AARRGGBB`, `tailwind`, and `sass` can be enabled or disabled
541+
independently.
512542

513543
Alias Options: `css` and `css_fn` enable multiple options at once.
514-
- `css_fn = true` enables `hsl_fn` and `rgb_fn`.
515-
- `css = true` enables `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, and
516-
`rgb_fn`.
544+
- `css_fn = true` enables `hsl_fn`, `rgb_fn`, and `oklch_fn`.
545+
- `css = true` enables `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`,
546+
`rgb_fn`, and `oklch_fn`.
517547

518548
Option Priority: Individual options have higher priority than aliases.
519549
If both `css` and `css_fn` are true, `css_fn` has more priority over `css`.
@@ -533,9 +563,11 @@ user_default_options *colorizer.config.user_default_options*
533563
{AARRGGBB} - boolean: Enables `0xAARRGGBB` hex codes.
534564
{rgb_fn} - boolean: Enables CSS `rgb()` and `rgba()` functions.
535565
{hsl_fn} - boolean: Enables CSS `hsl()` and `hsla()` functions.
536-
{css} - boolean: Enables all CSS features (`rgb_fn`, `hsl_fn`, `names`,
537-
`RGB`, `RRGGBB`).
538-
{css_fn} - boolean: Enables all CSS functions (`rgb_fn`, `hsl_fn`).
566+
{oklch_fn} - boolean: Enables CSS `oklch()` function.
567+
{css} - boolean: Enables all CSS features (`rgb_fn`, `hsl_fn`,
568+
`oklch_fn`, `names`, `RGB`, `RRGGBB`).
569+
{css_fn} - boolean: Enables all CSS functions (`rgb_fn`, `hsl_fn`,
570+
`oklch_fn`).
539571
{tailwind} - boolean|string: Enables Tailwind CSS colors (e.g.,
540572
`"normal"`, `"lsp"`, `"both"`).
541573
{tailwind_opts} - table: Tailwind options for updating names cache, etc
@@ -595,10 +627,11 @@ ud_opts *colorizer.config.ud_opts*
595627
- `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
596628
- `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.
597629
- `hsl_fn` (boolean): Enables CSS `hsl()` and `hsla()` functions.
630+
- `oklch_fn` (boolean): Enables CSS `oklch()` function.
598631
- `css` (boolean): Enables all CSS-related features (e.g., `names`, `RGB`,
599-
`RRGGBB`, `hsl_fn`, `rgb_fn`).
632+
`RRGGBB`, `hsl_fn`, `rgb_fn`, `oklch_fn`).
600633
- `css_fn` (boolean): Enables all CSS function-related features (e.g.,
601-
`rgb_fn`, `hsl_fn`).
634+
`rgb_fn`, `hsl_fn`, `oklch_fn`).
602635
- `tailwind` (boolean|string): Enables Tailwind CSS colors. Accepts `true`,
603636
`"normal"`, `"lsp"`, or `"both"`.
604637
- `tailwind_opts` (table): Tailwind options for updating names cache, etc
@@ -879,6 +912,50 @@ parser({line}, {i}, {m_opts}) *colorizer.parser.names.parser*
879912

880913

881914

915+
==============================================================================
916+
OKLCH *colorizer.parser.oklch-introduction*
917+
918+
This module provides a parser for identifying and converting `oklch()` CSS
919+
functions to RGB hexadecimal format.
920+
921+
OKLCH is a perceptual color space that provides better uniformity than HSL.
922+
It supports lightness as both decimal (0-1) and percentage (0-100%),
923+
chroma values, hue in degrees, and optional alpha transparency.
924+
This function is useful for syntax highlighting or color recognition in a text
925+
editor.
926+
927+
==============================================================================
928+
LUA API *colorizer.parser.oklch-lua-api*
929+
930+
Functions: ~
931+
|parser| - Parses `oklch()` CSS functions and converts them to RGB
932+
hexadecimal format.
933+
934+
935+
parser({line}, {i}, {_}) *colorizer.parser.oklch.parser*
936+
Parses `oklch()` CSS functions and converts them to RGB hexadecimal format.
937+
938+
This function matches `oklch()` functions within a line of text, extracting
939+
and converting
940+
the lightness, chroma, and hue to an RGB color. It handles lightness as
941+
decimal or percentage,
942+
and an optional alpha (transparency) value.
943+
944+
945+
Parameters: ~
946+
{line} - string: The line of text to parse
947+
{i} - number: The starting index within the line where parsing should
948+
begin
949+
{_} - table: Parsing options (unused, included for API consistency)
950+
951+
returns:~
952+
number or nil: The end index of the parsed `oklch` function within the
953+
line, or `nil` if no match was found.
954+
string or nil: The RGB hexadecimal color (e.g., "ff0000" for red), or
955+
`nil` if parsing failed
956+
957+
958+
882959
==============================================================================
883960
RGBA_HEX *colorizer.parser.rgba_hex-introduction*
884961

@@ -1032,10 +1109,11 @@ Functions: ~
10321109
parser({line}, {i}) *colorizer.parser.xterm.parser*
10331110
Parses xterm color codes and converts them to RGB hex format.
10341111

1035-
This function matches both #xNN format (decimal, 0-255) and ANSI escape
1036-
sequences \e[38;5;NNNm
1037-
for xterm 256-color palette. It returns the corresponding RGB hex string
1038-
from the xterm color palette.
1112+
This function matches following color codes:
1113+
1. #xNN format (decimal, 0-255).
1114+
2. ANSI escape sequences \e[38;5;NNNm for xterm 256-color palette.
1115+
3. ANSI escape sequences \e[X;Ym for xterm 16-color palette.
1116+
It returns the corresponding RGB hex string from the xterm color palette.
10391117

10401118

10411119
Parameters: ~

doc/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ <h2>Modules</h2>
4141
<li><a href="modules/colorizer.parser.rgb_hex.html">parser.rgb_hex</a></li>
4242
<li><a href="modules/colorizer.parser.hsl.html">parser.hsl</a></li>
4343
<li><a href="modules/colorizer.parser.names.html">parser.names</a></li>
44+
<li><a href="modules/colorizer.parser.oklch.html">parser.oklch</a></li>
4445
<li><a href="modules/colorizer.parser.rgb.html">parser.rgb</a></li>
4546
<li><a href="modules/colorizer.parser.rgba_hex.html">parser.rgba_hex</a></li>
4647
<li><a href="modules/colorizer.parser.xterm.html">parser.xterm</a></li>
@@ -95,6 +96,10 @@ <h2>Modules</h2>
9596
<td class="name" nowrap><a href="modules/colorizer.parser.names.html">colorizer.parser.names</a></td>
9697
<td class="summary">This module provides a parser that identifies named colors from a given line of text.</td>
9798
</tr>
99+
<tr>
100+
<td class="name" nowrap><a href="modules/colorizer.parser.oklch.html">colorizer.parser.oklch</a></td>
101+
<td class="summary">This module provides a parser for identifying and converting `oklch()` CSS functions to RGB hexadecimal format.</td>
102+
</tr>
98103
<tr>
99104
<td class="name" nowrap><a href="modules/colorizer.parser.rgb.html">colorizer.parser.rgb</a></td>
100105
<td class="summary">This module provides a parser for identifying and converting `rgb()` and `rgba()` CSS functions to RGB hexadecimal format.</td>
@@ -133,7 +138,7 @@ <h2>Modules</h2>
133138
</div> <!-- id="main" -->
134139
<div id="about">
135140
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
136-
<i style="float:right;">Last updated - July </i>
141+
<i style="float:right;">Last updated - October </i>
137142
</div> <!-- id="about" -->
138143
</div> <!-- id="container" -->
139144
</body>

doc/modules/colorizer.buffer.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ <h2>Modules</h2>
4848
<li><a href="../modules/colorizer.parser.rgb_hex.html">parser.rgb_hex</a></li>
4949
<li><a href="../modules/colorizer.parser.hsl.html">parser.hsl</a></li>
5050
<li><a href="../modules/colorizer.parser.names.html">parser.names</a></li>
51+
<li><a href="../modules/colorizer.parser.oklch.html">parser.oklch</a></li>
5152
<li><a href="../modules/colorizer.parser.rgb.html">parser.rgb</a></li>
5253
<li><a href="../modules/colorizer.parser.rgba_hex.html">parser.rgba_hex</a></li>
5354
<li><a href="../modules/colorizer.parser.xterm.html">parser.xterm</a></li>
@@ -234,7 +235,7 @@ <h3>Returns:</h3>
234235
</div> <!-- id="main" -->
235236
<div id="about">
236237
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
237-
<i style="float:right;">Last updated - July </i>
238+
<i style="float:right;">Last updated - October </i>
238239
</div> <!-- id="about" -->
239240
</div> <!-- id="container" -->
240241
</body>

doc/modules/colorizer.color.html

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ <h2>Modules</h2>
4848
<li><a href="../modules/colorizer.parser.rgb_hex.html">parser.rgb_hex</a></li>
4949
<li><a href="../modules/colorizer.parser.hsl.html">parser.hsl</a></li>
5050
<li><a href="../modules/colorizer.parser.names.html">parser.names</a></li>
51+
<li><a href="../modules/colorizer.parser.oklch.html">parser.oklch</a></li>
5152
<li><a href="../modules/colorizer.parser.rgb.html">parser.rgb</a></li>
5253
<li><a href="../modules/colorizer.parser.rgba_hex.html">parser.rgba_hex</a></li>
5354
<li><a href="../modules/colorizer.parser.xterm.html">parser.xterm</a></li>
@@ -81,6 +82,10 @@ <h2><a href="#Functions">Functions</a></h2>
8182
<td class="name" nowrap><a href="#is_bright">is_bright (r, g, b)</a></td>
8283
<td class="summary">Determines whether a color is bright, helping decide text color.</td>
8384
</tr>
85+
<tr>
86+
<td class="name" nowrap><a href="#oklch_to_rgb">oklch_to_rgb (L, C, H)</a></td>
87+
<td class="summary">Converts an OKLCH color value to RGB.</td>
88+
</tr>
8489
</table>
8590

8691
<br/>
@@ -195,6 +200,45 @@ <h3>Returns:</h3>
195200

196201

197202

203+
</dd>
204+
<dt>
205+
<a name = "oklch_to_rgb"></a>
206+
<strong>oklch_to_rgb (L, C, H)</strong>
207+
</dt>
208+
<dd>
209+
Converts an OKLCH color value to RGB.
210+
OKLCH is a perceptual color space that provides better uniformity than HSL.
211+
Accepts lightness, chroma, and hue values and converts them to RGB.
212+
<p> References:
213+
- OKLCH/OKLab specification: https://bottosson.github.io/posts/oklab/
214+
- W3C CSS Color Module Level 4: https://www.w3.org/TR/css-color-4/#ok-lab
215+
- Conversion algorithms: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/oklch
216+
217+
218+
219+
<h3>Parameters:</h3>
220+
<ul>
221+
<li><span class="parameter">L</span>
222+
number: Lightness, in the range [0, 1].
223+
</li>
224+
<li><span class="parameter">C</span>
225+
number: Chroma, typically in the range [0, 0.4] but can be higher.
226+
</li>
227+
<li><span class="parameter">H</span>
228+
number: Hue, in degrees [0, 360].
229+
</li>
230+
</ul>
231+
232+
<h3>Returns:</h3>
233+
<ol>
234+
235+
number|nil,number|nil,number|nil: Returns red, green, and blue values
236+
scaled to [0, 255], or nil if any input value is out of range.
237+
</ol>
238+
239+
240+
241+
198242
</dd>
199243
</dl>
200244

@@ -203,7 +247,7 @@ <h3>Returns:</h3>
203247
</div> <!-- id="main" -->
204248
<div id="about">
205249
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
206-
<i style="float:right;">Last updated - July </i>
250+
<i style="float:right;">Last updated - October </i>
207251
</div> <!-- id="about" -->
208252
</div> <!-- id="container" -->
209253
</body>

doc/modules/colorizer.config.html

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ <h2>Modules</h2>
4949
<li><a href="../modules/colorizer.parser.rgb_hex.html">parser.rgb_hex</a></li>
5050
<li><a href="../modules/colorizer.parser.hsl.html">parser.hsl</a></li>
5151
<li><a href="../modules/colorizer.parser.names.html">parser.names</a></li>
52+
<li><a href="../modules/colorizer.parser.oklch.html">parser.oklch</a></li>
5253
<li><a href="../modules/colorizer.parser.rgb.html">parser.rgb</a></li>
5354
<li><a href="../modules/colorizer.parser.rgba_hex.html">parser.rgba_hex</a></li>
5455
<li><a href="../modules/colorizer.parser.xterm.html">parser.xterm</a></li>
@@ -259,10 +260,10 @@ <h2 class="section-header "><a name="Tables"></a>Tables</h2>
259260
This table defines individual options and alias options, allowing configuration of
260261
colorizer's behavior for different color formats (e.g., `#RGB`, `#RRGGBB`, `#AARRGGBB`, etc.).
261262
<p>Individual Options: Options like `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`,
262-
`AARRGGBB`, `tailwind`, and `sass` can be enabled or disabled independently.
263+
`oklch_fn`, `AARRGGBB`, `tailwind`, and `sass` can be enabled or disabled independently.
263264
<p>Alias Options: `css` and `css_fn` enable multiple options at once.
264-
- `css_fn = true` enables `hsl_fn` and `rgb_fn`.
265-
- `css = true` enables `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, and `rgb_fn`.
265+
- `css_fn = true` enables `hsl_fn`, `rgb_fn`, and `oklch_fn`.
266+
- `css = true` enables `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`, and `oklch_fn`.
266267
<p>Option Priority: Individual options have higher priority than aliases.
267268
If both `css` and `css_fn` are true, `css_fn` has more priority over `css`.
268269

@@ -300,11 +301,14 @@ <h3>Fields:</h3>
300301
<li><span class="parameter">hsl_fn</span>
301302
boolean: Enables CSS `hsl()` and `hsla()` functions.
302303
</li>
304+
<li><span class="parameter">oklch_fn</span>
305+
boolean: Enables CSS `oklch()` function.
306+
</li>
303307
<li><span class="parameter">css</span>
304-
boolean: Enables all CSS features (`rgb_fn`, `hsl_fn`, `names`, `RGB`, `RRGGBB`).
308+
boolean: Enables all CSS features (`rgb_fn`, `hsl_fn`, `oklch_fn`, `names`, `RGB`, `RRGGBB`).
305309
</li>
306310
<li><span class="parameter">css_fn</span>
307-
boolean: Enables all CSS functions (`rgb_fn`, `hsl_fn`).
311+
boolean: Enables all CSS functions (`rgb_fn`, `hsl_fn`, `oklch_fn`).
308312
</li>
309313
<li><span class="parameter">tailwind</span>
310314
boolean|string: Enables Tailwind CSS colors (e.g., `"normal"`, `"lsp"`, `"both"`).
@@ -414,8 +418,9 @@ <h3>Fields:</h3>
414418
- `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
415419
- `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.
416420
- `hsl_fn` (boolean): Enables CSS `hsl()` and `hsla()` functions.
417-
- `css` (boolean): Enables all CSS-related features (e.g., `names`, `RGB`, `RRGGBB`, `hsl_fn`, `rgb_fn`).
418-
- `css_fn` (boolean): Enables all CSS function-related features (e.g., `rgb_fn`, `hsl_fn`).
421+
- `oklch_fn` (boolean): Enables CSS `oklch()` function.
422+
- `css` (boolean): Enables all CSS-related features (e.g., `names`, `RGB`, `RRGGBB`, `hsl_fn`, `rgb_fn`, `oklch_fn`).
423+
- `css_fn` (boolean): Enables all CSS function-related features (e.g., `rgb_fn`, `hsl_fn`, `oklch_fn`).
419424
- `tailwind` (boolean|string): Enables Tailwind CSS colors. Accepts `true`, `"normal"`, `"lsp"`, or `"both"`.
420425
- `tailwind_opts` (table): Tailwind options for updating names cache, etc
421426
- `update_names` (boolean): Updates Tailwind "normal" names cache from LSP results. This provides a smoother highlighting experience when tailwind = "both" is used. Highlighting on non-tailwind lsp buffers (like cmp) becomes more consistent.
@@ -460,7 +465,7 @@ <h3>Fields:</h3>
460465
</div> <!-- id="main" -->
461466
<div id="about">
462467
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
463-
<i style="float:right;">Last updated - July </i>
468+
<i style="float:right;">Last updated - October </i>
464469
</div> <!-- id="about" -->
465470
</div> <!-- id="container" -->
466471
</body>

0 commit comments

Comments
 (0)