|
68 | 68 | from ._color_data import XKCD_COLORS, CSS4_COLORS |
69 | 69 |
|
70 | 70 |
|
| 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 | + |
71 | 85 | _colors_full_map = { |
72 | 86 | 'b': (0, 0, 1), |
73 | 87 | 'g': (0, 0.5, 0), |
|
79 | 93 | 'w': (1, 1, 1)} |
80 | 94 | _colors_full_map.update(XKCD_COLORS) |
81 | 95 | _colors_full_map.update(CSS4_COLORS) |
| 96 | +_colors_full_map = _ColorMapping(_colors_full_map, {}) |
82 | 97 |
|
83 | 98 |
|
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 |
85 | 103 |
|
86 | 104 |
|
87 | 105 | def _is_nth_color(c): |
@@ -121,11 +139,11 @@ def to_rgba(c, alpha=None): |
121 | 139 | colors = prop_cycler._transpose()['color'] |
122 | 140 | c = colors[int(c[1]) % len(colors)] |
123 | 141 | try: |
124 | | - rgba = _colors_cache[c, alpha] |
| 142 | + rgba = _colors_full_map._cache[c, alpha] |
125 | 143 | except (KeyError, TypeError): # Not in cache, or unhashable. |
126 | 144 | rgba = _to_rgba_no_colorcycle(c, alpha) |
127 | 145 | try: |
128 | | - _colors_cache[c, alpha] = rgba |
| 146 | + _colors_full_map._cache[c, alpha] = rgba |
129 | 147 | except TypeError: |
130 | 148 | pass |
131 | 149 | return rgba |
@@ -247,7 +265,7 @@ class ColorConverter(object): |
247 | 265 | """ |
248 | 266 |
|
249 | 267 | colors = _colors_full_map |
250 | | - cache = _colors_cache |
| 268 | + cache = _colors_full_map._cache |
251 | 269 |
|
252 | 270 | @staticmethod |
253 | 271 | def to_rgb(arg): |
|
0 commit comments