@@ -160,11 +160,11 @@ def demo(sty):
160
160
# The visual below shows name collisions. Color names where color values agree
161
161
# are in bold.
162
162
163
- import matplotlib ._color_data as mcd
163
+ import matplotlib .colors as mcolors
164
164
import matplotlib .patches as mpatch
165
165
166
- overlap = {name for name in mcd .CSS4_COLORS
167
- if " xkcd:" + name in mcd .XKCD_COLORS }
166
+ overlap = {name for name in mcolors .CSS4_COLORS
167
+ if f' xkcd:{ name } ' in mcolors .XKCD_COLORS }
168
168
169
169
fig = plt .figure (figsize = [9 , 5 ])
170
170
ax = fig .add_axes ([0 , 0 , 1 , 1 ])
@@ -173,23 +173,30 @@ def demo(sty):
173
173
n_rows = len (overlap ) // n_groups + 1
174
174
175
175
for j , color_name in enumerate (sorted (overlap )):
176
- css4 = mcd .CSS4_COLORS [color_name ]
177
- xkcd = mcd .XKCD_COLORS ["xkcd:" + color_name ].upper ()
176
+ css4 = mcolors .CSS4_COLORS [color_name ]
177
+ xkcd = mcolors .XKCD_COLORS [f'xkcd:{ color_name } ' ].upper ()
178
+
179
+ # Pick text colour based on perceived luminance.
180
+ rgba = mcolors .to_rgba_array ([css4 , xkcd ])
181
+ luma = 0.299 * rgba [:, 0 ] + 0.587 * rgba [:, 1 ] + 0.114 * rgba [:, 2 ]
182
+ css4_text_color = 'k' if luma [0 ] > 0.5 else 'w'
183
+ xkcd_text_color = 'k' if luma [1 ] > 0.5 else 'w'
178
184
179
185
col_shift = (j // n_rows ) * 3
180
186
y_pos = j % n_rows
181
- text_args = dict (va = 'center' , fontsize = 10 ,
182
- weight = 'bold' if css4 == xkcd else None )
187
+ text_args = dict (fontsize = 10 , weight = 'bold' if css4 == xkcd else None )
183
188
ax .add_patch (mpatch .Rectangle ((0 + col_shift , y_pos ), 1 , 1 , color = css4 ))
184
189
ax .add_patch (mpatch .Rectangle ((1 + col_shift , y_pos ), 1 , 1 , color = xkcd ))
185
- ax .text (0 + col_shift , y_pos + .5 , ' ' + css4 , alpha = 0.5 , ** text_args )
186
- ax .text (1 + col_shift , y_pos + .5 , ' ' + xkcd , alpha = 0.5 , ** text_args )
187
- ax .text (2 + col_shift , y_pos + .5 , ' ' + color_name , ** text_args )
190
+ ax .text (0.5 + col_shift , y_pos + .7 , css4 ,
191
+ color = css4_text_color , ha = 'center' , ** text_args )
192
+ ax .text (1.5 + col_shift , y_pos + .7 , xkcd ,
193
+ color = xkcd_text_color , ha = 'center' , ** text_args )
194
+ ax .text (2 + col_shift , y_pos + .7 , f' { color_name } ' , ** text_args )
188
195
189
196
for g in range (n_groups ):
190
197
ax .hlines (range (n_rows ), 3 * g , 3 * g + 2.8 , color = '0.7' , linewidth = 1 )
191
- ax .text (0.5 + 3 * g , - 0.5 , 'X11' , ha = 'center' , va = 'center' )
192
- ax .text (1.5 + 3 * g , - 0.5 , 'xkcd' , ha = 'center' , va = 'center' )
198
+ ax .text (0.5 + 3 * g , - 0.3 , 'X11/CSS4 ' , ha = 'center' )
199
+ ax .text (1.5 + 3 * g , - 0.3 , 'xkcd' , ha = 'center' )
193
200
194
201
ax .set_xlim (0 , 3 * n_groups )
195
202
ax .set_ylim (n_rows , - 1 )
0 commit comments