diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dba830298..9161ba4d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Fixed +- Fix `plotly.colors.sample_colorscale` raising opaque errors for sample points outside the colorscale's range; out-of-range points now raise a clear `PlotlyError`. ## [7.2.0] - 2026-11-24 diff --git a/_plotly_utils/colors/__init__.py b/_plotly_utils/colors/__init__.py index e3a304c9bb..8a31023499 100644 --- a/_plotly_utils/colors/__init__.py +++ b/_plotly_utils/colors/__init__.py @@ -911,6 +911,11 @@ def sample_colorscale(colorscale, samplepoints, low=0.0, high=1.0, colortype="rg ] elif isinstance(samplepoints, float): samplepoints = [samplepoints] + else: + samplepoints = list(samplepoints) + + if any(point < 0 or point > 1 for point in samplepoints): + raise exceptions.PlotlyError("Samplepoints must be between 0 and 1.") sampled_colors = [] for point in samplepoints: diff --git a/tests/test_plotly_utils/colors/test_sample_colorscale.py b/tests/test_plotly_utils/colors/test_sample_colorscale.py new file mode 100644 index 0000000000..bb00448376 --- /dev/null +++ b/tests/test_plotly_utils/colors/test_sample_colorscale.py @@ -0,0 +1,12 @@ +import pytest + +from plotly.colors import sample_colorscale +from plotly.exceptions import PlotlyError + + +@pytest.mark.parametrize("point", [-0.25, 1.25]) +def test_sample_colorscale_rejects_out_of_range_points(point): + colorscale = [[0.0, "rgb(0, 0, 0)"], [1.0, "rgb(255, 255, 255)"]] + + with pytest.raises(PlotlyError, match="must be between 0 and 1"): + sample_colorscale(colorscale, [point])