✏️ Added more to help/fixed regex
This commit is contained in:
parent
db19605ebe
commit
c9f1a4a757
1 changed files with 32 additions and 23 deletions
55
scripts/dye
55
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
|
||||
;;
|
||||
?)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue