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

Reject floatlike strings in mcolors.to_rgba. #7896

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
Jan 22, 2017
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
Reject floatlike strings in mcolors.to_rgba.
  • Loading branch information
anntzer committed Jan 20, 2017
commit 0cad64f9a99c55b50183f9287c2ddb8ada73fc8b
12 changes: 7 additions & 5 deletions 12 lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,14 @@ def _to_rgba_no_colorcycle(c, alpha=None):
pass
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
# tuple color.
# Python 2.7 / numpy 1.6 apparently require this to return builtin floats,
# not numpy floats.
try:
c = tuple(map(float, c))
except TypeError:
c = np.array(c)
if not np.can_cast(c.dtype, float) or c.ndim != 1:
# Test the dtype explicitly as `map(float, ...)`, `np.array(...,
# float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
# Test dimensionality to reject single floats.
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
# Return a tuple to prevent the cached value from being modified.
c = tuple(c.astype(float))
if len(c) not in [3, 4]:
raise ValueError("RGBA sequence should have length 3 or 4")
if len(c) == 3 and alpha is None:
Expand Down
4 changes: 4 additions & 0 deletions 4 lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,10 @@ def test_cn():
def test_conversions():
# to_rgba_array("none") returns a (0, 4) array.
assert_array_equal(mcolors.to_rgba_array("none"), np.zeros((0, 4)))
# a list of grayscale levels, not a single color.
assert_array_equal(
mcolors.to_rgba_array([".2", ".5", ".8"]),
np.vstack([mcolors.to_rgba(c) for c in [".2", ".5", ".8"]]))
# 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))
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.