aboutsummaryrefslogtreecommitdiff
path: root/lua/colorizer/color.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/colorizer/color.lua')
-rw-r--r--lua/colorizer/color.lua126
1 files changed, 80 insertions, 46 deletions
diff --git a/lua/colorizer/color.lua b/lua/colorizer/color.lua
index fe3e15b..370131b 100644
--- a/lua/colorizer/color.lua
+++ b/lua/colorizer/color.lua
@@ -14,7 +14,6 @@ local byte_is_hex = utils.byte_is_hex
local byte_is_valid_colorchar = utils.byte_is_valid_colorchar
local count = utils.count
local parse_hex = utils.parse_hex
-local percent_or_hex = utils.percent_or_hex
local color = {}
@@ -298,76 +297,111 @@ function color.name_parser(line, i, opts)
end
end
+local CSS_RGBA_FN_MINIMUM_LENGTH = #"rgba(0,0,0)" - 1
local CSS_RGB_FN_MINIMUM_LENGTH = #"rgb(0,0,0)" - 1
----Parse for rgb() css function and return rgb hex.
+---Parse for rgb() rgba() css function and return rgb hex.
+-- For more info: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb
---@param line string: Line to parse
---@param i number: Index of line from where to start parsing
----@return number|nil: Index of line where the rgb function ended
+---@param opts table: Values passed from matchers like prefix
+---@return number|nil: Index of line where the rgb/rgba function ended
---@return string|nil: rgb hex value
-function color.rgb_function_parser(line, i)
- if #line < i + CSS_RGB_FN_MINIMUM_LENGTH then
- return
- end
- local r, g, b, match_end = line:sub(i):match "^rgb%(%s*(%d+%%?)%s*,%s*(%d+%%?)%s*,%s*(%d+%%?)%s*%)()"
- if not match_end then
- r, g, b, match_end = line:sub(i):match "^rgb%(%s*(%d+%%?)%s+(%d+%%?)%s+(%d+%%?)%s*%)()"
- if not match_end then
- return
- end
- end
- r = percent_or_hex(r)
- if not r then
- return
+function color.rgb_function_parser(line, i, opts)
+ local min_len = CSS_RGBA_FN_MINIMUM_LENGTH
+ local min_commas, min_spaces, min_percent = 2, 2, 3
+ local pattern = "^"
+ .. opts.prefix
+ .. "%(%s*([.%d]+)([%%]?)(%s?)%s*(,?)%s*([.%d]+)([%%]?)(%s?)%s*(,?)%s*([.%d]+)([%%]?)%s*(/?,?)%s*([.%d]*)([%%]?)%s*%)()"
+
+ if opts.prefix == "rgb" then
+ min_len = CSS_RGB_FN_MINIMUM_LENGTH
end
- g = percent_or_hex(g)
- if not g then
+
+ if #line < i + min_len then
return
end
- b = percent_or_hex(b)
- if not b then
+
+ local r, unit1, ssep1, csep1, g, unit2, ssep2, csep2, b, unit3, sep3, a, unit_a, match_end =
+ line:sub(i):match(pattern)
+ if not match_end then
return
end
- local rgb_hex = string.format("%02x%02x%02x", r, g, b)
- return match_end - 1, rgb_hex
-end
-local CSS_RGBA_FN_MINIMUM_LENGTH = #"rgba(0,0,0,0)" - 1
----Parse for rgba() css function and return rgb hex.
--- Todo consider removing the regexes here
--- Todo this might not be the best approach to alpha channel.
--- Things like pumblend might be useful here.
----@param line string: Line to parse
----@param i number: Index of line from where to start parsing
----@return number|nil: Index of line where the rgba function ended
----@return string|nil: rgb hex value
-function color.rgba_function_parser(line, i)
- if #line < i + CSS_RGBA_FN_MINIMUM_LENGTH then
- return
+ if a == "" then
+ a = nil
+ else
+ min_commas = min_commas + 1
end
- local r, g, b, a, match_end =
- line:sub(i):match "^rgba%(%s*(%d+%%?)%s*,%s*(%d+%%?)%s*,%s*(%d+%%?)%s*,%s*([.%d]+)%s*%)()"
- if not match_end then
- r, g, b, a, match_end = line:sub(i):match "^rgba%(%s*(%d+%%?)%s+(%d+%%?)%s+(%d+%%?)%s+([.%d]+)%s*%)()"
- if not match_end then
+
+ local units = ("%s%s%s"):format(unit1, unit2, unit3)
+ if units:match "%%" then
+ if not ((count(units, "%%")) == min_percent) then
return
end
end
- a = tonumber(a)
- if not a or a > 1 then
+
+ local c_seps = ("%s%s%s"):format(csep1, csep2, sep3)
+ local s_seps = ("%s%s"):format(ssep1, ssep2)
+ -- comma separator syntax
+ if c_seps:match "," then
+ if not (count(c_seps, ",") == min_commas) then
+ return
+ end
+ -- space separator syntax with decimal or percentage alpha
+ elseif count(s_seps, "%s") >= min_spaces then
+ if a then
+ if not (c_seps == "/") then
+ return
+ end
+ end
+ else
return
end
- r = percent_or_hex(r)
+
+ if not a then
+ a = 1
+ else
+ a = tonumber(a)
+ -- if percentage, then convert to decimal
+ if unit_a == "%" then
+ a = a / 100
+ end
+ -- although alpha doesn't support larger values than 1, css anyways renders it at 1
+ if a > 1 then
+ a = 1
+ end
+ end
+
+ r = tonumber(r)
if not r then
return
end
- g = percent_or_hex(g)
+ g = tonumber(g)
if not g then
return
end
- b = percent_or_hex(b)
+ b = tonumber(b)
if not b then
return
end
+
+ if unit1 == "%" then
+ r = r / 100 * 255
+ g = g / 100 * 255
+ b = b / 100 * 255
+ else
+ -- although r,g,b doesn't support larger values than 255, css anyways renders it at 255
+ if r > 255 then
+ r = 255
+ end
+ if g > 255 then
+ g = 255
+ end
+ if b > 255 then
+ b = 255
+ end
+ end
+
local rgb_hex = string.format("%02x%02x%02x", r * a, g * a, b * a)
return match_end - 1, rgb_hex
end