aboutsummaryrefslogtreecommitdiff
path: root/colors/wcag.sh
blob: 4b8d313ca61fee8bbb1bb862c70b72d5e6c1f253 (plain) (blame)
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
53
54
55
56
57
#!/bin/sh
. ./ansi.sh
. ./colors.sh

# this uses the webaim.org contrast checker api to check all colors against
# background.
# depends on jo, jq, and xh

[ $# -lt 0 ] || set -- \
	red   green   yellow   blue   magenta   cyan   white \
	brred brgreen bryellow brblue brmagenta brcyan brwhite

# lower: error if below this
lower=4.5
# higher: warn if below this
higher=7

# reset after printing
reprint() {
	printf '%s%s' "$@" "$(reset)"
}

prefix() {
	reprint "$(acol $1)$2"
}

demo() {
	reprint "$(fgc $color)$(bgc $background)demo text"
}

# $1 should be "fail" or "warn"
fmt() {
	case "$1" in
		fail) printf '"%s: #%s at \(.ratio)\t%s (%s)\n"' \
				"$(prefix 31 fail)" "$color" "$(demo)" "$colnm";;
		warn) printf '"%s: #%s at \(.ratio)\t%s (%s)\n"' \
				"$(prefix 33 warn)" "$color" "$(demo)" "$colnm";;
		*) printf '""' ;;
	esac
}

# reads stdin (json) and prints information about color if it fails
check() {
	jq -j "
		if (.ratio | tonumber) < $lower then    $(fmt fail)
		elif (.ratio | tonumber) < $higher then $(fmt warn)
		else                                    $(fmt none)
		end"
}

for i; do
	color=$(eval echo '$'$i)
	colnm=$i
	xh https://webaim.org/resources/contrastchecker/ api== \
		bcolor==$background fcolor==$color | \
		jo -f- color=$color | check
	done