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

Skip to content

Commit 72f34be

Browse files
committed
New API for accessing the named colors mapping.
For use e.g. by `seaborn.set(color_codes=True)`.
1 parent 75b0d13 commit 72f34be

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@
6868
from ._color_data import XKCD_COLORS, CSS4_COLORS
6969

7070

71+
class _ColorMapping(dict):
72+
def __init__(self, mapping, cache):
73+
super(_ColorMapping, self).__init__(mapping)
74+
self._cache = cache
75+
76+
def __setitem__(self, key, value):
77+
super(_ColorMapping, self).__setitem__(key, value)
78+
self._cache.clear()
79+
80+
def __delitem__(self, key, value):
81+
super(_ColorMapping, self).__delitem__(key, value)
82+
self._cache.clear()
83+
84+
7185
_colors_full_map = {
7286
'b': (0, 0, 1),
7387
'g': (0, 0.5, 0),
@@ -79,9 +93,13 @@
7993
'w': (1, 1, 1)}
8094
_colors_full_map.update(XKCD_COLORS)
8195
_colors_full_map.update(CSS4_COLORS)
96+
_colors_full_map = _ColorMapping(_colors_full_map, {})
8297

8398

84-
_colors_cache = {}
99+
def get_named_colors_mapping():
100+
"""Return the global mapping of names to named colors.
101+
"""
102+
return _colors_full_map
85103

86104

87105
def _is_nth_color(c):
@@ -121,11 +139,11 @@ def to_rgba(c, alpha=None):
121139
colors = prop_cycler._transpose()['color']
122140
c = colors[int(c[1]) % len(colors)]
123141
try:
124-
rgba = _colors_cache[c, alpha]
142+
rgba = _colors_full_map._cache[c, alpha]
125143
except (KeyError, TypeError): # Not in cache, or unhashable.
126144
rgba = _to_rgba_no_colorcycle(c, alpha)
127145
try:
128-
_colors_cache[c, alpha] = rgba
146+
_colors_full_map._cache[c, alpha] = rgba
129147
except TypeError:
130148
pass
131149
return rgba
@@ -247,7 +265,7 @@ class ColorConverter(object):
247265
"""
248266

249267
colors = _colors_full_map
250-
cache = _colors_cache
268+
cache = _colors_full_map._cache
251269

252270
@staticmethod
253271
def to_rgb(arg):

0 commit comments

Comments
 (0)