|
4 | 4 | Specifying Colors |
5 | 5 | ***************** |
6 | 6 |
|
7 | | -In almost all places in matplotlib where a color can be specified by the user it can be provided as: |
| 7 | +In almost all places in matplotlib where a color can be specified by the user |
| 8 | +it can be provided as: |
8 | 9 |
|
9 | 10 | * ``(r, g, b)`` tuples |
10 | 11 | * ``(r, g, b, a)`` tuples |
11 | 12 | * hex string, ex ``#OFOFOF`` |
12 | 13 | * float value between [0, 1] for gray level |
13 | 14 | * One of ``{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}`` |
14 | | -* valid css4/X11 color names |
15 | | -* valid name from the `XKCD color survey |
| 15 | +* valid CSS4/X11 color names |
| 16 | +* valid name from the `xkcd color survey |
16 | 17 | <http://blog.xkcd.com/2010/05/03/color-survey-results/>`__ These |
17 | 18 | names are prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``) to |
18 | 19 | prevent name clashes with the CSS4/X11 names. |
19 | 20 |
|
20 | 21 | All string specifications of color are case-insensitive. |
21 | 22 |
|
22 | 23 | Internally, mpl is moving to storing all colors as RGBA float quadruples. |
| 24 | + |
| 25 | +There are 95 (out of 148 colors in the css color list) conflicts between the |
| 26 | +CSS4/X11 names and the xkcd names. Given that the former are the standard |
| 27 | +color names of the web, matplotlib should follow them. Thus, xkcd color names |
| 28 | +are prefixed with ``'xkcd:'``, for example ``'blue'`` maps to ``'#0000FF'`` |
| 29 | +where as ``'xkcd:blue'`` maps to ``'#0343DF'``. |
| 30 | + |
| 31 | +.. plot:: |
| 32 | + |
| 33 | + import matplotlib.pyplot as plt |
| 34 | + import matplotlib._color_data as mcd |
| 35 | + import matplotlib.patches as mpatch |
| 36 | + |
| 37 | + overlap = {name for name in mcd.CSS4_COLORS |
| 38 | + if "xkcd:" + name in mcd.XKCD_COLORS} |
| 39 | + |
| 40 | + fig = plt.figure(figsize=[4.8, 16]) |
| 41 | + ax = fig.add_axes([0, 0, 1, 1]) |
| 42 | + |
| 43 | + for j, n in enumerate(sorted(overlap, reverse=True)): |
| 44 | + cn = mcd.CSS4_COLORS[n] |
| 45 | + xkcd = mcd.XKCD_COLORS["xkcd:" + n].upper() |
| 46 | + if cn != xkcd: |
| 47 | + print(n, cn, xkcd) |
| 48 | + |
| 49 | + r1 = mpatch.Rectangle((0, j), 1, 1, color=cn) |
| 50 | + r2 = mpatch.Rectangle((1, j), 1, 1, color=xkcd) |
| 51 | + txt = ax.text(2, j+.5, ' ' + n, va='center', fontsize=10) |
| 52 | + ax.add_patch(r1) |
| 53 | + ax.add_patch(r2) |
| 54 | + ax.axhline(j, color='k') |
| 55 | + |
| 56 | + ax.text(.5, j + .1, 'X11', ha='center') |
| 57 | + ax.text(1.5, j + .1, 'XKCD', ha='center') |
| 58 | + ax.set_xlim(0, 3) |
| 59 | + ax.set_ylim(0, j + 1) |
| 60 | + ax.axis('off') |
0 commit comments