Skip to content

Commit 0f70a22

Browse files
Raise ValueError for RGB values outside the [0, 1] range in rgb_to_hsv function (#28948)
* initial error correction * Remove unintentional white-space * Reorder closer to original * Remove unwanted whitespace * flake8 compliance * Improved Error messages * Update lib/matplotlib/colors.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Update lib/matplotlib/colors.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Update lib/matplotlib/colors.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * remove unnecessary arr_min calculation --------- Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
1 parent 592c123 commit 0f70a22

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/matplotlib/colors.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,8 +3097,22 @@ def rgb_to_hsv(arr):
30973097
dtype=np.promote_types(arr.dtype, np.float32), # Don't work on ints.
30983098
ndmin=2, # In case input was 1D.
30993099
)
3100+
31003101
out = np.zeros_like(arr)
31013102
arr_max = arr.max(-1)
3103+
# Check if input is in the expected range
3104+
if np.any(arr_max > 1):
3105+
raise ValueError(
3106+
"Input array must be in the range [0, 1]. "
3107+
f"Found a maximum value of {arr_max.max()}"
3108+
)
3109+
3110+
if arr.min() < 0:
3111+
raise ValueError(
3112+
"Input array must be in the range [0, 1]. "
3113+
f"Found a minimum value of {arr.min()}"
3114+
)
3115+
31023116
ipos = arr_max > 0
31033117
delta = np.ptp(arr, -1)
31043118
s = np.zeros_like(delta)

0 commit comments

Comments
 (0)