|
15 | 15 | normalization.
|
16 | 16 | """
|
17 | 17 |
|
18 |
| -from collections.abc import MutableMapping |
| 18 | +from collections.abc import Mapping, MutableMapping |
| 19 | +import copy |
19 | 20 | import functools
|
20 | 21 |
|
21 | 22 | import numpy as np
|
@@ -112,13 +113,44 @@ def _warn_deprecated(self):
|
112 | 113 | )
|
113 | 114 |
|
114 | 115 |
|
| 116 | +class ColormapRegistry(Mapping): |
| 117 | + r""" |
| 118 | + Container for registered colormaps. |
| 119 | +
|
| 120 | + This is a read-only mapping of names to `.Colormap`\s. Use like a dict. |
| 121 | +
|
| 122 | + Returned `.Colormap`.s are copies, so that their modification does not |
| 123 | + change the global definition of the colormap. |
| 124 | + """ |
| 125 | + def __init__(self, cmaps): |
| 126 | + self._cmaps = cmaps |
| 127 | + |
| 128 | + def __getitem__(self, item): |
| 129 | + try: |
| 130 | + return copy.deepcopy(self._cmaps[item]) |
| 131 | + except KeyError: |
| 132 | + raise KeyError(f"{item} is not a known colormap name") |
| 133 | + |
| 134 | + def __iter__(self): |
| 135 | + return iter(self._cmaps) |
| 136 | + |
| 137 | + def __len__(self): |
| 138 | + return len(self._cmaps) |
| 139 | + |
| 140 | + |
115 | 141 | _cmap_registry = _gen_cmap_registry()
|
116 | 142 | locals().update(_cmap_registry)
|
117 | 143 | # This is no longer considered public API
|
118 | 144 | cmap_d = _DeprecatedCmapDictWrapper(_cmap_registry)
|
| 145 | +colormaps = ColormapRegistry(_cmap_registry) |
| 146 | +r""" |
| 147 | +Container for all registered colormaps. |
119 | 148 |
|
| 149 | +This is a read-only mapping of names to `.Colormap`\s. Use like a dict. |
120 | 150 |
|
121 |
| -# Continue with definitions ... |
| 151 | +Returned `.Colormap`\s are copies, so that their modification does not |
| 152 | +change the global definition of the colormap. |
| 153 | +""" |
122 | 154 |
|
123 | 155 |
|
124 | 156 | def register_cmap(name=None, cmap=None, data=None, lut=None):
|
|
0 commit comments