12
12
from itertools import chain
13
13
from pathlib import Path
14
14
from textwrap import dedent
15
- from typing import TYPE_CHECKING , NewType
15
+ from typing import TYPE_CHECKING , NewType , overload
16
16
17
17
import matplotlib as mpl
18
18
import numpy as np
33
33
import os
34
34
import sys
35
35
from collections .abc import Callable , Iterator
36
- from typing import Literal , Protocol , TypeAlias
36
+ from typing import Literal , Protocol , TypeAlias , TypeVar
37
37
38
38
from matplotlib .artist import Artist
39
39
from numpy .typing import NDArray
43
43
else :
44
44
from typing_extensions import Self
45
45
46
+ T = TypeVar ("T" , int , float )
47
+ RGB : TypeAlias = tuple [T , T , T ]
48
+
46
49
class SupportsDunderLT (Protocol ):
47
50
def __lt__ (self , other : Self , / ) -> bool : ...
48
51
@@ -51,6 +54,7 @@ def __gt__(self, other: Self, /) -> bool: ...
51
54
52
55
SupportsOrdering : TypeAlias = SupportsDunderLT | SupportsDunderGT
53
56
57
+
54
58
_HAS_VISCM = find_spec ("viscm" ) is not None
55
59
56
60
# All declaration
@@ -78,12 +82,6 @@ def __gt__(self, other: Self, /) -> bool: ...
78
82
Category = NewType ("Category" , str )
79
83
Name = NewType ("Name" , str )
80
84
81
- # Type aliases
82
- RED : TypeAlias = float
83
- GREEN : TypeAlias = float
84
- BLUE : TypeAlias = float
85
- RGB : TypeAlias = list [tuple [RED , GREEN , BLUE ]]
86
-
87
85
88
86
# %% HELPER FUNCTIONS
89
87
# Define function for obtaining the sorting order for lightness ranking
@@ -1436,13 +1434,43 @@ def set_cmap_legend_entry(artist: Artist, label: str) -> None:
1436
1434
1437
1435
1438
1436
# Function to take N equally spaced colors from a colormap
1437
+ @overload
1438
+ def take_cmap_colors (
1439
+ cmap : Colormap | Name ,
1440
+ N : int | None ,
1441
+ * ,
1442
+ cmap_range : tuple [float , float ] = (0 , 1 ),
1443
+ return_fmt : Literal ["float" , "norm" ] = "float" ,
1444
+ ) -> RGB [float ]: ...
1445
+
1446
+
1447
+ @overload
1439
1448
def take_cmap_colors (
1440
1449
cmap : Colormap | Name ,
1441
1450
N : int | None ,
1442
1451
* ,
1443
1452
cmap_range : tuple [float , float ] = (0 , 1 ),
1444
- return_fmt : str = "float" ,
1445
- ) -> RGB :
1453
+ return_fmt : Literal ["int" , "8bit" ],
1454
+ ) -> RGB [int ]: ...
1455
+
1456
+
1457
+ @overload
1458
+ def take_cmap_colors (
1459
+ cmap : Colormap | Name ,
1460
+ N : int | None ,
1461
+ * ,
1462
+ cmap_range : tuple [float , float ] = (0 , 1 ),
1463
+ return_fmt : Literal ["str" , "hex" ],
1464
+ ) -> list [str ]: ...
1465
+
1466
+
1467
+ def take_cmap_colors (
1468
+ cmap : Colormap | Name ,
1469
+ N : int | None ,
1470
+ * ,
1471
+ cmap_range : tuple [float , float ] = (0 , 1 ),
1472
+ return_fmt : Literal ["float" , "norm" , "int" , "8bit" , "str" , "hex" ] = "float" ,
1473
+ ) -> RGB [float ] | RGB [int ] | list [str ]:
1446
1474
"""
1447
1475
Takes `N` equally spaced colors from the provided colormap `cmap` and
1448
1476
returns them.
@@ -1514,9 +1542,6 @@ def take_cmap_colors(
1514
1542
that describe the same property, but have a different initial state.
1515
1543
1516
1544
"""
1517
- # Convert provided fmt to lowercase
1518
- return_fmt = return_fmt .lower ()
1519
-
1520
1545
# Obtain the colormap
1521
1546
if isinstance (cmap , str ):
1522
1547
cmap = mpl .colormaps [cmap ]
@@ -1544,12 +1569,13 @@ def take_cmap_colors(
1544
1569
colors = np .apply_along_axis (to_rgb , 1 , colors ) # type: ignore [call-overload]
1545
1570
if return_fmt in ("int" , "8bit" ):
1546
1571
colors = np .array (np .rint (colors * 255 ), dtype = int )
1547
- colors = list (map (tuple , colors ))
1572
+ return [(int (c [0 ]), int (c [1 ]), int (c [2 ])) for c in colors ] # type: ignore [misc]
1573
+ else :
1574
+ return [(float (c [0 ]), float (c [1 ]), float (c [2 ])) for c in colors ] # type: ignore [misc]
1575
+ elif return_fmt in ("str" , "hex" ):
1576
+ return [to_hex (x ).upper () for x in colors ]
1548
1577
else :
1549
- colors = [to_hex (x ).upper () for x in colors ]
1550
-
1551
- # Return colors
1552
- return colors
1578
+ raise ValueError (return_fmt )
1553
1579
1554
1580
1555
1581
# Function to view what a colormap looks like
0 commit comments