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 1c752f7

Browse filesBrowse files
committed
Merge pull request #7896 from anntzer/to_rgba-reject-floatlike-strings
Reject floatlike strings in mcolors.to_rgba.
1 parent d778f54 commit 1c752f7
Copy full SHA for 1c752f7

File tree

Expand file treeCollapse file tree

2 files changed

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

2 files changed

+11
-5
lines changed

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,14 @@ def _to_rgba_no_colorcycle(c, alpha=None):
186186
pass
187187
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
188188
# tuple color.
189-
# Python 2.7 / numpy 1.6 apparently require this to return builtin floats,
190-
# not numpy floats.
191-
try:
192-
c = tuple(map(float, c))
193-
except TypeError:
189+
c = np.array(c)
190+
if not np.can_cast(c.dtype, float) or c.ndim != 1:
191+
# Test the dtype explicitly as `map(float, ...)`, `np.array(...,
192+
# float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
193+
# Test dimensionality to reject single floats.
194194
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
195+
# Return a tuple to prevent the cached value from being modified.
196+
c = tuple(c.astype(float))
195197
if len(c) not in [3, 4]:
196198
raise ValueError("RGBA sequence should have length 3 or 4")
197199
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
@@ -645,6 +645,10 @@ def test_cn():
645645
def test_conversions():
646646
# to_rgba_array("none") returns a (0, 4) array.
647647
assert_array_equal(mcolors.to_rgba_array("none"), np.zeros((0, 4)))
648+
# a list of grayscale levels, not a single color.
649+
assert_array_equal(
650+
mcolors.to_rgba_array([".2", ".5", ".8"]),
651+
np.vstack([mcolors.to_rgba(c) for c in [".2", ".5", ".8"]]))
648652
# alpha is properly set.
649653
assert_equal(mcolors.to_rgba((1, 1, 1), .5), (1, 1, 1, .5))
650654
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.