summaryrefslogtreecommitdiff
path: root/bin/isdarktty
blob: 334d8a092de4bc7f538825940e0e19ec9197834c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh

# Check darkness by computing luminance of the current terminal.

trap "stty $(stty -g)" EXIT
stty raw -echo min 0 time 0
printf '\e]11;?\e\\'
sleep 0.01

awk -v FS='' -v hex=123456789abcdef '{
	r = (16 * index(hex, $10) + index(hex, $11)) / 255
	g = (16 * index(hex, $15) + index(hex, $16)) / 255
	b = (16 * index(hex, $20) + index(hex, $21)) / 255
	# print 0.33 * r + 0.5 * g + 0.16 * b
	exit (0.33 * r + 0.5 * g + 0.16 * b > 0.5)
}'