Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1e35d8d

Browse filesBrowse files
zblztacaswell
authored andcommitted
Merge pull request #6517 from anntzer/fix-string-gray-conversion
Fix conversion of string grays with alpha.
1 parent 9ebdaed commit 1e35d8d
Copy full SHA for 1e35d8d

File tree

Expand file treeCollapse file tree

2 files changed

+5
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+5
-5
lines changed

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,12 @@ def _to_rgba_no_colorcycle(c, alpha=None):
146146
If `alpha` is not `None`, it forces the alpha value.
147147
"""
148148
orig_c = c
149-
if isinstance(c, six.string_types) and c.lower() == "none":
150-
return (0., 0., 0., 0.)
151149
if isinstance(c, six.string_types):
150+
if c.lower() == "none":
151+
return (0., 0., 0., 0.)
152152
# Named color.
153153
try:
154+
# This may turn c into a non-string, so we check again below.
154155
c = _colors_full_map[c.lower()]
155156
except KeyError:
156157
pass
@@ -161,7 +162,6 @@ def _to_rgba_no_colorcycle(c, alpha=None):
161162
return (tuple(int(n, 16) / 255
162163
for n in [c[1:3], c[3:5], c[5:7]])
163164
+ (alpha if alpha is not None else 1.,))
164-
if isinstance(c, six.string_types):
165165
# hex color with alpha.
166166
match = re.match(r"\A#[a-fA-F0-9]{8}\Z", c)
167167
if match:
@@ -170,10 +170,9 @@ def _to_rgba_no_colorcycle(c, alpha=None):
170170
if alpha is not None:
171171
color[-1] = alpha
172172
return tuple(color)
173-
if isinstance(c, six.string_types):
174173
# string gray.
175174
try:
176-
return (float(c),) * 3 + (1.,)
175+
return (float(c),) * 3 + (alpha if alpha is not None else 1.,)
177176
except ValueError:
178177
pass
179178
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))

‎lib/matplotlib/tests/test_colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ def test_conversions():
608608
assert_array_equal(mcolors.to_rgba_array("none"), np.zeros((0, 4)))
609609
# alpha is properly set.
610610
assert_equal(mcolors.to_rgba((1, 1, 1), .5), (1, 1, 1, .5))
611+
assert_equal(mcolors.to_rgba(".1", .5), (.1, .1, .1, .5))
611612
# builtin round differs between py2 and py3.
612613
assert_equal(mcolors.to_hex((.7, .7, .7)), "#b2b2b2")
613614
# hex roundtrip.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.