Skip to content

Commit 6cf4289

Browse files
committed
Translate grey-->gray and trigger adjust_grays for gray-like strings
1 parent 58ce2c9 commit 6cf4289

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

proplot/colors.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
_regex_hex = r'#(?:[0-9a-fA-F]{3,4}){2}' # 6-8 digit hex
6565
REGEX_HEX = re.compile(rf'\A{_regex_hex}\Z')
6666
REGEX_HEX_MULTI = re.compile(_regex_hex)
67+
REGEX_GRAY = re.compile(r'\A(light|dark|medium|pale|charcoal)?\s*(gray[0-9]?)?\Z')
6768

6869
# Colormap constants
6970
CMAPS_CYCLIC = tuple( # cyclic colormaps loaded from rgb files
@@ -2159,11 +2160,11 @@ def from_list(cls, *args, adjust_grays=True, **kwargs):
21592160
----------
21602161
%(colors.from_list)s
21612162
adjust_grays : bool, optional
2162-
Whether to adjust the hues of grayscale colors (including
2163-
``'white'`` and ``'black'``) to the hues of the preceding and
2164-
subsequent colors in the sequence. This facilitates the construction
2165-
of diverging colormaps with monochromatic segments using input like
2166-
``PerceptualColormap.from_list(['blueish', 'white', 'reddish'])``.
2163+
Whether to adjust the hues of grayscale colors (including ``'white'``,
2164+
``'black'``, and the ``'grayN'`` open-color colors) to the hues of the
2165+
preceding and subsequent colors in the sequence. This facilitates the
2166+
construction of diverging colormaps with monochromatic segments using
2167+
e.g. ``PerceptualColormap.from_list(['blue', 'white', 'red'])``.
21672168
21682169
Other parameters
21692170
----------------
@@ -2205,9 +2206,9 @@ def from_list(cls, *args, adjust_grays=True, **kwargs):
22052206

22062207
# Adjust grays
22072208
if adjust_grays:
2208-
rgbs = [to_rgb(color) for color in colors]
22092209
hues = cdict['hue'] # segment data
2210-
for i, rgb in enumerate(rgbs):
2210+
for i, color in enumerate(colors):
2211+
rgb = to_rgb(color)
22112212
if not np.allclose(np.array(rgb), rgb[0]):
22122213
continue
22132214
hues[i] = list(hues[i]) # enforce mutability
@@ -2829,8 +2830,8 @@ def _get_rgba(self, arg, alpha):
28292830

28302831
class ColorDatabase(dict):
28312832
"""
2832-
Dictionary subclass used to replace the builtin matplotlib color
2833-
database. See `~ColorDatabase.cache` for details.
2833+
Dictionary subclass used to replace the builtin matplotlib color database.
2834+
See `~ColorDatabase.__getitem__` for details.
28342835
"""
28352836
# NOTE: Matplotlib's database also inherits from dict. MutableMapping not needed
28362837
# since usage is entirely internal (we just make it public for documentation)
@@ -2844,9 +2845,26 @@ def __init__(self, mapping):
28442845
super().__init__(mapping)
28452846
self._cache = _ColorCache()
28462847

2848+
def __getitem__(self, key):
2849+
"""
2850+
Get a color. Translates ``grey`` into ``gray`` and supports retrieving
2851+
colors "on-the-fly" from registered colormaps and color cycles.
2852+
2853+
* For a colormap, use e.g. ``color=('Blues', 0.8)``.
2854+
The number is the colormap index, and must be between 0 and 1.
2855+
* For a color cycle, use e.g. ``color=('colorblind', 2)``.
2856+
The number is the color list index.
2857+
2858+
This works with anywhere that colors are used in matplotlib, for example
2859+
as ``'color'``, ``'edgecolor'``, or ``'facecolor'`` arguments.
2860+
"""
2861+
if isinstance(key, str):
2862+
key = re.sub(r'\bgrey[0-9]?\b', 'gray', key)
2863+
return super().__getitem__(key)
2864+
28472865
def __setitem__(self, key, value):
28482866
"""
2849-
Add a color to the database and clear the cache.
2867+
Add a color and clear the cache.
28502868
"""
28512869
if not isinstance(key, str):
28522870
raise ValueError(f'Invalid color name {key!r}. Must be string.')
@@ -2855,25 +2873,15 @@ def __setitem__(self, key, value):
28552873

28562874
def __delitem__(self, key):
28572875
"""
2858-
Delete a color from the database and clear the cache.
2876+
Delete a color and clear the cache.
28592877
"""
28602878
super().__delitem__(key)
28612879
self.cache.clear()
28622880

28632881
@property
28642882
def cache(self):
2865-
"""
2866-
A special dictionary subclass capable of retrieving colors
2867-
"on-the-fly" from registered colormaps and color cycles.
2868-
2869-
* For a colormap, use e.g. ``color=('Blues', 0.8)``.
2870-
The number is the colormap index, and must be between 0 and 1.
2871-
* For a color cycle, use e.g. ``color=('colorblind', 2)``.
2872-
The number is the color list index.
2873-
2874-
This works with anywhere that colors are used in matplotlib, for example
2875-
as ``'color'``, ``'edgecolor'``, or ``'facecolor'`` arguments.
2876-
"""
2883+
# Matplotlib uses 'cache' but treat '_cache' as synonym
2884+
# to guard against private API changes.
28772885
return self._cache
28782886

28792887

0 commit comments

Comments
 (0)