-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-contrast
executable file
·179 lines (144 loc) · 4.07 KB
/
test-contrast
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh
init_variables() {
COMMAND=${0##*/}
THEME=
}
show_usage() {
cat <<- EOF
usage: ${COMMAND} <theme>
Test the theme colors contrast to each other.
Only the most meaningful combinations are tested.
Options:
-h show this message only
EOF
}
parse_command_line() {
while getopts h OPT; do
case "${OPT}" in
h) show_usage; exit 0 ;;
?) exit_error ;;
esac
done
shift $(( OPTIND - 1 ))
THEME="$1"
}
validate_command_line() {
if [ -z "${THEME}" ]; then
exit_error 'missing theme'
fi
if [ ! -f "config/colors/base_${THEME}" ]; then
exit_error "invalid theme '${THEME}'"
fi
}
# shellcheck disable=SC2120
exit_error() {
[ -z "$1" ] || echo "${COMMAND}: $1"
printf "Try '%s -h' for more information.\n" "${COMMAND}"
exit 1
} >&2
print_contrast() {
RGB1=$( base_to_rgb "$1" )
RGB2=$( base_to_rgb "$2" )
CONTRAST=$( contrast_ratio "${RGB1}" "${RGB2}" )
printf '\033[0m' # reset
printf '\033[38;2;%sm' "${RGB1}" # foreground
printf '\033[48;2;%sm' "${RGB2}" # background
printf ' %s ' "$1" # base 1
printf '\033[7m' # invert background/foreground
printf ' %s ' "$2" # base 2
printf '\033[0m' # reset
printf ' %4.1f' "${CONTRAST}" # contrast
printf '\n' # newline
}
base_to_hex() {
sed -n "s/^| $1 | #\([[:alnum:]]\+\) |.*/\1/p" "config/colors/base_${THEME}"
}
hex_to_rgb() {
HEX_R=$( echo "$1" | cut -c 1-2 )
HEX_G=$( echo "$1" | cut -c 3-4 )
HEX_B=$( echo "$1" | cut -c 5-6 )
# use ; as a delimiter between the RGB components
# to be able to directly use the full value inside ANSI escape sequences
printf "%d;%d;%d\n" "0x${HEX_R}" "0x${HEX_G}" "0x${HEX_B}"
}
base_to_rgb() { hex_to_rgb "$( base_to_hex "$1" )"; }
relative_luminance() {
R="$1"
G="$2"
B="$3"
tmp_value() {
VALUE="$1"
DEC_VALUE=$( echo "${VALUE} / 255" | bc -l )
if [ "$( echo "${DEC_VALUE} <= 0.03928" | bc -l )" -eq 1 ]; then
echo "${DEC_VALUE} / 12.92" | bc -l
else
# bc doesn't support fractional exponent so we replace it with exp/ln usage
echo "e(2.4 * l( ( ${DEC_VALUE} + 0.055 ) / 1.055 ) )" | bc -l
fi
}
R=$( tmp_value "${R}" )
G=$( tmp_value "${G}" )
B=$( tmp_value "${B}" )
echo "( 0.2126 * ${R} + 0.7152 * ${G} + 0.0722 * ${B} )" | bc -l
}
contrast_ratio() {
COLOR_1="$1"
COLOR_2="$2"
R1=$( echo "${COLOR_1}" | cut -f 1 -d ';' )
G1=$( echo "${COLOR_1}" | cut -f 2 -d ';' )
B1=$( echo "${COLOR_1}" | cut -f 3 -d ';' )
R2=$( echo "${COLOR_2}" | cut -f 1 -d ';' )
G2=$( echo "${COLOR_2}" | cut -f 2 -d ';' )
B2=$( echo "${COLOR_2}" | cut -f 3 -d ';' )
L1=$( relative_luminance "${R1}" "${G1}" "${B1}" )
L2=$( relative_luminance "${R2}" "${G2}" "${B2}" )
if [ "$( echo "${L1} < ${L2}" | bc -l )" -eq 1 ]; then
L_HIGH="${L2}"
L_LOW="${L1}"
else
L_HIGH="${L1}"
L_LOW="${L2}"
fi
echo "( ${L_HIGH} + 0.05 ) / ( ${L_LOW} + 0.05 )" | bc -l
}
test_contrast() {
print_contrast base04 base00
print_contrast base04 base01
print_contrast base04 base02
printf '\n'
print_contrast base05 base01
print_contrast base05 base02
print_contrast base05 base03
printf '\n'
print_contrast base03 base00
print_contrast base04 base00
print_contrast base05 base00
print_contrast base06 base00
print_contrast base07 base00
print_contrast base08 base00
print_contrast base09 base00
print_contrast base0A base00
print_contrast base0B base00
print_contrast base0C base00
print_contrast base0D base00
print_contrast base0E base00
print_contrast base0F base00
printf '\n'
print_contrast base03 base01
print_contrast base04 base01
print_contrast base05 base01
print_contrast base06 base01
print_contrast base07 base01
print_contrast base08 base01
print_contrast base09 base01
print_contrast base0A base01
print_contrast base0B base01
print_contrast base0C base01
print_contrast base0D base01
print_contrast base0E base01
print_contrast base0F base01
}
init_variables
parse_command_line "$@"
validate_command_line
test_contrast