From 5f2439e5194ef8bb8a3c24951119b6ece8db7336 Mon Sep 17 00:00:00 2001 From: myyc Date: Thu, 7 Jul 2016 14:02:00 +0200 Subject: [PATCH 1/5] partially fixing a bug that caused 'YeXXXX' hex colours to be recognised as floats in scientific notation. fixed for Y>1, needs some decision in the Y=0,Y=1 cases. --- lib/matplotlib/colors.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 511f46d18710..380f6d27c110 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -171,7 +171,11 @@ def _to_rgba_no_colorcycle(c, alpha=None): return tuple(color) # string gray. try: - return (float(c),) * 3 + (alpha if alpha is not None else 1.,) + fc = float(c) + if fc <= 1: + return (float(fc),) * 3 + (alpha if alpha is not None else 1.,) + else: + raise ValueError except ValueError: pass raise ValueError("Invalid RGBA argument: {!r}".format(orig_c)) From a9703b0e3a3a75dd0d35aa3dab33135705abc169 Mon Sep 17 00:00:00 2001 From: myyc Date: Thu, 7 Jul 2016 15:36:55 +0200 Subject: [PATCH 2/5] Moving the fix to rcsetup.validate_color --- lib/matplotlib/colors.py | 6 +----- lib/matplotlib/rcsetup.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 380f6d27c110..511f46d18710 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -171,11 +171,7 @@ def _to_rgba_no_colorcycle(c, alpha=None): return tuple(color) # string gray. try: - fc = float(c) - if fc <= 1: - return (float(fc),) * 3 + (alpha if alpha is not None else 1.,) - else: - raise ValueError + return (float(c),) * 3 + (alpha if alpha is not None else 1.,) except ValueError: pass raise ValueError("Invalid RGBA argument: {!r}".format(orig_c)) diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 200518e80070..873adc75d536 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -367,12 +367,19 @@ def validate_color(s): return 'None' except AttributeError: pass + + if isinstance(s, six.string_types): + if len(s) == 7 or len(s) == 9: + if is_color_like(s): + return s + if len(s) == 6 or len(s) == 8: + stmp = '#' + s + if is_color_like(stmp): + return stmp + if is_color_like(s): return s - stmp = '#' + s - if is_color_like(stmp): - return stmp # If it is still valid, it must be a tuple. colorarg = s msg = '' From 0934f8eccf2e35b6a4105d6a5767727e95a83bc7 Mon Sep 17 00:00:00 2001 From: myyc Date: Thu, 7 Jul 2016 16:54:31 +0200 Subject: [PATCH 3/5] adding a test + importing cycler from 'matplotlib' instead of 'cycler' --- lib/matplotlib/tests/test_colors.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 072959eb8ff7..0daf3cd4b151 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -18,7 +18,7 @@ from numpy.testing.utils import assert_array_equal, assert_array_almost_equal from nose.plugins.skip import SkipTest -from cycler import cycler +from matplotlib import cycler import matplotlib import matplotlib.colors as mcolors import matplotlib.cm as cm @@ -615,6 +615,13 @@ def test_cn(): assert mcolors.to_hex("C0") == '#0343df' assert mcolors.to_hex("C1") == '#ff0000' + matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['8e4585', 'r']) + + assert mcolors.to_hex("C0") == '#8e4585' + # if '8e4585' gets parsed as a float before it gets detected as a hex colour it will be interpreted as a very + # large number. this mustn't happen. + assert mcolors.to_rgb("C0")[0] != np.inf + def test_conversions(): # to_rgba_array("none") returns a (0, 4) array. From a851860b20586f068fa864fa348318b03ba684c1 Mon Sep 17 00:00:00 2001 From: myyc Date: Thu, 7 Jul 2016 17:00:00 +0200 Subject: [PATCH 4/5] removing unnecessary check --- lib/matplotlib/rcsetup.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 873adc75d536..829646b8a784 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -369,9 +369,6 @@ def validate_color(s): pass if isinstance(s, six.string_types): - if len(s) == 7 or len(s) == 9: - if is_color_like(s): - return s if len(s) == 6 or len(s) == 8: stmp = '#' + s if is_color_like(stmp): From 40e5a0a0d52a585d0d2bc550bf09a3ed91a20e53 Mon Sep 17 00:00:00 2001 From: myyc Date: Mon, 11 Jul 2016 11:52:36 +0200 Subject: [PATCH 5/5] - fix comment length --- lib/matplotlib/tests/test_colors.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 0daf3cd4b151..4f12766b7def 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -618,8 +618,9 @@ def test_cn(): matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['8e4585', 'r']) assert mcolors.to_hex("C0") == '#8e4585' - # if '8e4585' gets parsed as a float before it gets detected as a hex colour it will be interpreted as a very - # large number. this mustn't happen. + # if '8e4585' gets parsed as a float before it gets detected as a hex + # colour it will be interpreted as a very large number. + # this mustn't happen. assert mcolors.to_rgb("C0")[0] != np.inf