8000 Fix string numbers in to_rgba() and is_color_like() · matplotlib/matplotlib@de7b0c6 · GitHub
[go: up one dir, main page]

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 de7b0c6

Browse files
committed
Fix string numbers in to_rgba() and is_color_like()
1 parent bc4c4cc commit de7b0c6

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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)
0