check hex

This commit is contained in:
Mylloon 2024-10-06 20:54:57 +02:00
parent 43f6ef96a1
commit a1f7c195a8
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -18,23 +18,24 @@ function latex-color --description "Get the LaTeX color from HEX code"
set hex (string sub -s 2 $hex)
end
# Check if the length of the hex is valid
if not contains $(string length $hex) $(string split " " "3 4 6 8")
echo "Error: Invalid HEX code"
# Check if the hex code is valid
if not string match -qir '^[0-9A-F]{3,8}$' $hex
and not contains $(string length $hex) $(string split " " "3 4 6 8")
echo "Error: Invalid HEX code" 1>&2
return 1
end
# Expand 3 hex to 4 hex
# Expand 3 to 4
if test (string length $hex) -eq 3
set hex $(string join "" $hex 0)
end
# Expand 4 hex to 8 hex
# Expand 4 to 8
if test (string length $hex) -eq 4
set hex "$(string replace -ar '(.)' '$1$1' $hex)00"
end
# Expand 6 hex to 8 hex
# Expand 6 to 8
if test (string length $hex) -eq 6
set hex $(string join "" $hex 00)
end