Skip to content

Commit

Permalink
feat: #rgba support (#120)
Browse files Browse the repository at this point in the history
* feat: adds utils.rgb_to_hex function

* feat: adds support for #RGBA highlighting

* doc: generates ldoc

* chore: adds RGBA to expect.lua config

* ref: refactors names parser

* test: removes fail cases for #RGBA
  • Loading branch information
catgoose authored Dec 27, 2024
1 parent 078d5aa commit 8c6164f
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 71 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ library to do custom highlighting themselves.
-- Set to false|nil to disable
names_custom = false, -- Custom names to be highlighted: table|function|false|nil
RGB = true, -- #RGB hex codes
RGBA = true, -- #RGBA hex codes
RRGGBB = true, -- #RRGGBB hex codes
RRGGBBAA = false, -- #RRGGBBAA hex codes
AARRGGBB = false, -- 0xAARRGGBB hex codes
Expand Down
27 changes: 19 additions & 8 deletions doc/colorizer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ parse_lines({bufnr}, {lines}, {line_start}, {options})
Parameters: ~
{bufnr} - number: Buffer number (0 for current)
{lines} - table: Table of lines to parse
{line_start} - number: This is the buffer line number, from where to
start highlighting
{line_start} - number: Buffer line number to start highlighting
{options} - table: Passed in `colorizer.setup`, Only uses
`user_default_options`

Expand Down Expand Up @@ -788,7 +787,7 @@ parser({line}, {i}, {opts}) *colorizer.parser.rgba_hex.parser*
{opts} - table Options containing:
- `minlen` (number): Minimum length of the color string
- `maxlen` (number): Maximum length of the color string
- `valid_lengths` (table): Set of valid lengths (e.g., `{4, 7, 9}`)
- `valid_lengths` (table): Set of valid lengths (e.g., `{3, 4, 6, 8}`)

returns:~
number or nil The end index of the parsed hex color within the line, or
Expand Down Expand Up @@ -1056,6 +1055,8 @@ Provides utility functions for color handling and file operations.
LUA API *colorizer.utils-lua-api*

Functions: ~
|rgb_to_hex| - Returns HEX format from RGB values

|byte_is_alphanumeric| - Checks if a byte represents an alphanumeric
character.

Expand All @@ -1075,14 +1076,24 @@ Functions: ~
|bufme| - Validates and returns a buffer number.


rgb_to_hex({r}, {g}, {b}) *colorizer.utils.rgb_to_hex*
Returns HEX format from RGB values

Parameters: ~
{r} - number: Red value
{g} - number: Green value
{b} - number: Blue value



byte_is_alphanumeric({byte}) *colorizer.utils.byte_is_alphanumeric*
Checks if a byte represents an alphanumeric character.

Parameters: ~
{byte} - number The byte to check.

returns:~
boolean `true` if the byte is alphanumeric, otherwise `false`.
boolean: `true` if the byte is alphanumeric, otherwise `false`.



Expand All @@ -1093,7 +1104,7 @@ byte_is_hex({byte}) *colorizer.utils.byte_is_hex*
{byte} - number The byte to check.

returns:~
boolean `true` if the byte is hexadecimal, otherwise `false`.
boolean: `true` if the byte is hexadecimal, otherwise `false`.



Expand All @@ -1105,7 +1116,7 @@ byte_is_valid_colorchar({byte}) *colorizer.utils.byte_is_valid_colorchar*
{byte} - number The byte to check.

returns:~
boolean `true` if the byte is valid, otherwise `false`.
boolean: `true` if the byte is valid, otherwise `false`.



Expand Down Expand Up @@ -1139,7 +1150,7 @@ parse_hex({byte}) *colorizer.utils.parse_hex*
{byte} - number The byte to parse.

returns:~
number The parsed hexadecimal value of the byte.
number: The parsed hexadecimal value of the byte.



Expand All @@ -1166,7 +1177,7 @@ bufme({bufnr}) *colorizer.utils.bufme*
{bufnr} - number|nil: The buffer number to validate.

returns:~
number The validated buffer number.
number: The validated buffer number.



Expand Down
2 changes: 1 addition & 1 deletion doc/modules/colorizer.buffer.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ <h3>Parameters:</h3>
table: Table of lines to parse
</li>
<li><span class="parameter">line_start</span>
number: This is the buffer line number, from where to start highlighting
number: Buffer line number to start highlighting
</li>
<li><span class="parameter">options</span>
table: Passed in `colorizer.setup`, Only uses `user_default_options`
Expand Down
2 changes: 1 addition & 1 deletion doc/modules/colorizer.parser.rgba_hex.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ <h3>Parameters:</h3>
table Options containing:
- `minlen` (number): Minimum length of the color string
- `maxlen` (number): Maximum length of the color string
- `valid_lengths` (table): Set of valid lengths (e.g., `{4, 7, 9}`)
- `valid_lengths` (table): Set of valid lengths (e.g., `{3, 4, 6, 8}`)
</li>
</ul>

Expand Down
40 changes: 35 additions & 5 deletions doc/modules/colorizer.utils.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ <h1>Module <code>colorizer.utils</code></h1>

<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#rgb_to_hex">rgb_to_hex (r, g, b)</a></td>
<td class="summary">Returns HEX format from RGB values</td>
</tr>
<tr>
<td class="name" nowrap><a href="#byte_is_alphanumeric">byte_is_alphanumeric (byte)</a></td>
<td class="summary">Checks if a byte represents an alphanumeric character.</td>
Expand Down Expand Up @@ -110,6 +114,32 @@ <h2><a href="#Functions">Functions</a></h2>
<h2 class="section-header "><a name="Functions"></a>Functions</h2>

<dl class="function">
<dt>
<a name = "rgb_to_hex"></a>
<strong>rgb_to_hex (r, g, b)</strong>
</dt>
<dd>
Returns HEX format from RGB values


<h3>Parameters:</h3>
<ul>
<li><span class="parameter">r</span>
number: Red value
</li>
<li><span class="parameter">g</span>
number: Green value
</li>
<li><span class="parameter">b</span>
number: Blue value
</li>
</ul>





</dd>
<dt>
<a name = "byte_is_alphanumeric"></a>
<strong>byte_is_alphanumeric (byte)</strong>
Expand All @@ -128,7 +158,7 @@ <h3>Parameters:</h3>
<h3>Returns:</h3>
<ol>

boolean `true` if the byte is alphanumeric, otherwise `false`.
boolean: `true` if the byte is alphanumeric, otherwise `false`.
</ol>


Expand All @@ -153,7 +183,7 @@ <h3>Parameters:</h3>
<h3>Returns:</h3>
<ol>

boolean `true` if the byte is hexadecimal, otherwise `false`.
boolean: `true` if the byte is hexadecimal, otherwise `false`.
</ol>


Expand All @@ -178,7 +208,7 @@ <h3>Parameters:</h3>
<h3>Returns:</h3>
<ol>

boolean `true` if the byte is valid, otherwise `false`.
boolean: `true` if the byte is valid, otherwise `false`.
</ol>


Expand Down Expand Up @@ -256,7 +286,7 @@ <h3>Parameters:</h3>
<h3>Returns:</h3>
<ol>

number The parsed hexadecimal value of the byte.
number: The parsed hexadecimal value of the byte.
</ol>


Expand Down Expand Up @@ -313,7 +343,7 @@ <h3>Parameters:</h3>
<h3>Returns:</h3>
<ol>

number The validated buffer number.
number: The validated buffer number.
</ol>


Expand Down
13 changes: 5 additions & 8 deletions lua/colorizer/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ end
-- rgb_hex and range per line
---@param bufnr number: Buffer number (0 for current)
---@param lines table: Table of lines to parse
---@param line_start number: This is the buffer line number, from where to start highlighting
---@param line_start number: Buffer line number to start highlighting
---@param options table: Passed in `colorizer.setup`, Only uses `user_default_options`
---@return table|nil
function M.parse_lines(bufnr, lines, line_start, options)
Expand All @@ -177,18 +177,15 @@ function M.parse_lines(bufnr, lines, line_start, options)
end

local data = {}
for current_linenum, line in ipairs(lines) do
current_linenum = current_linenum - 1 + line_start
data[current_linenum] = data[current_linenum] or {}
for line_nr, line in ipairs(lines) do
line_nr = line_nr - 1 + line_start
data[line_nr] = data[line_nr] or {}

local i = 1
while i < #line do
local length, rgb_hex = loop_parse_fn(line, i, bufnr)
if length and rgb_hex then
table.insert(
data[current_linenum],
{ rgb_hex = rgb_hex, range = { i - 1, i + length - 1 } }
)
table.insert(data[line_nr], { rgb_hex = rgb_hex, range = { i - 1, i + length - 1 } })
i = i + length
else
i = i + 1
Expand Down
4 changes: 3 additions & 1 deletion lua/colorizer/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local plugin_user_default_options = {
names = true,
names_custom = false,
RGB = true,
RGBA = true,
RRGGBB = true,
RRGGBBAA = false,
AARRGGBB = false,
Expand Down Expand Up @@ -95,7 +96,8 @@ end
---@return table
function M.apply_alias_options(options)
local aliases = {
["css"] = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
-- TODO: 2024-12-24 - Should aliases be configurable?
["css"] = { "names", "RGB", "RGBA", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
["css_fn"] = { "hsl_fn", "rgb_fn" },
}
local function handle_alias(name, opts)
Expand Down
32 changes: 18 additions & 14 deletions lua/colorizer/matcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function M.make(options)
local enable_sass = options.sass and options.sass.enable
local enable_tailwind = options.tailwind
local enable_RGB = options.RGB
local enable_RGBA = options.RGBA
local enable_RRGGBB = options.RRGGBB
local enable_RRGGBBAA = options.RRGGBBAA
local enable_AARRGGBB = options.AARRGGBB
Expand All @@ -95,15 +96,16 @@ function M.make(options)
+ (enable_names and 1 or 0)
+ (enable_names_custom and 2 or 0)
+ (enable_RGB and 4 or 0)
+ (enable_RRGGBB and 8 or 0)
+ (enable_RRGGBBAA and 16 or 0)
+ (enable_AARRGGBB and 32 or 0)
+ (enable_rgb and 64 or 0)
+ (enable_hsl and 128 or 0)
+ ((enable_tailwind == true or enable_tailwind == "normal") and 256 or 0)
+ (enable_tailwind == "lsp" and 512 or 0)
+ (enable_tailwind == "both" and 1024 or 0)
+ (enable_sass and 2048 or 0)
+ (enable_RGBA and 8 or 0)
+ (enable_RRGGBB and 16 or 0)
+ (enable_RRGGBBAA and 32 or 0)
+ (enable_AARRGGBB and 64 or 0)
+ (enable_rgb and 128 or 0)
+ (enable_hsl and 256 or 0)
+ ((enable_tailwind == true or enable_tailwind == "normal") and 512 or 0)
+ (enable_tailwind == "lsp" and 1024 or 0)
+ (enable_tailwind == "both" and 2048 or 0)
+ (enable_sass and 4096 or 0)

if matcher_key == 0 then
return false
Expand Down Expand Up @@ -133,7 +135,8 @@ function M.make(options)
matchers.sass_name_parser = true
end

local valid_lengths = { [3] = enable_RGB, [6] = enable_RRGGBB, [8] = enable_RRGGBBAA }
local valid_lengths =
{ [3] = enable_RGB, [4] = enable_RGBA, [6] = enable_RRGGBB, [8] = enable_RRGGBBAA }
local minlen, maxlen
for k, v in pairs(valid_lengths) do
if v then
Expand All @@ -143,10 +146,11 @@ function M.make(options)
end

if minlen then
matchers.rgba_hex_parser = {}
matchers.rgba_hex_parser.valid_lengths = valid_lengths
matchers.rgba_hex_parser.maxlen = maxlen
matchers.rgba_hex_parser.minlen = minlen
matchers.rgba_hex_parser = {
valid_lengths = valid_lengths,
minlen = minlen,
maxlen = maxlen,
}
end

-- TODO: 2024-11-05 - Add custom prefixes
Expand Down
2 changes: 1 addition & 1 deletion lua/colorizer/parser/argb_hex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function M.parser(line, i)
return
end

local rgb_hex = string.format("%02x%02x%02x", r, g, b)
local rgb_hex = utils.rgb_to_hex(r, g, b)
return length, rgb_hex
end

Expand Down
4 changes: 3 additions & 1 deletion lua/colorizer/parser/hsl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local M = {}
local count = require("colorizer.utils").count
local floor = math.floor
local hsl_to_rgb = require("colorizer.color").hsl_to_rgb
local utils = require("colorizer.utils")

--- Parses `hsl()` and `hsla()` CSS functions and converts them to RGB hexadecimal format.
-- This function matches `hsl()` or `hsla()` functions within a line of text, extracting and converting the hue, saturation, and luminance
Expand Down Expand Up @@ -106,7 +107,8 @@ function M.parser(line, i, opts)
if not r or not g or not b then
return
end
local rgb_hex = string.format("%02x%02x%02x", r * a, g * a, b * a)

local rgb_hex = utils.rgb_to_hex(r, g, b)
return match_end - 1, rgb_hex
end

Expand Down
10 changes: 5 additions & 5 deletions lua/colorizer/parser/rgb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

local M = {}

local count = require("colorizer.utils").count
local utils = require("colorizer.utils")

--- Parses `rgb()` and `rgba()` CSS functions and converts them to RGB hexadecimal format.
-- This function matches `rgb()` or `rgba()` functions in a line of text, extracting RGB and optional alpha values.
Expand Down Expand Up @@ -45,7 +45,7 @@ function M.parser(line, i, opts)

local units = ("%s%s%s"):format(unit1, unit2, unit3)
if units:match("%%") then
if not ((count(units, "%%")) == min_percent) then
if not ((utils.count(units, "%%")) == min_percent) then
return
end
end
Expand All @@ -54,11 +54,11 @@ function M.parser(line, i, opts)
local s_seps = ("%s%s"):format(ssep1, ssep2)
-- Comma separator syntax
if c_seps:match(",") then
if not (count(c_seps, ",") == min_commas) then
if not (utils.count(c_seps, ",") == min_commas) then
return
end
-- Space separator syntax with decimal or percentage alpha
elseif count(s_seps, "%s") >= min_spaces then
elseif utils.count(s_seps, "%s") >= min_spaces then
if a then
if not (c_seps == "/") then
return
Expand Down Expand Up @@ -108,7 +108,7 @@ function M.parser(line, i, opts)
end

-- Convert to hex, applying alpha to each channel
local rgb_hex = string.format("%02x%02x%02x", r * a, g * a, b * a)
local rgb_hex = utils.rgb_to_hex(r, g, b)
return match_end - 1, rgb_hex
end

Expand Down
Loading

0 comments on commit 8c6164f

Please sign in to comment.