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 b55f226

Browse filesBrowse files
committed
Reject floatlike strings in mcolors.to_rgba.
1 parent 9000e58 commit b55f226
Copy full SHA for b55f226

File tree

Expand file treeCollapse file tree

2 files changed

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

2 files changed

+10
-5
lines changed

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,13 @@ def _to_rgba_no_colorcycle(c, alpha=None):
185185
pass
186186
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
187187
# tuple color.
188-
# Python 2.7 / numpy 1.6 apparently require this to return builtin floats,
189-
# not numpy floats.
190-
try:
191-
c = tuple(map(float, c))
192-
except TypeError:
188+
if not np.can_cast(np.min_scalar_type(c), float):
189+
# We need to test explicitly as `map(float, ...)`, `np.array(...,
190+
# float)` and `np.array(...).astype(float)` all happily convert "0.5"
191+
# to 0.5.
192+
# 0.5.
193193
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
194+
c = tuple(map(float, c))
194195
if len(c) not in [3, 4]:
195196
raise ValueError("RGBA sequence should have length 3 or 4")
196197
if len(c) == 3 and alpha is None:

‎lib/matplotlib/tests/test_colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,10 @@ def test_cn():
658658
def test_conversions():
659659
# to_rgba_array("none") returns a (0, 4) array.
660660
assert_array_equal(mcolors.to_rgba_array("none"), np.zeros((0, 4)))
661+
# a list of grayscale levels, not a single color.
662+
assert_array_equal(
663+
mcolors.to_rgba_array([".2", ".5", ".8"]),
664+
np.vstack([mcolors.to_rgba(c) for c in [".2", ".5", ".8"]]))
661665
# alpha is properly set.
662666
assert_equal(mcolors.to_rgba((1, 1, 1), .5), (1, 1, 1, .5))
663667
assert_equal(mcolors.to_rgba(".1", .5), (.1, .1, .1, .5))

0 commit comments

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