| 1 |
""" |
| 2 |
Contrast Tests |
| 3 |
~~~~~~~~~~ |
| 4 |
|
| 5 |
Pygments styles should be accessible to people with suboptimal vision. |
| 6 |
This test ensures that the minimum contrast of styles does not degrade, |
| 7 |
and that every rule of a new style fulfills the WCAG AA standard.[1] |
| 8 |
|
| 9 |
[1]: https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html |
| 10 |
""" |
| 11 |
|
| 12 |
import json |
| 13 |
import os |
| 14 |
|
| 15 |
import pygments.styles |
| 16 |
import pygments.token |
| 17 |
import wcag_contrast_ratio |
| 18 |
|
| 19 |
JSON_FILENAME = os.path.join(os.path.dirname(__file__), "min_contrasts.json") |
| 20 |
WCAG_AA_CONTRAST = 4.5 |
| 21 |
|
| 22 |
|
| 23 |
def hex2rgb(hexstr): |
| 24 |
hexstr = hexstr.lstrip("#") |
| 25 |
r = int(hexstr[:2], 16) / 255 |
| 26 |
g = int(hexstr[2:4], 16) / 255 |
| 27 |
b = int(hexstr[4:], 16) / 255 |
| 28 |
return (r, g, b) |
| 29 |
|
| 30 |
|
| 31 |
def get_style_contrasts(style_cls): |
| 32 |
return [ |
| 33 |
( |
| 34 |
round( |
| 35 |
wcag_contrast_ratio.rgb( |
| 36 |
hex2rgb(style["bgcolor"] or style_cls.background_color), |
| 37 |
hex2rgb(style["color"] or "#000000") |
| 38 |
# we default to black because browsers also do |
| 39 |
), |
| 40 |
1, |
| 41 |
), |
| 42 |
ttype, |
| 43 |
) |
| 44 |
for ttype, style in style_cls.list_styles() |
| 45 |
if ttype != pygments.token.Whitespace |
| 46 |
] |
| 47 |
|
| 48 |
|
| 49 |
def builtin_styles(): |
| 50 |
for style_name in pygments.styles.STYLE_MAP: |
| 51 |
yield (style_name, pygments.styles.get_style_by_name(style_name)) |
| 52 |
|
| 53 |
|
| 54 |
def min_contrasts(): |
| 55 |
return { |
| 56 |
name: min(x[0] for x in get_style_contrasts(style)) |
| 57 |
for name, style in builtin_styles() |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
def update_json(): |
| 62 |
with open(JSON_FILENAME, "w", encoding="utf-8") as f: |
| 63 |
json.dump( |
| 64 |
min_contrasts(), |
| 65 |
f, |
| 66 |
indent=2, |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
def test_contrasts(fail_if_improved=True): |
| 71 |
with open(JSON_FILENAME, encoding="utf-8") as f: |
| 72 |
previous_contrasts = json.load(f) |
| 73 |
|
| 74 |
for style_name in pygments.styles.STYLE_MAP: |
| 75 |
style = pygments.styles.get_style_by_name(style_name) |
| 76 |
contrasts = get_style_contrasts(style) |
| 77 |
min_contrast = min([x[0] for x in contrasts]) |
| 78 |
|
| 79 |
bar = previous_contrasts.get(style_name, WCAG_AA_CONTRAST) |
| 80 |
|
| 81 |
assert not min_contrast < bar, ( |
| 82 |
"contrast degradation for style '{}'\n" |
| 83 |
"The following rules have a contrast lower than the required {}:\n\n" |
| 84 |
"{}\n" |
| 85 |
).format( |
| 86 |
style_name, |
| 87 |
bar, |
| 88 |
"\n".join( |
| 89 |
[ |
| 90 |
f"* {contrast:.2f} {ttype}" |
| 91 |
for contrast, ttype in contrasts |
| 92 |
if contrast < bar |
| 93 |
] |
| 94 |
), |
| 95 |
) |
| 96 |
|
| 97 |
if fail_if_improved: |
| 98 |
assert ( |
| 99 |
not min_contrast > bar |
| 100 |
), "congrats, you improved a contrast! please run ./scripts/update_contrasts.py" |