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

# compute luminance of the current terminal

oldstty=$(stty -g)
stty raw -echo min 0 time 0
printf "\e]11;?\e\\"
sleep 0.01
read -r answer
stty "$oldstty"

awk -v s="$answer" 'BEGIN {
	hex="123456789abcdef"
	split(s, a, "")
	r = (16 * index(hex, a[10]) + index(hex, a[11])) / 255
	g = (16 * index(hex, a[15]) + index(hex, a[16])) / 255
	b = (16 * index(hex, a[20]) + index(hex, a[21])) / 255
	# print 0.33 * r + 0.5 * g + 0.16 * b
	exit (0.33 * r + 0.5 * g + 0.16 * b > 0.5)
}'