Skip to content

Commit 302349d

Browse files
committed
Validate sample_colorscale sample points within range
1 parent 2b5fddb commit 302349d

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## Unreleased
66

7+
### Fixed
8+
- 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`.
79

810
## [7.2.0] - 2026-11-24
911

‎_plotly_utils/colors/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,11 @@ def sample_colorscale(colorscale, samplepoints, low=0.0, high=1.0, colortype="rg
911911
]
912912
elif isinstance(samplepoints, float):
913913
samplepoints = [samplepoints]
914+
else:
915+
samplepoints = list(samplepoints)
916+
917+
if any(point < 0 or point > 1 for point in samplepoints):
918+
raise exceptions.PlotlyError("Samplepoints must be between 0 and 1.")
914919

915920
sampled_colors = []
916921
for point in samplepoints:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
from plotly.colors import sample_colorscale
4+
from plotly.exceptions import PlotlyError
5+
6+
7+
@pytest.mark.parametrize("point", [-0.25, 1.25])
8+
def test_sample_colorscale_rejects_out_of_range_points(point):
9+
colorscale = [[0.0, "rgb(0, 0, 0)"], [1.0, "rgb(255, 255, 255)"]]
10+
11+
with pytest.raises(PlotlyError, match="must be between 0 and 1"):
12+
sample_colorscale(colorscale, [point])

0 commit comments

Comments
 (0)