From 4e114ad5a5c152f7259ab81a44d411b87a167744 Mon Sep 17 00:00:00 2001 From: Gregory Ashton Date: Wed, 28 May 2014 20:40:45 +0100 Subject: [PATCH 1/3] Adds check that rgb sequence is of length 3 --- lib/matplotlib/colors.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 74c463c41253..73d7da6e4ba8 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -358,10 +358,14 @@ def to_rgba(self, arg, alpha=None): if alpha < 0.0 or alpha > 1.0: raise ValueError("alpha must be in range 0-1") return arg[0], arg[1], arg[2], alpha - r, g, b = arg[:3] - if [x for x in (r, g, b) if (float(x) < 0) or (x > 1)]: + if len(arg) == 3: + r, g, b = arg + if [x for x in (r, g, b) if (float(x) < 0) or (x > 1)]: + raise ValueError( + 'number in rbg sequence outside 0-1 range') + else: raise ValueError( - 'number in rbg sequence outside 0-1 range') + 'length of rgba sequence should be either 3 or 4') else: r, g, b = self.to_rgb(arg) if alpha is None: From e5700ba7a041a43f9b9a0e7738f212251dd2edc9 Mon Sep 17 00:00:00 2001 From: Gregory Ashton Date: Wed, 28 May 2014 22:07:32 +0100 Subject: [PATCH 2/3] Improves check of arg range --- lib/matplotlib/colors.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 73d7da6e4ba8..25d38a8f7d6b 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -349,8 +349,7 @@ def to_rgba(self, arg, alpha=None): try: if not cbook.is_string_like(arg) and cbook.iterable(arg): if len(arg) == 4: - if [x for x in arg if (float(x) < 0) or (x > 1)]: - # This will raise TypeError if x is not a number. + if any(float(x) < 0. or float(x) > 1. for x in arg): raise ValueError( 'number in rbga sequence outside 0-1 range') if alpha is None: @@ -360,7 +359,7 @@ def to_rgba(self, arg, alpha=None): return arg[0], arg[1], arg[2], alpha if len(arg) == 3: r, g, b = arg - if [x for x in (r, g, b) if (float(x) < 0) or (x > 1)]: + if any(float(x) < 0. or float(x) > 1. for x in arg): raise ValueError( 'number in rbg sequence outside 0-1 range') else: From dcf9a6e5988762df567ae4c84828f194516a4a46 Mon Sep 17 00:00:00 2001 From: Gregory Ashton Date: Thu, 29 May 2014 08:38:06 +0100 Subject: [PATCH 3/3] Removes unnecessary float conversions --- lib/matplotlib/colors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 25d38a8f7d6b..dbc96197c134 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -349,7 +349,7 @@ def to_rgba(self, arg, alpha=None): try: if not cbook.is_string_like(arg) and cbook.iterable(arg): if len(arg) == 4: - if any(float(x) < 0. or float(x) > 1. for x in arg): + if any(float(x) < 0 or x > 1 for x in arg): raise ValueError( 'number in rbga sequence outside 0-1 range') if alpha is None: @@ -359,7 +359,7 @@ def to_rgba(self, arg, alpha=None): return arg[0], arg[1], arg[2], alpha if len(arg) == 3: r, g, b = arg - if any(float(x) < 0. or float(x) > 1. for x in arg): + if any(float(x) < 0 or x > 1 for x in arg): raise ValueError( 'number in rbg sequence outside 0-1 range') else: