8000 Fix string numbers in to_rgba() and is_color_like() by timhoffm · Pull Request #13913 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix string numbers in to_rgba() and is_color_like() #13913

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
Apr 15, 2019
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
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/2019-04-13-TH.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
API changes
```````````

`matplotlib.color.is_colorlike()` used to return True for all string
representations of floats. However, only those with values in 0-1 are valid
colors (representing grayscale values). ``is_colorlike()`` now returns False
for string representations of floats outside 0-1.
12 changes: 9 additions & 3 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,23 @@ def _to_rgba_no_colorcycle(c, alpha=None):
return tuple(color)
# string gray.
try:
return (float(c),) * 3 + (alpha if alpha is not None else 1.,)
c = float(c)
except ValueError:
pass
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
else:
if not (0 <= c <= 1):
raise ValueError(
f"Invalid string grayscale value {orig_c!r}. "
f"Value must be within 0-1 range")
return c, c, c, alpha if alpha is not None else 1.
raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
# tuple color.
c = np.array(c)
if not np.can_cast(c.dtype, float, "same_kind") 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))
raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
# Return a tuple to prevent the cached value from being modified.
c = tuple(c.astype(float))
if len(c) not in [3, 4]:
Expand Down
18 changes: 12 additions & 6 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,6 @@ def test_autoscale_masked():
plt.draw()


def test_colors_no_float():
# Gray must be a string to distinguish 3-4 grays from RGB or RGBA.
with pytest.raises(ValueError):
mcolors.to_rgba(0.4)


@image_comparison(baseline_images=['light_source_shading_topo'],
extensions=['png'])
def test_light_source_topo_surface():
Expand Down Expand Up @@ -756,6 +750,18 @@ def test_conversions():
hex_color


def test_failed_conversions():
with pytest.raises(ValueError):
mcolors.to_rgba('5')
with pytest.raises(ValueError):
mcolors.to_rgba('-1')
with pytest.raises(ValueError):
mcolors.to_rgba('nan')
with pytest.raises(ValueError):
# Gray must be a string to distinguish 3-4 grays from RGB or RGBA.
mcolors.to_rgba(0.4)


def test_grey_gray():
color_mapping = mcolors._colors_full_map
for k in color_mapping.keys():
Expand Down
0