forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor-conversion.sh
executable file
·63 lines (51 loc) · 1.47 KB
/
color-conversion.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Color Conversion
# @raycast.mode fullOutput
# @raycast.packageName Conversions
# Optional parameters:
# @raycast.icon 🎨
# @raycast.needsConfirmation false
# @raycast.argument1 { "type": "text", "placeholder": "Color (e.g. #FFEEFF | RGB(..) | RGBA(..))" }
# @raycast.argument2 { "type": "text", "placeholder": "New format (e.g. RGB | RGBA | HEX)" }
# Documentation:
# @raycast.description Convert color formats (e.g. #FFEEFF -> rgba(255,238,255,1)
# @raycast.author quelhasu
# @raycast.authorURL https://github.com/quelhasu
function rgbToHex() {
RGB=$(echo "$1" | awk -F '[()]' '{print $2}')
IFS=,
set $RGB
R=$1
G=$2
B=$3
printf "%02x%02x%02x" "$R" "$G" "$B"
}
color=$(echo "$1" | tr '[:upper:]' '[:lower:]')
to_format=$(echo "$2" | tr '[:upper:]' '[:lower:]')
if [ "${color:0:1}" = "#" ]; then
hex=$(echo "${color:1:6}")
elif [ "${color:0:4}" = "rgba" ] || [ "${color:0:3}" = "rgb" ]; then
hex=$(rgbToHex $color)
else
echo "Color format unknown"
fi
case $to_format in
rgb)
transformed_color=$(printf "rgb(%d,%d,%d)" 0x${hex:0:2} 0x${hex:2:2} 0x${hex:4:2})
;;
rgba)
transformed_color=$(printf "rgba(%d,%d,%d,1)" 0x${hex:0:2} 0x${hex:2:2} 0x${hex:4:2})
;;
hex)
transformed_color=$(echo "#${hex}")
;;
*)
echo "Use correct format {HEX|RGBA|RGB}"
;;
esac
if [ "${transformed_color}" ]; then
echo $transformed_color | pbcopy
echo "Color ${color} -> ${transformed_color}"
fi