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

Skip to content

Fix conversion of string grays with alpha. #6517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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))
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down