Skip to content

Commit 9e7b1c0

Browse files
committed
colors.rgb_to_hsv: don't divide by zero
1 parent 1389499 commit 9e7b1c0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/matplotlib/colors.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1006,19 +1006,21 @@ def rgb_to_hsv(arr):
10061006
convert rgb values in a numpy array to hsv values
10071007
input and output arrays should have shape (M,N,3)
10081008
"""
1009-
out = np.empty_like(arr)
1009+
out = np.zeros_like(arr)
10101010
arr_max = arr.max(-1)
1011+
ipos = arr_max > 0
10111012
delta = arr.ptp(-1)
1012-
s = delta / arr_max
1013-
s[delta==0] = 0
1013+
s = np.zeros_like(delta)
1014+
s[ipos] = delta[ipos] / arr_max[ipos]
1015+
ipos = delta > 0
10141016
# red is max
1015-
idx = (arr[:,:,0] == arr_max)
1017+
idx = (arr[:,:,0] == arr_max) & ipos
10161018
out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx]
10171019
# green is max
1018-
idx = (arr[:,:,1] == arr_max)
1020+
idx = (arr[:,:,1] == arr_max) & ipos
10191021
out[idx, 0] = 2. + (arr[idx, 2] - arr[idx, 0] ) / delta[idx]
10201022
# blue is max
1021-
idx = (arr[:,:,2] == arr_max)
1023+
idx = (arr[:,:,2] == arr_max) & ipos
10221024
out[idx, 0] = 4. + (arr[idx, 0] - arr[idx, 1] ) / delta[idx]
10231025
out[:,:,0] = (out[:,:,0]/6.0) % 1.0
10241026
out[:,:,1] = s

0 commit comments

Comments
 (0)