summaryrefslogtreecommitdiff
path: root/bin/termcolor
diff options
context:
space:
mode:
Diffstat (limited to 'bin/termcolor')
-rwxr-xr-xbin/termcolor52
1 files changed, 52 insertions, 0 deletions
diff --git a/bin/termcolor b/bin/termcolor
new file mode 100755
index 0000000..a310e19
--- /dev/null
+++ b/bin/termcolor
@@ -0,0 +1,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"