From ddc617325d8b25f4b99e50cf6041f60795769d7d Mon Sep 17 00:00:00 2001 From: z3rOR0ne Date: Tue, 20 Dec 2022 22:41:06 -0800 Subject: [PATCH] :lipstick: Added tcolors and refactored dye --- scripts/dye | 66 +++++++++++++++++++++++++------------------------ scripts/tcolors | 12 +++++++++ 2 files changed, 46 insertions(+), 32 deletions(-) create mode 100755 scripts/tcolors diff --git a/scripts/dye b/scripts/dye index 564869f4..9f0f8d76 100755 --- a/scripts/dye +++ b/scripts/dye @@ -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 diff --git a/scripts/tcolors b/scripts/tcolors new file mode 100755 index 00000000..9d8b0c36 --- /dev/null +++ b/scripts/tcolors @@ -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"