Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea8fa7b

Browse files
committed
Merge pull request #3092 from ga7g08/fix-issue-3079
Adds check that rgb sequence is of length 3
2 parents 62bae67 + dcf9a6e commit ea8fa7b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/matplotlib/colors.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,22 @@ def to_rgba(self, arg, alpha=None):
349349
try:
350350
if not cbook.is_string_like(arg) and cbook.iterable(arg):
351351
if len(arg) == 4:
352-
if [x for x in arg if (float(x) < 0) or (x > 1)]:
353-
# This will raise TypeError if x is not a number.
352+
if any(float(x) < 0 or x > 1 for x in arg):
354353
raise ValueError(
355354
'number in rbga sequence outside 0-1 range')
356355
if alpha is None:
357356
return tuple(arg)
358357
if alpha < 0.0 or alpha > 1.0:
359358
raise ValueError("alpha must be in range 0-1")
360359
return arg[0], arg[1], arg[2], alpha
361-
r, g, b = arg[:3]
362-
if [x for x in (r, g, b) if (float(x) < 0) or (x > 1)]:
360+
if len(arg) == 3:
361+
r, g, b = arg
362+
if any(float(x) < 0 or x > 1 for x in arg):
363+
raise ValueError(
364+
'number in rbg sequence outside 0-1 range')
365+
else:
363366
raise ValueError(
364-
'number in rbg sequence outside 0-1 range')
367+
'length of rgba sequence should be either 3 or 4')
365368
else:
366369
r, g, b = self.to_rgb(arg)
367370
if alpha is None:

0 commit comments

Comments
 (0)