-
Notifications
You must be signed in to change notification settings - Fork 19
/
hexcolortoner
executable file
·52 lines (42 loc) · 1.14 KB
/
hexcolortoner
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
#!/usr/bin/env bash
# take a color as hexadecimal input and convert it to a lighter or darker variant
# usage hexcolortoner hexcolor amount
# amount should be -ve to make color darker, +ve for lighter
hex=$1
if test -z "$1"; then
echo -n "Type a hex color: "
read -r hex
fi
initpound=$(echo "$hex" | grep -o '^#')
hex=$( echo "$hex" | cut -d'#' -f2)
hextoint() {
echo "$(( 16#$1 ))"
}
inttohex() {
printf "%02x\n" "$1"
}
amount=$2
if test -z "$2"; then
echo -n "Type amount to change: "
read -r amount
fi
if test "$(echo -n "$hex" | wc -c)" -ne 6; then
echo "Hex color has to be 6 digits"
exit 1
fi
red=$(hextoint "$( echo "$hex" | cut -c1-2 )" )
green=$(hextoint "$( echo "$hex" | cut -c3-4 )" )
blue=$(hextoint "$( echo "$hex" | cut -c5-6 )" )
(( red+=amount ))
(( green+=amount ))
(( blue+=amount ))
if test "$red" -gt 255; then red=255; fi
if test "$green" -gt 255; then green=255; fi
if test "$blue" -gt 255; then blue=255; fi
if test "$red" -lt 0; then red=0; fi
if test "$green" -lt 0; then green=0; fi
if test "$blue" -lt 0; then blue=0; fi
rh=$(inttohex $red)
gh=$(inttohex $green)
bh=$(inttohex $blue)
echo "${initpound}${rh}${gh}${bh}"