|
40 | 40 | """
|
41 | 41 |
|
42 | 42 | import base64
|
43 |
| -from collections.abc import Sized, Sequence |
| 43 | +from collections.abc import Sized, Sequence, Mapping |
44 | 44 | import copy
|
45 | 45 | import functools
|
46 | 46 | import inspect
|
|
53 | 53 |
|
54 | 54 | import matplotlib as mpl
|
55 | 55 | import numpy as np
|
56 |
| -from matplotlib import _api, cbook, scale |
| 56 | +from matplotlib import _api, _cm, cbook, scale |
57 | 57 | from ._color_data import BASE_COLORS, TABLEAU_COLORS, CSS4_COLORS, XKCD_COLORS
|
58 | 58 |
|
59 | 59 |
|
@@ -93,6 +93,113 @@ def get_named_colors_mapping():
|
93 | 93 | return _colors_full_map
|
94 | 94 |
|
95 | 95 |
|
| 96 | +class ColorSequenceRegistry(Mapping): |
| 97 | + r""" |
| 98 | + Container for sequences of colors that are known to Matplotlib by name. |
| 99 | +
|
| 100 | + The universal registry instance is `matplotlib.color_sequences`. There |
| 101 | + should be no need for users to instantiate `.ColorSequenceRegistry` |
| 102 | + themselves. |
| 103 | +
|
| 104 | + Read access uses a dict-like interface mapping names to lists of colors:: |
| 105 | +
|
| 106 | + import matplotlib as mpl |
| 107 | + cmap = mpl.color_sequences['tab10'] |
| 108 | +
|
| 109 | + The returned lists are copies, so that their modification does not change |
| 110 | + the global definition of the color sequence. |
| 111 | +
|
| 112 | + Additional color sequences can be added via |
| 113 | + `.ColorSequenceRegistry.register`:: |
| 114 | +
|
| 115 | + mpl.color_sequences.register('rgb', ['r', 'g' ,'b']) |
| 116 | + """ |
| 117 | + |
| 118 | + _BUILTIN_COLOR_SEQUENCES = { |
| 119 | + 'tab10': _cm._tab10_data, |
| 120 | + 'tab20': _cm._tab20_data, |
| 121 | + 'tab20b': _cm._tab20b_data, |
| 122 | + 'tab20c': _cm._tab20c_data, |
| 123 | + 'Pastel1': _cm._Pastel1_data, |
| 124 | + 'Pastel2': _cm._Pastel2_data, |
| 125 | + 'Paired': _cm._Paired_data, |
| 126 | + 'Accent': _cm._Accent_data, |
| 127 | + 'Dark2': _cm._Dark2_data, |
| 128 | + 'Set1': _cm._Set1_data, |
| 129 | + 'Set2': _cm._Set1_data, |
| 130 | + 'Set3': _cm._Set1_data, |
| 131 | + } |
| 132 | + |
| 133 | + def __init__(self): |
| 134 | + self._color_sequences = {**self._BUILTIN_COLOR_SEQUENCES} |
| 135 | + |
| 136 | + def __getitem__(self, item): |
| 137 | + try: |
| 138 | + return list(self._color_sequences[item]) |
| 139 | + except KeyError: |
| 140 | + raise KeyError(f"{item!r} is not a known color sequence name") |
| 141 | + |
| 142 | + def __iter__(self): |
| 143 | + return iter(self._color_sequences) |
| 144 | + |
| 145 | + def __len__(self): |
| 146 | + return len(self._color_sequences) |
| 147 | + |
| 148 | + def __str__(self): |
| 149 | + return ('ColorSequenceRegistry; available colormaps:\n' + |
| 150 | + ', '.join(f"'{name}'" for name in self)) |
| 151 | + |
| 152 | + def register(self, name, color_list): |
| 153 | + """ |
| 154 | + Register a new color sequence. |
| 155 | +
|
| 156 | + The colormap registry stores a copy of the given *color_list*, so that |
| 157 | + future changes to the original list do not affect the registered |
| 158 | + color sequence. Think of this as the registry taking a snapshot |
| 159 | + of *color_list* at registration. |
| 160 | +
|
| 161 | + Parameters |
| 162 | + ---------- |
| 163 | + name : str |
| 164 | + The name for the color sequence. |
| 165 | +
|
| 166 | + color_list : list of colors |
| 167 | + An iterable returning valid Matplotlib colors when iterating over. |
| 168 | + Note however that the returned color sequence will always be a |
| 169 | + list regardless of the input type. |
| 170 | +
|
| 171 | + """ |
| 172 | + if name in self._BUILTIN_COLOR_SEQUENCES: |
| 173 | + raise ValueError(f"{name!r} is a reserved name for a builtin " |
| 174 | + "color sequence") |
| 175 | + |
| 176 | + color_list = list(color_list) # force copy and coerce type to list |
| 177 | + for color in color_list: |
| 178 | + try: |
| 179 | + to_rgba(color) |
| 180 | + except ValueError: |
| 181 | + raise ValueError( |
| 182 | + f"{color!r} is not a valid color specification") |
| 183 | + |
| 184 | + self._color_sequences[name] = color_list |
| 185 | + |
| 186 | + def unregister(self, name): |
| 187 | + """ |
| 188 | + Remove a sequence from the registry. |
| 189 | +
|
| 190 | + You cannot remove built-in color sequences. |
| 191 | +
|
| 192 | + If the name is not registered, returns with no error. |
| 193 | + """ |
| 194 | + if name in self._BUILTIN_COLOR_SEQUENCES: |
| 195 | + raise ValueError( |
| 196 | + f"Cannot unregister builtin color sequence {name!r}") |
| 197 | + self._color_sequences.pop(name, None) |
| 198 | + |
| 199 | + |
| 200 | +_color_sequences = ColorSequenceRegistry() |
| 201 | + |
| 202 | + |
96 | 203 | def _sanitize_extrema(ex):
|
97 | 204 | if ex is None:
|
98 | 205 | return ex
|
|
0 commit comments