-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_accessibility.py
112 lines (89 loc) · 3.62 KB
/
test_accessibility.py
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
# ----------------------------------------------------
# Colorblindness tools
# ----------------------------------------------------
from matplotlib.colors import ListedColormap
from colormath import color_diff
from colormath.color_objects import sRGBColor, LabColor, HSVColor, CMYKColor, LCHabColor
from colormath.color_conversions import convert_color
from colortools import *
import matplotlib.pyplot as plt
def get_colormath_sRGB(rgb_old, is_upscaled=False):
'''
Arguments:
- `rgb_old` (list) : a list in which the first 3 elements are [r, g, b] coordinates.
Coordinates are either upscaled (0.0 - 1.0) or not (0 - 255)
- `is_upscaled` (bool) : If True, RGB values are between 1-255. If False, 0.0-1.0.
Returns:
sRGB color object from colormath package
Example Usage:
rgb = get_colormath_sRGB(new_cmaps['rafaj_AQ'].colors[2], is_upscaled=False)
'''
#rgb_old = cmap.colors[i]
rgb = sRGBColor(rgb_old[0], rgb_old[1], rgb_old[2], is_upscaled=is_upscaled)
return rgb
def cmap_to_greyscale(cmap):
'''
Description: wrapper to sequentially call to_grayscale() and return greyscale `hex_list`
Arguments:
- `cmap` a matplotlib ListedColormap (e.g., new_cmaps['rafaj_AQ'])
Returns:
- `hex_list` a list of hex codes corresponding to greyscale version of input cmap
'''
hex_list = []
for rgb in cmap.colors:
sRGB = get_colormath_sRGB(rgb)
greyscale_sRGB = to_grayscale(sRGB)
hex = greyscale_sRGB.get_rgb_hex()
hex_list.append(hex)
return hex_list
def cmap_to_colorblind_g(cmap):
'''
Description: wrapper to sequentially call to_colorblind_g() and return colorblind_g `hex_list`
Arguments:
- `cmap` a matplotlib ListedColormap (e.g., new_cmaps['rafaj_AQ'])
Returns:
- `hex_list` a list of hex codes corresponding to colorblind_g version of input cmap
'''
hex_list = []
for rgb in cmap.colors:
sRGB = get_colormath_sRGB(rgb)
colorblind_g_sRGB = to_colorblind_g(sRGB)
hex = colorblind_g_sRGB.get_rgb_hex()
hex_list.append(hex)
return hex_list
def cmap_to_colorblind_r(cmap):
'''
Description: wrapper to sequentially call to_colorblind_r() and return colorblind_r `hex_list`
Arguments:
- `cmap` a matplotlib ListedColormap (e.g., new_cmaps['rafaj_AQ'])
Returns:
- `hex_list` a list of hex codes corresponding to colorblind_r version of input cmap
'''
hex_list = []
for rgb in cmap.colors:
sRGB = get_colormath_sRGB(rgb)
colorblind_r_sRGB = to_colorblind_r(sRGB)
hex = colorblind_r_sRGB.get_rgb_hex()
hex_list.append(hex)
return hex_list
def display_accessibility_comparison(cmap):
'''
Displays print and colorblindness accessibility for input colormap using colormath tools
Arguments:
- `cmap` (matplotlib ListedColormap)
Example Usage:
display_colorblind_comparison(new_cmaps['rafaj_AQ'])
'''
greyscale = ListedColormap(cmap_to_greyscale(cmap))
colorblind_g = ListedColormap(cmap_to_colorblind_g(cmap))
colorblind_r = ListedColormap(cmap_to_colorblind_r(cmap))
x = np.arange(len(cmap.colors)).reshape(1,-1)
for (cmap, subplt, title) in zip([cmap, greyscale, colorblind_g, colorblind_r],
[411, 412, 413, 414],
['original', 'greyscale', 'colorblind g', 'colorblind r']):
ax = plt.subplot(subplt)
ax.imshow(x, cmap=cmap)
ax.set_title(title)
ax.set_yticks(ticks=[])
ax.set_xticks(ticks=[])
return