|
33 | 33 | for name, colors in palettes.items():
|
34 | 34 | C = mpl.colors.to_rgba_array(colors).reshape((1, len(colors), 4))
|
35 | 35 | ax.imshow(C, extent=[xmin, xmax, ymin, ymax])
|
| 36 | + |
| 37 | + # Drop alpha by assuming we're on a white background PDF. |
| 38 | + alpha = C[0, :, 3] |
| 39 | + rgb = C[0, :, :3] * alpha[:, np.newaxis] + (1 - alpha[:, np.newaxis]) |
| 40 | + # Same calculation for luminance as |
| 41 | + # https://matplotlib.org/stable/users/explain/colors/colors.html#comparison-between-x11-css4-and-xkcd-colors |
| 42 | + luma = 0.299 * rgb[:, 0] + 0.587 * rgb[:, 1] + 0.114 * rgb[:, 2] |
| 43 | + |
36 | 44 | dx = (xmax-xmin)/len(colors)
|
37 | 45 | for i in range(len(colors)):
|
38 |
| - color = "white" |
39 |
| - if colors[i] in ['1.0', 'w']: color = "black" |
| 46 | + text_color = "black" if luma[i] > 0.5 else "white" |
40 | 47 | text = str(colors[i]).replace(' ', '')
|
41 |
| - ax.text((i+0.5)*dx, (ymin+ymax)/2, text, color=color, zorder=10, |
| 48 | + ax.text((i+0.5)*dx, (ymin+ymax)/2, text, color=text_color, zorder=10, |
42 | 49 | family="Source Code Pro", size=9, ha="center", va="cent
4162
er")
|
43 | 50 |
|
44 | 51 | fig.savefig(ROOT_DIR / f"figures/colors-{name}.pdf")
|
|
0 commit comments