From c9f1a4a757e928e4086129344fb4d9e9fe07111f Mon Sep 17 00:00:00 2001 From: z3rOR0ne Date: Thu, 22 Dec 2022 19:44:47 -0800 Subject: [PATCH] :pencil2: Added more to help/fixed regex --- scripts/dye | 55 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/scripts/dye b/scripts/dye index 60b9ec74..aecef630 100755 --- a/scripts/dye +++ b/scripts/dye @@ -10,15 +10,17 @@ options: examples: dye -x "#ffffff" dye -r 255 255 255 + dye -r "rgb(255, 255, 255)" dye -x "#43fF6480" dye -r 67 255 100 0.5 + dye -r "rgba(67, 255, 100, 0.5)" EOM - exit 1 + exit 0 } error() { printf "error: %s\n" "$1" 1>&2 - ${2:+exit $2} + exit 2 } dependencycheck() { @@ -73,11 +75,18 @@ hex_to_rgb() { printf "%s\n" "rgba($r, $g, $b, $a)" } -# parse_rgb() takes rgb string to extract -# numeric values from it and then pass that to rgb_to_hex() -# parse_rgb() { -# -# } +# parse_rgb() takes rgb string to pass to rgb_to_hex() +parse_rgb() { + local regex='^\s?rgb[a]?[(][0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}[)]$|^rgb[a]?[(][0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, [0-1]?.[0-9][0-9]?[)]\s?$' + if [[ $2 =~ $regex ]]; then + local numbers + numbers=$(echo "$2" | sed -n 's/.*\(([^()]*)\).*/\1/p' | sed 's/[()]//g' | sed 's/,\s/ /g') + read -r -a rgb <<< "$numbers" + rgb_to_hex "${rgb[$"0"]}" "${rgb[$"1"]}" "${rgb[$"2"]}" "${rgb[$"3"]}" + else + error "pattern passed is not valid rgba format" 2 + fi +} rgb_to_hex() { local a @@ -99,7 +108,6 @@ main() { x) if [[ "$#" -gt 2 ]]; then error "-x only parses one following argument" - exit 2 fi local rgbval clipit rgbval=$(hex_to_rgb "${2:-''}") @@ -111,22 +119,23 @@ main() { fi ;; r) - # parse an 'rgb()' or 'rgba' string here - # regex compare to string - if [[ "$#" -gt 5 ]]; then - error "-r parses up to four following argument" - exit 2 - elif [[ "$#" -lt 4 ]]; then - error "-r parses no less than three arguments" - exit 2 - fi 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 - dependencycheck xclip - printf "%s" "$hexval" | xclip -sel clip + if [[ "$#" -eq 2 ]]; then + hexval=$(parse_rgb "$@") + printf "%s\n" "$hexval" + read -e -r -p "save code to clipboard?(y/n): " clipit + if [[ "$clipit" == "yes" || "$clipit" == "y" ]]; then + dependencycheck xclip + printf "%s" "$hexval" | xclip -sel clip + fi + else + 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 + dependencycheck xclip + printf "%s" "$hexval" | xclip -sel clip + fi fi ;; ?)