Skip to content

Commit

Permalink
fix: rgb parser 255 clamp (norcalli#102)
Browse files Browse the repository at this point in the history
* feat(test): minimal.lua adds autocmd for expect.txt to dynamically set colorizer buffer opts from returned table

* fix: setting rgb percentages greater than 100 will no longer create invalid rgb hex

* doc: updates readme
  • Loading branch information
catgoose authored Nov 21, 2024
1 parent 2f546ad commit 486212f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 86 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ or use `:h colorizer` once installed.
## Testing

To test colorization with your config, edit `test/expect.txt` to see expected
highlights
highlights. The returned table from `text/expect.txt` can be edited and when
saved will reattach the Colorizer with those settings.

For troubleshooting use `test/minimal.lua`. Edit the file to configure colorizer.
Run `minimal.lua` to startup neovim:
Expand Down
21 changes: 7 additions & 14 deletions lua/colorizer/parser/rgb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,15 @@ function M.rgb_function_parser(line, i, opts)
return
end

-- Convert percentages to 0-255 scale if applicable
-- clamp values to 0-255
if unit1 == "%" then
r = r / 100 * 255
g = g / 100 * 255
b = b / 100 * 255
r = r > 100 and 255 or r / 100 * 255
g = g > 100 and 255 or g / 100 * 255
b = b > 100 and 255 or b / 100 * 255
else
-- Clamp RGB values to a maximum of 255
if r > 255 then
r = 255
end
if g > 255 then
g = 255
end
if b > 255 then
b = 255
end
r = r > 255 and 255 or r
b = b > 255 and 255 or b
g = g > 255 and 255 or g
end

-- Convert to hex, applying alpha to each channel
Expand Down
106 changes: 94 additions & 12 deletions test/expect.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-- vim:ft=lua

-- Configuration options
return {
RGB = true,
RRGGBB = true,
Expand All @@ -18,42 +20,100 @@ return {
virtualtext = "",
virtualtext_inline = false,
virtualtext_mode = "foreground",
always_update = false,
always_update = true,
}
--[[ SUCCESS

--[[ TEST CASES
0xFFFFFFF1 -- why does this highlight?
SUCCESS CASES:
Hexadecimal:
#RGB:
#F0F
#FFF #FFA #F0F #0FF #FF0
#RRGGBB:
#FFFF00
#FFFFFF #FFAA00 #FF00FF #00FFFF #FFFF99
#RRGGBBAA:
#FFFFFFCC
#FFFFAA99 #FF77FF99 #00FFFF88
0xRGB:
0xF0F
0xFFF 0xFFA 0xF0F 0x0FF 0xFF0
0xRRGGBB:
0xFFFF00
0xFFFFFF 0xFFAA00 0xFF00FF 0x00FFFF 0xFFFF99
0xRRGGBBAA:
0xFFFFFFCC
0xFFFFAA99 0xFF77FF99 0xFF3F3F88
0xFf32A14B 0xFf32A14B
0x1B29FB 0x1B29FB
0xF0F 0xF0F
0xA3B67CDE 0x7F12D9A5 0x7E43F2 0x34E8D3 0xB3A 0x1CD
#32a14b
#F0F #FF00FF #FFF00F8F #F0F #FF00FF
#FF32A14B
#FFF00F8F
#F0F #F00
#FF00FF #F00
#FFF00F8F #F00
#def
#deadbeef
CSS Named Colors:
olive -- do not remove
cyan magenta gold chartreuse lightgreen pink violet orange
lightcoral lightcyan lemonchiffon papayawhip peachpuff
blue gray lightblue gray100 white gold blue
Blue LightBlue Gray100 White
White
#def #deadbeef
rgb( 31 12.90 50 /0.5) rgb( 10, 100 , 100, 0.3)
RGB (standard and percentages):
rgb( 201 82.90 50 /0.5) rgb( 109, 100 , 100, 0.8)
rgb(30% 20% 50%) rgb(0,0,0) rgb(255 122 127 / 80%)
rgb(255 122 127 / .2) rgba(200,30,0,1) rgba(200,30,0,0.5)
rgb(255 122 127 / .7) rgba(200,30,0,1) rgba(200,30,0,0.5)
rgb(255, 200, 80)
rgb(255, 255, 255) rgb(255, 240, 200) rgb(240, 180, 120) rgb(80%, 60%, 40%)
rgb(255, 180, 180) rgb(255, 220, 120) rgb(255, 255, 100, 0.8)
rgb(255, 255, 255, 255)
rgb(255000, 255000, 255000, 255000)
rgb(100%, 100%, 100%)
rgb(100000%, 100000%, 100000%)
RGBA:
rgba(255, 240, 200, 0.5)
rgba(255, 255, 255, 1) rgba(255, 220, 180, 0.8) rgba(255, 200, 120, 0.4)
rgba(240, 180, 120, 0.6) rgba(255, 200, 80, 0.9) rgba(255, 180, 100, 0.7)
rgba(255, 255, 255, 1)
rgba(255000, 255000, 255000, 1000)
HSL:
hsl(300 50% 50%) hsl(300 50% 50% / 1) hsl(100 80% 50% / 0.4)
hsl(990 80% 50% / 0.4) hsl(720 80% 50% / 0.4)
hsl(1turn 80% 50% / 0.4) hsl(0.4turn 80% 50% / 0.4) hsl(1.4turn 80% 50% / 0.4)
hsl(60, 100%, 80%)
hsl(0, 100%, 90%) hsl(45, 100%, 70%) hsl(120, 100%, 85%) hsl(240, 100%, 85%)
hsl(300, 80%, 75%) hsl(180, 100%, 80%) hsl(210, 80%, 90%) hsl(90, 100%, 85%)
hsl(255, 100%, 100%)
hsl(10000, 10000%, 10000%)
HSLA:
hsla(300 50% 50%) hsla(300 50% 50% / 1)
hsla(300 50% 50% / 0.4) hsla(300,50%,50%,05)
hsla(360 , 50% , 50% , 1.0000000000000001)
]]
hsla(60, 100%, 85%, 0.5)
hsla(0, 100%, 90%, 1) hsla(120, 100%, 85%, 0.8) hsla(240, 100%, 85%, 0.7)
hsla(300, 80%, 75%, 0.6) hsla(180, 100%, 80%, 0.9) hsla(90, 100%, 85%, 0.4)
hsl(255, 100%, 100%, 1)
hsl(255000, 100000%, 100000%, 1000)
--[[ FAIL
FAIL CASES:
matcher#add
Invalid Hexadecimal:
#F #FF #FFF0F #GGGGGG #F0FFF0F #F0FFF0FFF
0xGHI 0x1234 0xFFFFF
#FG0 #ZZZZZZ #12345 #FFFFF0F 0xGGG 0x12345 0xFFFFFG
0xf32A14B 0xf32A14B
0xB29FB 0xB29FB
0x0F 0x0F
Expand All @@ -63,14 +123,36 @@ hsla(360 , 50% , 50% , 1.0000000000000001)
#F0FFF
#F0FFF0F
#F0FFF0FFF
Blueberry Gray1000 BlueGree BlueGray
#define
#def0
matcher#add
Invalid CSS Named Colors:
ceruleanblue goldenrodlight brightcyan darkmagentapurple
Blueberry Gray1000 BlueGree BlueGray
Invalid RGB:
rgb(10, 1 00, 100) rgb(255, 255, 255, -1) rgb(10,,100) rgb()
rgb(256, 100, 100 rgb(-10, 100, 100) rgb(100, 100)
rgb(100,,100) -- causes error
rgb (10,255,100)
rgb(10, 1 00 , 100)
Invalid RGBA:
rgba(10, 100) rgba(-10, 0, 255, 0.2)
rgba(100, 100, 100, -0.5)
rgba(100, 100) rgba(255, , 255, 0.5)
Invalid HSL:
hsl(300 50% 50 / 1) hsl(30, 50%, 20%,) hsl()
hsl(300, 50, 50) hsl(300,,50%) hsl(300, 50%,)
hsl(300 50% 50% 1)
hsl(300 50% 50 / 1)
Invalid HSLA:
hsla(120, 50%, 50, -0.1) hsla(300, 50) hsla(30, 100%, 50% 1) hsla()
hsla(300, 50%, 50%, -0.5)
hsla(300, 50, 50%, 0.5) hsla(300, 50%,) hsla(300, 50%, 50% 0.5)
hsla(, 50%, 50%, 0.5)
hsla(10 10% 10% 1)
hsla(300,50%,50,1.0000000000000001)
hsla(300,50,50,1.0000000000000001)
Expand Down
96 changes: 37 additions & 59 deletions test/minimal.lua
Original file line number Diff line number Diff line change
@@ -1,80 +1,58 @@
-- Run this file as `nvim --clean -u minimal.lua`

-- local local_plugin_dir = os.getenv("HOME") .. "/git/nvim-colorizer.lua"
-- if vim.fn.isdirectory(local_plugin_dir) == 1 then
-- vim.opt.runtimepath:append(local_plugin_dir)
-- else
-- vim.notify("Local plugin directory not found: " .. local_plugin_dir, vim.log.levels.ERROR)
-- end
local use_remote = true

for name, url in pairs({
colorizer = "https://github.com/NvChad/nvim-colorizer.lua",
}) do
local install_path = vim.fn.fnamemodify("colorizer_issue/" .. name, ":p")
if vim.fn.isdirectory(install_path) == 0 then
vim.fn.system({ "git", "clone", "--depth=1", url, install_path })
if use_remote then
for name, url in pairs({
colorizer = "https://github.com/NvChad/nvim-colorizer.lua",
}) do
local install_path = vim.fn.fnamemodify("colorizer_issue/" .. name, ":p")
if vim.fn.isdirectory(install_path) == 0 then
vim.fn.system({ "git", "clone", "--depth=1", url, install_path })
end
vim.opt.runtimepath:append(install_path)
end
vim.opt.runtimepath:append(install_path)
else
local local_plugin_dir = os.getenv("HOME") .. "/git/nvim-colorizer.lua"
if vim.fn.isdirectory(local_plugin_dir) == 1 then
vim.opt.runtimepath:append(local_plugin_dir)
else
vim.notify("Local plugin directory not found: " .. local_plugin_dir, vim.log.levels.ERROR)
end
end

local function get_opts(file_path)
local opts
local success, err = pcall(function()
opts = dofile(file_path)
end)
if not success then
vim.notify("Error reloading file: " .. err, vim.log.levels.ERROR)
return
end
if not opts or type(opts) ~= "table" then
vim.notify("Invalid options in " .. file_path, vim.log.levels.ERROR)
return
end
return opts
end

-- Configure setup opts
local setup_opts = {
user_default_options = {
RGB = true,
RRGGBB = true,
names = true,
RRGGBBAA = true,
AARRGGBB = true,
rgb_fn = true,
hsl_fn = true,
css = true,
css_fn = true,
mode = "background",
tailwind = "normal",
sass = {
enable = false,
parsers = { "css" },
},
virtualtext = "",
virtualtext_inline = false,
virtualtext_mode = "foreground",
always_update = false,
},
user_default_options = get_opts("expect.txt"),
buftypes = { "!prompt", "!popup" },
user_commands = true,
}
require("colorizer").setup(setup_opts)

vim.api.nvim_create_autocmd("BufWritePost", {
pattern = "expect.txt", -- Ensure this matches your file name
pattern = "expect.txt",
callback = function(evt)
vim.schedule(function()
local file_path = evt.match

if not file_path or file_path == "" then
vim.notify("Could not determine the file path", vim.log.levels.ERROR)
return
end

local opts
local success, err = pcall(function()
opts = dofile(file_path) -- Dynamically execute the saved file
end)

if not success then
vim.notify("Error reloading file: " .. err, vim.log.levels.ERROR)
return
end

if not opts or type(opts) ~= "table" then
vim.notify("Invalid options in " .. file_path, vim.log.levels.ERROR)
return
end

local opts = get_opts(evt.match)
require("colorizer").detach_from_buffer(evt.buf)
require("colorizer").attach_to_buffer(evt.buf, opts)

vim.notify("Colorizer reloaded with updated options from " .. file_path, vim.log.levels.INFO)
vim.notify("Colorizer reloaded with updated options from " .. evt.match, vim.log.levels.INFO)
end)
end,
})
Expand Down

0 comments on commit 486212f

Please sign in to comment.