Skip to content

Commit 216fca2

Browse files
authored
Merge pull request #4110 from AaronStiff/4105_n_colors_clipping
n_colors: constrain colors between 0 and 255
2 parents 3b613fc + 5f13927 commit 216fca2

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1212
- Fixed issue with shapes and annotations plotting on the wrong y axis when supplied with a specific axis in the `yref` parameter [[#4177](https://github.com/plotly/plotly.py/pull/4177)]
1313
- Remove `use_2to3` setuptools arg, which is invalid in the latest Python and setuptools versions [[#4206](https://github.com/plotly/plotly.py/pull/4206)]
1414
- Fix [#4066](https://github.com/plotly/plotly.py/issues/4066) JupyterLab v4 giving tiny default graph height [[#4227](https://github.com/plotly/plotly.py/pull/4227)]
15+
- Fixed issue with `colors.n_colors` where generated RGB color values were not being constrained to stay between 0 and 255 [[#4110](https://github.com/plotly/plotly.py/pull/4110)]
1516

1617
## [5.14.1] - 2023-04-05
1718

Diff for: packages/python/plotly/_plotly_utils/colors/__init__.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,19 @@ def n_colors(lowcolor, highcolor, n_colors, colortype="tuple"):
694694
incr_2 = diff_2 / (n_colors - 1)
695695
list_of_colors = []
696696

697+
def _constrain_color(c):
698+
if c > 255.0:
699+
return 255.0
700+
elif c < 0.0:
701+
return 0.0
702+
else:
703+
return c
704+
697705
for index in range(n_colors):
698706
new_tuple = (
699-
lowcolor[0] + (index * incr_0),
700-
lowcolor[1] + (index * incr_1),
701-
lowcolor[2] + (index * incr_2),
707+
_constrain_color(lowcolor[0] + (index * incr_0)),
708+
_constrain_color(lowcolor[1] + (index * incr_1)),
709+
_constrain_color(lowcolor[2] + (index * incr_2)),
702710
)
703711
list_of_colors.append(new_tuple)
704712

Diff for: packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py

+27
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,30 @@ def test_sample_colorscale(self):
210210
colors.sample_colorscale("TuRbId_r", 12),
211211
colors.sequential.turbid_r,
212212
)
213+
214+
def test_n_colors(self):
215+
# test that n_colors constrains values to between 0 and 255
216+
generated_colorscale = colors.n_colors(
217+
lowcolor="rgb(255,0,0)",
218+
highcolor="rgb(0,255,0)",
219+
n_colors=14,
220+
colortype="rgb",
221+
)
222+
expected_colorscale = [
223+
"rgb(255.0, 0.0, 0.0)",
224+
"rgb(235.3846153846154, 19.615384615384617, 0.0)",
225+
"rgb(215.76923076923077, 39.23076923076923, 0.0)",
226+
"rgb(196.15384615384613, 58.846153846153854, 0.0)",
227+
"rgb(176.53846153846155, 78.46153846153847, 0.0)",
228+
"rgb(156.9230769230769, 98.07692307692308, 0.0)",
229+
"rgb(137.3076923076923, 117.69230769230771, 0.0)",
230+
"rgb(117.69230769230768, 137.30769230769232, 0.0)",
231+
"rgb(98.07692307692307, 156.92307692307693, 0.0)",
232+
"rgb(78.46153846153845, 176.53846153846155, 0.0)",
233+
"rgb(58.84615384615384, 196.15384615384616, 0.0)",
234+
"rgb(39.230769230769226, 215.76923076923077, 0.0)",
235+
"rgb(19.615384615384585, 235.38461538461542, 0.0)",
236+
"rgb(0.0, 255.0, 0.0)",
237+
]
238+
239+
self.assertEqual(generated_colorscale, expected_colorscale)

0 commit comments

Comments
 (0)