8000 Merge pull request #6390 from anntzer/xkcd-colors-namespace · matplotlib/matplotlib@c2b6769 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2b6769

Browse files
committed
Merge pull request #6390 from anntzer/xkcd-colors-namespace
API: Use xkcd: prefix to avoid color name clashes.
1 parent 9b39f3e commit c2b6769

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

doc/users/colors.rst

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,57 @@
44
Specifying Colors
55
*****************
66

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:
89

910
* ``(r, g, b)`` tuples
1011
* ``(r, g, b, a)`` tuples
1112
* hex string, ex ``#OFOFOF``
1213
* float value between [0, 1] for gray level
1314
* 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
1617
<http://blog.xkcd.com/2010/05/03/color-survey-results/>`__ These
17-
names are available both with and with out spaces. In the case of name clashes
18-
the css/X11 names have priority. To ensure colors
19-
from the XK 8000 CD mapping are used prefix the space-less name with
20-
``'XKCD'``.
18+
names are prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``) to
19+
prevent name clashes with the CSS4/X11 names.
2120

2221
All string specifications of color are case-insensitive.
2322

2423
Internally, mpl is moving to storing all colors as RGBA float quadruples.
2524

26-
Name clash between CSS4/X11 and XKCD
27-
------------------------------------
28-
29-
The color names in the XKCD survey include spaces (unlike css4/X11
30-
names). Matplotlib exposes all of the XKCD colors both with and
31-
without spaces.
32-
33-
There are 95 (out of 148 colors in the css color list) conflicts
34-
between the css4/X11 names and the XKCD names. Given that these are
35-
the standard color names of the web, matplotlib should follow these
36-
conventions. To accesses the XKCD colors which are shadowed by css4,
37-
prefix the colorname with ``'XKCD'``, for example ``'blue'`` maps to
38-
``'#0000FF'`` where as ``'XKCDblue'`` maps to ``'#0343DF'``.
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'``.
3930

4031
.. plot::
4132

4233
import matplotlib.pyplot as plt
4334
import matplotlib._color_data as mcd
44-
4535
import matplotlib.patches as mpatch
46-
overlap = (set(mcd.CSS4_COLORS) & set(mcd.XKCD_COLORS))
36+
37+
overlap = {name for name in mcd.CSS4_COLORS
38+
if "xkcd:" + name in mcd.XKCD_COLORS}
4739

4840
fig = plt.figure(figsize=[4.8, 16])
4941
ax = fig.add_axes([0, 0, 1, 1])
5042

51-
j = 0
52-
53-
for n in sorted(overlap, reverse=True):
43+
for j, n in enumerate(sorted(overlap, reverse=True)):
5444
cn = mcd.CSS4_COLORS[n]
55-
xkcd = mcd.XKCD_COLORS[n].upper()
45+
xkcd = mcd.XKCD_COLORS["xkcd:" + n].upper()
5646
if cn != xkcd:
57-
print (n, cn, xkcd)
47+
print(n, cn, xkcd)
5848

5949
r1 = mpatch.Rectangle((0, j), 1, 1, color=cn)
6050
r2 = mpatch.Rectangle((1, j), 1, 1, color=xkcd)
6151
txt = ax.text(2, j+.5, ' ' + n, va='center', fontsize=10)
6252
ax.add_patch(r1)
6353
ax.add_patch(r2)
6454
ax.axhline(j, color='k')
65-
j += 1
6655

67-
ax.text(.5, j+.1, 'X11', ha='center')
68-
ax.text(1.5, j+.1, 'XKCD', ha='center')
56+
ax.text(.5, j + .1, 'X11', ha='center')
57+
ax.text(1.5, j + .1, 'XKCD', ha='center')
6958
ax.set_xlim(0, 3)
7059
ax.set_ylim(0, j + 1)
7160
ax.axis('off')

lib/matplotlib/_color_data.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -961,14 +961,9 @@
961961
'green': '#15b01a',
962962
'purple': '#7e1e9c'}
963963

964-
# normalize to names with no spaces and provide versions with XKCD
965-
# prefix.
966-
for k in list(XKCD_COLORS):
967-
XKCD_COLORS['xkcd'+k] = XKCD_COLORS[k]
968-
_k = k.replace(' ', '')
969-
if _k != k:
970-
XKCD_COLORS[_k] = XKCD_COLORS[k]
971-
XKCD_COLORS['xkcd'+_k] = XKCD_COLORS[k]
964+
965+
# Normalize name to "xkcd:<name>" to avoid name collisions.
966+
XKCD_COLORS = {'xkcd:' + name: value for name, value in XKCD_COLORS.items()}
972967

973968

974969
# https://drafts.csswg.org/css-color-4/#named-colors

lib/matplotlib/tests/test_colors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ def test_xkcd():
562562
mcolors.colorConverter.to_rgb('blue'))
563563
assert x11_blue == '#0000ff'
564564
XKCD_blue = mcolors.rgb2hex(
565-
mcolors.colorConverter.to_rgb('XKCDblue'))
565+
mcolors.colorConverter.to_rgb('xkcd:blue'))
566566
assert XKCD_blue == '#0343df'
567567

568568

@@ -608,7 +608,7 @@ def test_cn():
608608
assert red == '#ff0000'
609609

610610
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
611-
['XKCDblue', 'r'])
611+
['xkcd:blue', 'r'])
612612
XKCD_blue = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C0'))
613613
assert XKCD_blue == '#0343df'
614614
red = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C1'))

0 commit comments

Comments
 (0)
0