diff options
| author | 2023-07-12 14:15:20 +0200 | |
|---|---|---|
| committer | 2023-07-12 14:15:20 +0200 | |
| commit | 647ecf960f95b65ce184865baced09070e20217b (patch) | |
| tree | 7bd6a76085530ea285e5299746dc486d8e274269 | |
| parent | tool: move ansi helpers out of demo.sh, enforce background in demo (diff) | |
tool: add wcag test script
| -rw-r--r-- | colors/wcag.sh | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/colors/wcag.sh b/colors/wcag.sh new file mode 100644 index 0000000..4b8d313 --- /dev/null +++ b/colors/wcag.sh @@ -0,0 +1,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 |
