diff --git a/ultraplot/colors.py b/ultraplot/colors.py index 08e4a655d..0604b715b 100644 --- a/ultraplot/colors.py +++ b/ultraplot/colors.py @@ -536,7 +536,7 @@ def _make_segment_data(values, coords=None, ratios=None): coords = np.atleast_1d(coords) if ratios is not None: warnings._warn_ultraplot( - f"Segment coordinates were provided, ignoring " f"ratios={ratios!r}." + f"Segment coordinates were provided, ignoring ratios={ratios!r}." ) if len(coords) != len(values) or coords[0] != 0 or coords[-1] != 1: raise ValueError(f"Coordinates must range from 0 to 1, got {coords!r}.") @@ -793,8 +793,7 @@ def _get_data(self, ext, alpha=True): data = "\n".join(" ".join(f"{num:0.6f}" for num in line) for line in data) else: raise ValueError( - f"Invalid extension {ext!r}. Options are: " - "'hex', 'txt', 'rgb', 'rgba'." + f"Invalid extension {ext!r}. Options are: 'hex', 'txt', 'rgb', 'rgba'." ) return data @@ -3107,6 +3106,29 @@ def __init__(self, kwargs): # These are converted to ultraplot's own colormap objects # on the fly when they are first accessed. + def __contains__(self, key): + # Work around for mpl 3.11.2 as it is not allowed to + # this piece of code will allow look up to both occur in our flattened representation (e.g. 'fire') as well as the original "Fire" + try: + key = self._translate_deprecated(key) + key = self._translate_key(key, mirror=True) + except (KeyError, TypeError, ValueError): + return False + + # Directly registered colormap + if self._has_item(key): + return True + + # Dynamically generated shifted colormap + if key.endswith("_s"): + key = key.removesuffix("_s") + + # Dynamically generated reversed colormap + if key.endswith("_r"): + key = key.removesuffix("_r") + + return self._has_item(key) + def _translate_deprecated(self, key): """ Check if a colormap has been deprecated. diff --git a/ultraplot/tests/test_colormap.py b/ultraplot/tests/test_colormap.py index 882b5aaa0..1adeab277 100644 --- a/ultraplot/tests/test_colormap.py +++ b/ultraplot/tests/test_colormap.py @@ -1,4 +1,7 @@ -import ultraplot as uplt, pytest, numpy as np +import numpy as np +import pytest + +import ultraplot as uplt def test_colormap_reversal(): @@ -10,3 +13,13 @@ def test_colormap_reversal(): assert np.allclose( cmap(i), cmap_r(255 - i) ), f"Reversed colormap mismatch at index {i}" + + +def test_colormap_name_sensitivty(): + assert "fire" in uplt.colormaps + assert "Fire" in uplt.colormaps + assert "fire_r" in uplt.colormaps + assert "fire_s" in uplt.colormaps + + with pytest.raises(KeyError): + uplt.colormaps["should_not_exist"]