Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions ultraplot/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion ultraplot/tests/test_colormap.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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"]
Loading