💄 Added tcolors and refactored dye

This commit is contained in:
z3rOR0ne 2022-12-20 22:41:06 -08:00
parent b2d4ba5657
commit ddc617325d
2 changed files with 46 additions and 32 deletions

View file

@ -31,37 +31,6 @@ EOM
exit 1
}
alpha_to_hex() {
printf "%02x\n" "$(echo "scale=0; $1*255" | bc)" 2>/dev/null
}
hex_to_rgb() {
: "${1/\#/}"
if ((${#_} == 8)); then
((r = 16#${_:0:2}, g = 16#${_:2:2}, b = 16#${_:4:2}, a = 16#${_:6:2}))
a=$(echo "scale=2; $a / 255" | bc)
else
((r = 16#${_:0:2}, g = 16#${_:2:2}, b = 16#${_:4:2}))
a=1.0
fi
printf "%s\n" "rgba($r, $g, $b, $a)"
}
rgb_to_hex() {
echo "$@"
if [[ "$*" =~ rgb ]]; then
set -- "$(tr ',' ' ' <<<"$*" | tr -cd ' 0-9')"
fi
if [[ -n "$4" ]]; then
local a
a=$(alpha_to_hex "$4")
printf "#%02x%02x%02x%s\n" "$1" "$2" "$3" "$a"
else
printf "#%02x%02x%02x\n" "$1" "$2" "$3"
fi
}
error() {
printf "error: %s\n" "$1" 1>&2
${2:+exit $2}
@ -79,6 +48,37 @@ dependencycheck() {
fi
}
alpha_to_hex() {
printf "%02x\n" "$(echo "scale=0; $1*255" | bc)" 2>/dev/null
}
hex_to_rgb() {
local a
: "${1/\#/}"
if ((${#_} == 8)); then
((r = 16#${_:0:2}, g = 16#${_:2:2}, b = 16#${_:4:2}, a = 16#${_:6:2}))
a=$(echo "scale=2; $a / 255" | bc)
else
((r = 16#${_:0:2}, g = 16#${_:2:2}, b = 16#${_:4:2}))
a=1.0
fi
printf "%s\n" "rgba($r, $g, $b, $a)"
}
rgb_to_hex() {
local a
if [[ "$*" =~ rgb ]]; then
set -- "$(tr ',' ' ' <<<"$*" | tr -cd ' 0-9')"
fi
if [[ -n "$4" ]]; then
a=$(alpha_to_hex "$4")
printf "#%02x%02x%02x%s\n" "$1" "$2" "$3" "$a"
else
printf "#%02x%02x%02x\n" "$1" "$2" "$3"
fi
}
main() {
if [[ "$#" -lt 1 ]]; then
printhelp
@ -91,6 +91,7 @@ main() {
error "-x only parses one following argument"
exit 2
fi
local rgbval clipit
rgbval=$(hex_to_rgb "${2:-''}")
printf "%s\n" "$rgbval"
read -e -r -p "save code to clipboard?(y/n): " clipit
@ -107,7 +108,8 @@ main() {
error "-r parses no less than three arguments"
exit 2
fi
hexval=$(rgb_to_hex "${@:2}" | sed -n 2p)
local hexval clipit
hexval=$(rgb_to_hex "${@:2}")
printf "%s\n" "$hexval"
read -e -r -p "save code to clipboard?(y/n): " clipit
if [[ "$clipit" == "yes" || "$clipit" == "y" ]]; then

12
scripts/tcolors Executable file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env bash
# there are 255 total colors...
echo "$(tput setaf 1)red"
echo "$(tput setaf 2)green"
echo "$(tput setaf 3)yellow"
echo "$(tput setaf 4)blue"
echo "$(tput setaf 5)purple"
echo "$(tput setaf 6)cyan"
echo "$(tput setaf 7)white"
echo "$(tput setaf 8)grey"
echo "$(tput setaf 9)red"
echo "$(tput setaf 10)green"