File tree Expand file tree Collapse file tree 2 files changed +21
-9
lines changed Expand file tree Collapse file tree 2 files changed +21
-9
lines changed Original file line number Diff line number Diff line change @@ -225,17 +225,23 @@ 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
- 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} " )
232
238
# tuple color.
233
239
c = np .array (c )
234
240
if not np .can_cast (c .dtype , float , "same_kind" ) or c .ndim != 1 :
235
241
# Test the dtype explicitly as `map(float, ...)`, `np.array(...,
236
242
# float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
237
243
# 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} " )
239
245
# Return a tuple to prevent the cached value from being modified.
240
246
c = tuple (c .astype (float ))
241
247
if len (c ) not in [3 , 4 ]:
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