summaryrefslogtreecommitdiff
path: root/bin/termcolor
blob: a310e19d3380b7f369379a4f9bf19527c1990e14 (plain)
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
#!/bin/sh
# Change background on a MacOS terminal.

# hsv2rgb converts a hue-saturation-brightness color into red-green-blue.
hsv2rgb() {
	awk -v h="$1" -v s="$2" -v v="$3" 'BEGIN {
		h = h / 360     # normalize hue angle in degree (0-360)
		s = s / 100     # normalize saturation percentage (0-100)
		v = v / 100     # normalize brightness percentage (0-100)
		i = int(h * 6)
		f = h * 6 - i
		p = v * (1 - s)
		q = v * (1 - f * s)
		t = v * (1 - (1 - f) * s)
		j = i % 6
		if (j == 0) { r = v; g = t; b = p } else
		if (j == 1) { r = q; g = v; b = p } else
		if (j == 2) { r = p; g = v; b = t } else
		if (j == 3) { r = p; g = q; b = v } else
		if (j == 4) { r = t; g = p; b = v } else
		if (j == 5) { r = v; g = p; b = q }
		print "{" int(r * 65535) ", " int(g * 65535) ", " int(b * 65535) "}"
	}'
}

sat=8 bri=100 fg="{0, 0, 0}"
case $1 in
	white) hue=0 sat=0 ;;
	gr[ae]y) hue=0 sat=0 bri=93 ;;
	black) hue=0 sat=100 bri=0 fg="{56283, 56283, 56283}" ;;
	red) hue=0 ;;
	orange) hue=30 ;;
	yellow) hue=45 ;;
	green) hue=90 ;;
	blue) hue=180 ;;
	purple) hue=270 ;;
	-[0-9]) hue=$((-60 * $1)) ;;
	[0-9]*) hue=$1 ;;
	*) hue=$((RANDOM % 360)) ;;
esac

[ "$2" ] && sat=$2
[ "$3" ] && bri=$3

case $TERM_PROGRAM in
	Apple_Terminal) tp=Terminal ;;
	*) tp='' ;;
esac

[ "$tp" ] || exit
osascript -e "tell application \"$tp\" to set background color of window 1 to $(hsv2rgb $hue $sat $bri)"
osascript -e "tell application \"$tp\" to set normal text color of window 1 to $fg"