Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fd9beb

Browse files
authored
Merge pull request #13913 from timhoffm/fix-to_rgba-str
Fix string numbers in to_rgba() and is_color_like()
2 parents 0c4f13a + dbc205d commit 6fd9beb

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
API changes
2+
```````````
3+
4+
`matplotlib.color.is_colorlike()` used to return True for all string
5+
representations of floats. However, only those with values in 0-1 are valid
6+
colors (representing grayscale values). ``is_colorlike()`` now returns False
7+
for string representations of floats outside 0-1.

lib/matplotlib/colors.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,23 @@ def _to_rgba_no_colorcycle(c, alpha=None):
225225
return tuple(color)
226226
# string gray.
227227
try:
228-
return (float(c),) * 3 + (alpha if alpha is not None else 1.,)
228+
c = float(c)
229229
except ValueError:
230230
pass
231-
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
231+
else:
232+
if not (0 <= c <= 1):
233+
raise ValueError(
234+
f"Invalid string grayscale value {orig_c!r}. "
235+
f"Value must be within 0-1 range")
236+
return c, c, c, alpha if alpha is not None else 1.
237+
raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
232238
# tuple color.
233239
c = np.array(c)
234240
if not np.can_cast(c.dtype, float, "same_kind") or c.ndim != 1:
235241
# Test the dtype explicitly as `map(float, ...)`, `np.array(...,
236242
# float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
237243
# Test dimensionality to reject single floats.
238-
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
244+
raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
239245
# Return a tuple to prevent the cached value from being modified.
240246
c = tuple(c.astype(float))
241247
if len(c) not in [3, 4]:

lib/matplotlib/tests/test_colors.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,6 @@ def test_autoscale_masked():
471471
plt.draw()
472472

473473

474-
def test_colors_no_float():
475-
# Gray must be a string to distinguish 3-4 grays from RGB or RGBA.
476-
with pytest.raises(ValueError):
477-
mcolors.to_rgba(0.4)
478-
479-
480474
@image_comparison(baseline_images=['light_source_shading_topo'],
481475
extensions=['png'])
482476
def test_light_source_topo_surface():
@@ -756,6 +750,18 @@ def test_conversions():
756750
hex_color
757751

758752

753+
def test_failed_conversions():
754+
with pytest.raises(ValueError):
755+
mcolors.to_rgba('5')
756+
with pytest.raises(ValueError):
757+
mcolors.to_rgba('-1')
758+
with pytest.raises(ValueError):
759+
mcolors.to_rgba('nan')
760+
with pytest.raises(ValueError):
761+
# Gray must be a string to distinguish 3-4 grays from RGB or RGBA.
762+
mcolors.to_rgba(0.4)
763+
764+
759765
def test_grey_gray():
760766
color_mapping = mcolors._colors_full_map
761767
for k in color_mapping.keys():

0 commit comments

Comments
 (0)