diff --git a/src/cmap/_colormap.py b/src/cmap/_colormap.py index c20371cbd..4821f2895 100644 --- a/src/cmap/_colormap.py +++ b/src/cmap/_colormap.py @@ -1161,7 +1161,7 @@ def reversed(self) -> ColorStops: rev_lutfunc = partial(self._reverser, lut_func) return type(self)(lut_func=rev_lutfunc) # invert the positions in the stops - rev_stops = self._stops[::-1] + rev_stops = self._stops[::-1].copy() rev_stops[:, 0] = 1 - rev_stops[:, 0] return type(self)(rev_stops, interpolation=self._interpolation) diff --git a/tests/test_colormap.py b/tests/test_colormap.py index d1550136b..62911f808 100644 --- a/tests/test_colormap.py +++ b/tests/test_colormap.py @@ -90,6 +90,15 @@ def test_colorstops() -> None: assert reversed(cmap.color_stops) == ColorStops.parse(["b", "m", "r"]) +def test_colorstops_reversed_does_not_mutate_source() -> None: + stops = ColorStops.parse(["red", "green", "blue"]) + before = np.asarray(stops).copy() + + stops.reversed() + + npt.assert_array_equal(np.asarray(stops), before) + + def test_colormap_copy() -> None: """Test Colormap copy.""" import pickle