From 5fd8c7609ccfa1e0d55af09b4ac2f55cb1347170 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 31 May 2016 16:42:10 -0700 Subject: [PATCH] Fix conversion of string grays with alpha. --- lib/matplotlib/colors.py | 9 ++++----- lib/matplotlib/tests/test_colors.py | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index bcfe5579f58f..3fa0a1734ccf 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -146,11 +146,12 @@ def _to_rgba_no_colorcycle(c, alpha=None): If `alpha` is not `None`, it forces the alpha value. """ orig_c = c - if isinstance(c, six.string_types) and c.lower() == "none": - return (0., 0., 0., 0.) if isinstance(c, six.string_types): + if c.lower() == "none": + return (0., 0., 0., 0.) # Named color. try: + # This may turn c into a non-string, so we check again below. c = _colors_full_map[c.lower()] except KeyError: pass @@ -161,7 +162,6 @@ def _to_rgba_no_colorcycle(c, alpha=None): return (tuple(int(n, 16) / 255 for n in [c[1:3], c[3:5], c[5:7]]) + (alpha if alpha is not None else 1.,)) - if isinstance(c, six.string_types): # hex color with alpha. match = re.match(r"\A#[a-fA-F0-9]{8}\Z", c) if match: @@ -170,10 +170,9 @@ def _to_rgba_no_colorcycle(c, alpha=None): if alpha is not None: color[-1] = alpha return tuple(color) - if isinstance(c, six.string_types): # string gray. try: - return (float(c),) * 3 + (1.,) + 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/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index f24f4bc64f77..d9fd93f40a47 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -621,6 +621,7 @@ def test_conversions(): assert_array_equal(mcolors.to_rgba_array("none"), np.zeros((0, 4))) # alpha is properly set. assert_equal(mcolors.to_rgba((1, 1, 1), .5), (1, 1, 1, .5)) + assert_equal(mcolors.to_rgba(".1", .5), (.1, .1, .1, .5)) # builtin round differs between py2 and py3. assert_equal(mcolors.to_hex((.7, .7, .7)), "#b2b2b2") # hex roundtrip.