diff --git a/_plotly_utils/colors/__init__.py b/_plotly_utils/colors/__init__.py index e3a304c9bb..9d0666f731 100644 --- a/_plotly_utils/colors/__init__.py +++ b/_plotly_utils/colors/__init__.py @@ -694,6 +694,9 @@ def n_colors(lowcolor, highcolor, n_colors, colortype="tuple"): lowcolor = unlabel_rgb(lowcolor) highcolor = unlabel_rgb(highcolor) + if n_colors == 1: + return [lowcolor] + diff_0 = float(highcolor[0] - lowcolor[0]) incr_0 = diff_0 / (n_colors - 1) diff_1 = float(highcolor[1] - lowcolor[1]) @@ -906,9 +909,13 @@ def sample_colorscale(colorscale, samplepoints, low=0.0, high=1.0, colortype="rg colors = validate_colors(colors, colortype="tuple") if isinstance(samplepoints, int): - samplepoints = [ - low + idx / (samplepoints - 1) * (high - low) for idx in range(samplepoints) - ] + if samplepoints == 1: + samplepoints = [low] + else: + samplepoints = [ + low + idx / (samplepoints - 1) * (high - low) + for idx in range(samplepoints) + ] elif isinstance(samplepoints, float): samplepoints = [samplepoints] diff --git a/tests/test_core/test_colors/test_colors.py b/tests/test_core/test_colors/test_colors.py index f40e1e31d8..6be435dd9f 100644 --- a/tests/test_core/test_colors/test_colors.py +++ b/tests/test_core/test_colors/test_colors.py @@ -250,3 +250,15 @@ def test_n_colors(self): ] self.assertEqual(generated_colorscale, expected_colorscale) + + def test_sample_colorscale_single_color(self): + result = colors.sample_colorscale("Viridis", 1) + assert len(result) == 1 + assert result[0] == "rgb(68, 1, 84)" + + + def test_n_colors_single_color(self): + result = colors.n_colors( + (0, 0, 0), (255, 255, 255), 1 + ) + assert result == [(0, 0, 0)]