fix latex-color

This commit is contained in:
Mylloon 2024-10-06 20:39:35 +02:00
parent 04bb7b1c37
commit a3ed69f858
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -1,4 +1,4 @@
function latexcolor --description "Get the LaTeX color from HEX code" function latex-color --description "Get the LaTeX color from HEX code"
# Declare our arguments # Declare our arguments
argparse h/help -- $argv argparse h/help -- $argv
@ -6,21 +6,36 @@ function latexcolor --description "Get the LaTeX color from HEX code"
set current_name $(status current-function) set current_name $(status current-function)
# Check if not enough arguments provided, or help menu # Check if not enough arguments provided, or help menu
if set -ql _flag_help || test -z $argv[2] if set -ql _flag_help || test -z $argv[1]
echo -e "Usage: $current_name hex" echo -e "Usage: $current_name \"hexcode\""
echo -e " $current_name [-h|--help] \t\t - Show this help message" echo -e " $current_name [-h|--help] - Show this help message"
return 0 return 0
end end
# Remove '#'
set hex $argv[1] set hex $argv[1]
if string match -r '^#' -- $hex if string match -qr '^#' -- $hex
set hex (string sub -s 2 $hex) set hex (string sub -s 2 $hex)
end end
# Convert hex to RGB # Expand 3 hex to 4 hex
set r (math "0x${hex[1..2]} / 255") # TODO
set g (math "0x${hex[3..4]} / 255")
set b (math "0x${hex[5..6]} / 255")
printf "{%.2f, %.2f, %.2f}\n" $r $g $b # Expand 3 hex to 8 hex
if test (string length $hex) -eq 3
set hex "$(string replace -ar '(.)' '$1$1' $hex)00"
end
# Expand 6 hex to 8 hex
if test (string length $hex) -eq 6
set hex $(string join "" $hex 00)
end
# Convert hex to RGB
set r (math "0x$(string sub -s 1 -l 2 $hex) / 255")
set g (math "0x$(string sub -s 3 -l 2 $hex) / 255")
set b (math "0x$(string sub -s 5 -l 2 $hex) / 255")
set a (math "0x$(string sub -s 7 -l 2 $hex) / 255")
printf "{%.2f, %.2f, %.2f, %.2f}\n" $r $g $b $a
end end