File tree Expand file tree Collapse file tree 2 files changed +18
-7
lines changed Expand file tree Collapse file tree 2 files changed +18
-7
lines changed Original file line number Diff line number Diff line change @@ -225,9 +225,14 @@ def _to_rgba_no_colorcycle(c, alpha=None):
225
225
return tuple (color )
226
226
# string gray.
227
227
try :
228
- return ( float ( c ),) * 3 + ( alpha if alpha is not None else 1. , )
228
+ c = float ( c )
229
229
except ValueError :
230
230
pass
231
+ else :
232
+ if not (0 <= c <= 1 ):
233
+ raise ValueError (
234
+ "String grayscale values must be within 0-1 range" )
235
+ return (float (c ),) * 3 + (alpha if alpha is not None else 1. ,)
231
236
raise ValueError ("Invalid RGBA argument: {!r}" .format (orig_c ))
232
237
# tuple color.
233
238
c = np .array (c )
Original file line number Diff line number Diff line change @@ -471,12 +471,6 @@ def test_autoscale_masked():
471
471
plt .draw ()
472
472
473
473
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
-
480
474
@image_comparison (baseline_images = ['light_source_shading_topo' ],
481
475
extensions = ['png' ])
482
476
def test_light_source_topo_surface ():
@@ -756,6 +750,18 @@ def test_conversions():
756
750
hex_color
757
751
758
752
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
+
759
765
def test_grey_gray ():
760
766
color_mapping = mcolors ._colors_full_map
761
767
for k in color_mapping .keys ():
You can’t perform that action at this time.
0 commit comments