Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c1d4e6

Browse files
committed
Use xkcd: prefix to avoid color name clashes.
Otherwise, merging CSS4_COLORS and XKCD_COLORS becomes order-dependent. It's also unclear why space-less names are needed, but I can put them back if someone feels strongly about it...
1 parent aeb6112 commit 3c1d4e6

3 files changed

Lines changed: 7 additions & 61 deletions

File tree

doc/users/colors.rst

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,9 @@ In almost all places in matplotlib where a color can be specified by the user it
1414
* valid css4/X11 color names
1515
* valid name from the `XKCD color survey
1616
<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 XKCD mapping are used prefix the space-less name with
20-
``'XKCD'``.
17+
names are prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``) to
18+
prevent name clashes with the CSS4/X11 names.
2119

2220
All string specifications of color are case-insensitive.
2321

2422
Internally, mpl is moving to storing all colors as RGBA float quadruples.
25-
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'``.
39-
40-
.. plot::
41-
42-
import matplotlib.pyplot as plt
43-
import matplotlib._color_data as mcd
44-
45-
import matplotlib.patches as mpatch
46-
overlap = (set(mcd.CSS4_COLORS) & set(mcd.XKCD_COLORS))
47-
48-
fig = plt.figure(figsize=[4.8, 16])
49-
ax = fig.add_axes([0, 0, 1, 1])
50-
51-
j = 0
52-
53-
for n in sorted(overlap, reverse=True):
54-
cn = mcd.CSS4_COLORS[n]
55-
xkcd = mcd.XKCD_COLORS[n].upper()
56-
if cn != xkcd:
57-
print (n, cn, xkcd)
58-
59-
r1 = mpatch.Rectangle((0, j), 1, 1, color=cn)
60-
r2 = mpatch.Rectangle((1, j), 1, 1, color=xkcd)
61-
txt = ax.text(2, j+.5, ' ' + n, va='center', fontsize=10)
62-
ax.add_patch(r1)
63-
ax.add_patch(r2)
64-
ax.axhline(j, color='k')
65-
j += 1
66-
67-
ax.text(.5, j+.1, 'X11', ha='center')
68-
ax.text(1.5, j+.1, 'XKCD', ha='center')
69-
ax.set_xlim(0, 3)
70-
ax.set_ylim(0, j + 1)
71-
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
@@ -563,7 +563,7 @@ def test_xkcd():
563563
mcolors.colorConverter.to_rgb('blue'))
564564
assert x11_blue == '#0000ff'
565565
XKCD_blue = mcolors.rgb2hex(
566-
mcolors.colorConverter.to_rgb('XKCDblue'))
566+
mcolors.colorConverter.to_rgb('xkcd:blue'))
567567
assert XKCD_blue == '#0343df'
568568

569569

@@ -621,7 +621,7 @@ def test_cn():
621621
assert red == '#ff0000'
622622

623623
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
624-
['XKCDblue', 'r'])
624+
['xkcd:blue', 'r'])
625625
XKCD_blue = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C0'))
626626
assert XKCD_blue == '#0343df'
627627
red = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C1'))

0 commit comments

Comments
 (0)