@@ -1353,6 +1353,48 @@ def test_to_rgba_error_with_color_invalid_alpha_tuple():
1353
1353
mcolors .to_rgba (('blue' , 2.0 ))
1354
1354
1355
1355
1356
+ @pytest .mark .parametrize ("bytes" , (True , False ))
1357
+ def test_scalarmappable_to_rgba (bytes ):
1358
+ sm = cm .ScalarMappable ()
1359
+ alpha_1 = 255 if bytes else 1
1360
+
1361
+ # uint8 RGBA
1362
+ x = np .ones ((2 , 3 , 4 ), dtype = np .uint8 )
1363
+ expected = x .copy () if bytes else x .astype (np .float32 )/ 255
1364
+ np .testing .assert_array_equal (sm .to_rgba (x , bytes = bytes ), expected )
1365
+ # uint8 RGB
1366
+ expected [..., 3 ] = alpha_1
1367
+ np .testing .assert_array_equal (sm .to_rgba (x [..., :3 ], bytes = bytes ), expected )
1368
+ # uint8 masked RGBA
1369
+ xm = np .ma .masked_array (x , mask = np .zeros_like (x ))
1370
+ xm .mask [0 , 0 , 0 ] = True
1371
+ expected = x .copy () if bytes else x .astype (np .float32 )/ 255
1372
+ expected [0 , 0 , 3 ] = 0
1373
+ np .testing .assert_array_equal (sm .to_rgba (xm , bytes = bytes ), expected )
1374
+ # uint8 masked RGB
1375
+ expected [..., 3 ] = alpha_1
1376
+ expected [0 , 0 , 3 ] = 0
1377
+ np .testing .assert_array_equal (sm .to_rgba (xm [..., :3 ], bytes = bytes ), expected )
1378
+
1379
+ # float RGBA
1380
+ x = np .ones ((2 , 3 , 4 ), dtype = float ) * 0.5
1381
+ expected = (x * 255 ).astype (np .uint8 ) if bytes else x .copy ()
1382
+ np .testing .assert_array_equal (sm .to_rgba (x , bytes = bytes ), expected )
1383
+ # float RGB
1384
+ expected [..., 3 ] = alpha_1
1385
+ np .testing .assert_array_equal (sm .to_rgba (x [..., :3 ], bytes = bytes ), expected )
1386
+ # float masked RGBA
1387
+ xm = np .ma .masked_array (x , mask = np .zeros_like (x ))
1388
+ xm .mask [0 , 0 , 0 ] = True
1389
+ expected = (x * 255 ).astype (np .uint8 ) if bytes else x .copy ()
1390
+ expected [0 , 0 , 3 ] = 0
1391
+ np .testing .assert_array_equal (sm .to_rgba (xm , bytes = bytes ), expected )
1392
+ # float masked RGB
1393
+ expected [..., 3 ] = alpha_1
1394
+ expected [0 , 0 , 3 ] = 0
1395
+ np .testing .assert_array_equal (sm .to_rgba (xm [..., :3 ], bytes = bytes ), expected )
1396
+
1397
+
1356
1398
def test_failed_conversions ():
1357
1399
with pytest .raises (ValueError ):
1358
1400
mcolors .to_rgba ('5' )
0 commit comments