diff --git a/.config/fish/functions/latex-color.fish b/.config/fish/functions/latex-color.fish index 4a5a885..3e31259 100644 --- a/.config/fish/functions/latex-color.fish +++ b/.config/fish/functions/latex-color.fish @@ -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