Skip to content

Commit 8c6164f

Browse files
authored
feat: #rgba support (#120)
* 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
1 parent 078d5aa commit 8c6164f

14 files changed

+148
-71
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ library to do custom highlighting themselves.
129129
-- Set to false|nil to disable
130130
names_custom = false, -- Custom names to be highlighted: table|function|false|nil
131131
RGB = true, -- #RGB hex codes
132+
RGBA = true, -- #RGBA hex codes
132133
RRGGBB = true, -- #RRGGBB hex codes
133134
RRGGBBAA = false, -- #RRGGBBAA hex codes
134135
AARRGGBB = false, -- 0xAARRGGBB hex codes

doc/colorizer.txt

+19-8
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ parse_lines({bufnr}, {lines}, {line_start}, {options})
318318
Parameters: ~
319319
{bufnr} - number: Buffer number (0 for current)
320320
{lines} - table: Table of lines to parse
321-
{line_start} - number: This is the buffer line number, from where to
322-
start highlighting
321+
{line_start} - number: Buffer line number to start highlighting
323322
{options} - table: Passed in `colorizer.setup`, Only uses
324323
`user_default_options`
325324

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

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

10581057
Functions: ~
1058+
|rgb_to_hex| - Returns HEX format from RGB values
1059+
10591060
|byte_is_alphanumeric| - Checks if a byte represents an alphanumeric
10601061
character.
10611062

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

10771078

1079+
rgb_to_hex({r}, {g}, {b}) *colorizer.utils.rgb_to_hex*
1080+
Returns HEX format from RGB values
1081+
1082+
Parameters: ~
1083+
{r} - number: Red value
1084+
{g} - number: Green value
1085+
{b} - number: Blue value
1086+
1087+
1088+
10781089
byte_is_alphanumeric({byte}) *colorizer.utils.byte_is_alphanumeric*
10791090
Checks if a byte represents an alphanumeric character.
10801091

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

10841095
returns:~
1085-
boolean `true` if the byte is alphanumeric, otherwise `false`.
1096+
boolean: `true` if the byte is alphanumeric, otherwise `false`.
10861097

10871098

10881099

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

10951106
returns:~
1096-
boolean `true` if the byte is hexadecimal, otherwise `false`.
1107+
boolean: `true` if the byte is hexadecimal, otherwise `false`.
10971108

10981109

10991110

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

11071118
returns:~
1108-
boolean `true` if the byte is valid, otherwise `false`.
1119+
boolean: `true` if the byte is valid, otherwise `false`.
11091120

11101121

11111122

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

11411152
returns:~
1142-
number The parsed hexadecimal value of the byte.
1153+
number: The parsed hexadecimal value of the byte.
11431154

11441155

11451156

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

11681179
returns:~
1169-
number The validated buffer number.
1180+
number: The validated buffer number.
11701181

11711182

11721183

doc/modules/colorizer.buffer.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ <h3>Parameters:</h3>
218218
table: Table of lines to parse
219219
</li>
220220
<li><span class="parameter">line_start</span>
221-
number: This is the buffer line number, from where to start highlighting
221+
number: Buffer line number to start highlighting
222222
</li>
223223
<li><span class="parameter">options</span>
224224
table: Passed in `colorizer.setup`, Only uses `user_default_options`

doc/modules/colorizer.parser.rgba_hex.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ <h3>Parameters:</h3>
104104
table Options containing:
105105
- `minlen` (number): Minimum length of the color string
106106
- `maxlen` (number): Maximum length of the color string
107-
- `valid_lengths` (table): Set of valid lengths (e.g., `{4, 7, 9}`)
107+
- `valid_lengths` (table): Set of valid lengths (e.g., `{3, 4, 6, 8}`)
108108
</li>
109109
</ul>
110110

doc/modules/colorizer.utils.html

+35-5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ <h1>Module <code>colorizer.utils</code></h1>
6969

7070
<h2><a href="#Functions">Functions</a></h2>
7171
<table class="function_list">
72+
<tr>
73+
<td class="name" nowrap><a href="#rgb_to_hex">rgb_to_hex (r, g, b)</a></td>
74+
<td class="summary">Returns HEX format from RGB values</td>
75+
</tr>
7276
<tr>
7377
<td class="name" nowrap><a href="#byte_is_alphanumeric">byte_is_alphanumeric (byte)</a></td>
7478
<td class="summary">Checks if a byte represents an alphanumeric character.</td>
@@ -110,6 +114,32 @@ <h2><a href="#Functions">Functions</a></h2>
110114
<h2 class="section-header "><a name="Functions"></a>Functions</h2>
111115

112116
<dl class="function">
117+
<dt>
118+
<a name = "rgb_to_hex"></a>
119+
<strong>rgb_to_hex (r, g, b)</strong>
120+
</dt>
121+
<dd>
122+
Returns HEX format from RGB values
123+
124+
125+
<h3>Parameters:</h3>
126+
<ul>
127+
<li><span class="parameter">r</span>
128+
number: Red value
129+
</li>
130+
<li><span class="parameter">g</span>
131+
number: Green value
132+
</li>
133+
<li><span class="parameter">b</span>
134+
number: Blue value
135+
</li>
136+
</ul>
137+
138+
139+
140+
141+
142+
</dd>
113143
<dt>
114144
<a name = "byte_is_alphanumeric"></a>
115145
<strong>byte_is_alphanumeric (byte)</strong>
@@ -128,7 +158,7 @@ <h3>Parameters:</h3>
128158
<h3>Returns:</h3>
129159
<ol>
130160

131-
boolean `true` if the byte is alphanumeric, otherwise `false`.
161+
boolean: `true` if the byte is alphanumeric, otherwise `false`.
132162
</ol>
133163

134164

@@ -153,7 +183,7 @@ <h3>Parameters:</h3>
153183
<h3>Returns:</h3>
154184
<ol>
155185

156-
boolean `true` if the byte is hexadecimal, otherwise `false`.
186+
boolean: `true` if the byte is hexadecimal, otherwise `false`.
157187
</ol>
158188

159189

@@ -178,7 +208,7 @@ <h3>Parameters:</h3>
178208
<h3>Returns:</h3>
179209
<ol>
180210

181-
boolean `true` if the byte is valid, otherwise `false`.
211+
boolean: `true` if the byte is valid, otherwise `false`.
182212
</ol>
183213

184214

@@ -256,7 +286,7 @@ <h3>Parameters:</h3>
256286
<h3>Returns:</h3>
257287
<ol>
258288

259-
number The parsed hexadecimal value of the byte.
289+
number: The parsed hexadecimal value of the byte.
260290
</ol>
261291

262292

@@ -313,7 +343,7 @@ <h3>Parameters:</h3>
313343
<h3>Returns:</h3>
314344
<ol>
315345

316-
number The validated buffer number.
346+
number: The validated buffer number.
317347
</ol>
318348

319349

lua/colorizer/buffer.lua

+5-8
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ end
167167
-- rgb_hex and range per line
168168
---@param bufnr number: Buffer number (0 for current)
169169
---@param lines table: Table of lines to parse
170-
---@param line_start number: This is the buffer line number, from where to start highlighting
170+
---@param line_start number: Buffer line number to start highlighting
171171
---@param options table: Passed in `colorizer.setup`, Only uses `user_default_options`
172172
---@return table|nil
173173
function M.parse_lines(bufnr, lines, line_start, options)
@@ -177,18 +177,15 @@ function M.parse_lines(bufnr, lines, line_start, options)
177177
end
178178

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

184184
local i = 1
185185
while i < #line do
186186
local length, rgb_hex = loop_parse_fn(line, i, bufnr)
187187
if length and rgb_hex then
188-
table.insert(
189-
data[current_linenum],
190-
{ rgb_hex = rgb_hex, range = { i - 1, i + length - 1 } }
191-
)
188+
table.insert(data[line_nr], { rgb_hex = rgb_hex, range = { i - 1, i + length - 1 } })
192189
i = i + length
193190
else
194191
i = i + 1

lua/colorizer/config.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local plugin_user_default_options = {
77
names = true,
88
names_custom = false,
99
RGB = true,
10+
RGBA = true,
1011
RRGGBB = true,
1112
RRGGBBAA = false,
1213
AARRGGBB = false,
@@ -95,7 +96,8 @@ end
9596
---@return table
9697
function M.apply_alias_options(options)
9798
local aliases = {
98-
["css"] = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
99+
-- TODO: 2024-12-24 - Should aliases be configurable?
100+
["css"] = { "names", "RGB", "RGBA", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
99101
["css_fn"] = { "hsl_fn", "rgb_fn" },
100102
}
101103
local function handle_alias(name, opts)

lua/colorizer/matcher.lua

+18-14
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function M.make(options)
8383
local enable_sass = options.sass and options.sass.enable
8484
local enable_tailwind = options.tailwind
8585
local enable_RGB = options.RGB
86+
local enable_RGBA = options.RGBA
8687
local enable_RRGGBB = options.RRGGBB
8788
local enable_RRGGBBAA = options.RRGGBBAA
8889
local enable_AARRGGBB = options.AARRGGBB
@@ -95,15 +96,16 @@ function M.make(options)
9596
+ (enable_names and 1 or 0)
9697
+ (enable_names_custom and 2 or 0)
9798
+ (enable_RGB and 4 or 0)
98-
+ (enable_RRGGBB and 8 or 0)
99-
+ (enable_RRGGBBAA and 16 or 0)
100-
+ (enable_AARRGGBB and 32 or 0)
101-
+ (enable_rgb and 64 or 0)
102-
+ (enable_hsl and 128 or 0)
103-
+ ((enable_tailwind == true or enable_tailwind == "normal") and 256 or 0)
104-
+ (enable_tailwind == "lsp" and 512 or 0)
105-
+ (enable_tailwind == "both" and 1024 or 0)
106-
+ (enable_sass and 2048 or 0)
99+
+ (enable_RGBA and 8 or 0)
100+
+ (enable_RRGGBB and 16 or 0)
101+
+ (enable_RRGGBBAA and 32 or 0)
102+
+ (enable_AARRGGBB and 64 or 0)
103+
+ (enable_rgb and 128 or 0)
104+
+ (enable_hsl and 256 or 0)
105+
+ ((enable_tailwind == true or enable_tailwind == "normal") and 512 or 0)
106+
+ (enable_tailwind == "lsp" and 1024 or 0)
107+
+ (enable_tailwind == "both" and 2048 or 0)
108+
+ (enable_sass and 4096 or 0)
107109

108110
if matcher_key == 0 then
109111
return false
@@ -133,7 +135,8 @@ function M.make(options)
133135
matchers.sass_name_parser = true
134136
end
135137

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

145148
if minlen then
146-
matchers.rgba_hex_parser = {}
147-
matchers.rgba_hex_parser.valid_lengths = valid_lengths
148-
matchers.rgba_hex_parser.maxlen = maxlen
149-
matchers.rgba_hex_parser.minlen = minlen
149+
matchers.rgba_hex_parser = {
150+
valid_lengths = valid_lengths,
151+
minlen = minlen,
152+
maxlen = maxlen,
153+
}
150154
end
151155

152156
-- TODO: 2024-11-05 - Add custom prefixes

lua/colorizer/parser/argb_hex.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function M.parser(line, i)
7171
return
7272
end
7373

74-
local rgb_hex = string.format("%02x%02x%02x", r, g, b)
74+
local rgb_hex = utils.rgb_to_hex(r, g, b)
7575
return length, rgb_hex
7676
end
7777

lua/colorizer/parser/hsl.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local M = {}
88
local count = require("colorizer.utils").count
99
local floor = math.floor
1010
local hsl_to_rgb = require("colorizer.color").hsl_to_rgb
11+
local utils = require("colorizer.utils")
1112

1213
--- Parses `hsl()` and `hsla()` CSS functions and converts them to RGB hexadecimal format.
1314
-- This function matches `hsl()` or `hsla()` functions within a line of text, extracting and converting the hue, saturation, and luminance
@@ -106,7 +107,8 @@ function M.parser(line, i, opts)
106107
if not r or not g or not b then
107108
return
108109
end
109-
local rgb_hex = string.format("%02x%02x%02x", r * a, g * a, b * a)
110+
111+
local rgb_hex = utils.rgb_to_hex(r, g, b)
110112
return match_end - 1, rgb_hex
111113
end
112114

lua/colorizer/parser/rgb.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
local M = {}
77

8-
local count = require("colorizer.utils").count
8+
local utils = require("colorizer.utils")
99

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

4646
local units = ("%s%s%s"):format(unit1, unit2, unit3)
4747
if units:match("%%") then
48-
if not ((count(units, "%%")) == min_percent) then
48+
if not ((utils.count(units, "%%")) == min_percent) then
4949
return
5050
end
5151
end
@@ -54,11 +54,11 @@ function M.parser(line, i, opts)
5454
local s_seps = ("%s%s"):format(ssep1, ssep2)
5555
-- Comma separator syntax
5656
if c_seps:match(",") then
57-
if not (count(c_seps, ",") == min_commas) then
57+
if not (utils.count(c_seps, ",") == min_commas) then
5858
return
5959
end
6060
-- Space separator syntax with decimal or percentage alpha
61-
elseif count(s_seps, "%s") >= min_spaces then
61+
elseif utils.count(s_seps, "%s") >= min_spaces then
6262
if a then
6363
if not (c_seps == "/") then
6464
return
@@ -108,7 +108,7 @@ function M.parser(line, i, opts)
108108
end
109109

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

0 commit comments

Comments
 (0)