Skip to content

Commit 1b0374a

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR matplotlib#32058: MNT: Fix handling of ints in hsv_to_rgb()
1 parent f5ccc77 commit 1b0374a

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3729,11 +3729,10 @@ def hsv_to_rgb(hsv):
37293729
f"shape {hsv.shape} was found.")
37303730

37313731
in_shape = hsv.shape
3732-
hsv = np.array(
3733-
hsv, copy=False,
3734-
dtype=np.promote_types(hsv.dtype, np.float32), # Don't work on ints.
3735-
ndmin=2, # In case input was 1D.
3736-
)
3732+
# ensure numerics are done at least on float32; ints are cast as well
3733+
hsv = np.asarray(hsv, dtype=np.promote_types(hsv.dtype, np.float32))
3734+
if hsv.ndim == 1:
3735+
hsv = np.expand_dims(hsv, axis=0) # ensure hsv is 2D
37373736

37383737
h = hsv[..., 0]
37393738
s = hsv[..., 1]

lib/matplotlib/tests/test_colors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,11 @@ def test_rgb_to_hsv_int():
946946
assert_array_equal(mcolors.rgb_to_hsv((0, 1, 0)), (1/3, 1, 1)) # green
947947

948948

949+
def test_hsv_to_rgb_int():
950+
# Test that int hsv values (still range 0-1) are processed correctly.
951+
assert_array_equal(mcolors.hsv_to_rgb((0, 1, 1)), (1, 0, 0)) # red
952+
953+
949954
def test_autoscale_masked():
950955
# Test for #2336. Previously fully masked data would trigger a ValueError.
951956
data = np.ma.masked_all((12, 20))

0 commit comments

Comments
 (0)