Skip to content

Commit c217600

Browse files
committed
added missing documentation
1 parent 49b8c4e commit c217600

1 file changed

Lines changed: 69 additions & 16 deletions

File tree

colormaps/matplotlib_cmaps.py

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,47 @@
1313
)
1414

1515

16-
def rgb_colors_to_hex_list(colors: list[tuple[int, int, int]]):
16+
def rgb_colors_to_hex_list(
17+
colors: list[tuple[int, int, int]]
18+
) -> list[tuple[float, str]]:
19+
"""Convert a list of RGB colors to a list of tuples with the position of the color
20+
and the color in hex format. Positions evenly distributed between 0 and 1.
21+
22+
Args:
23+
colors: list of RGB colors
24+
25+
Returns:
26+
list of tuples with the position of the color and the color in hex format
27+
"""
1728
return [(i / len(colors), pltc.to_hex(color)) for i, color in enumerate(colors)]
1829

1930

20-
def _interpolate(val, vmin, vmax):
21-
"""Interpolate a color component between to values as provided
22-
by matplotlib colormaps
31+
def _interpolate(
32+
val: float, vmin: tuple[float, float, float], vmax: tuple[float, float, float]
33+
):
34+
"""Interpolate between two level of a color.
35+
36+
Args:
37+
val: value to interpolate
38+
vmin: R, G or B tuple from a matplotlib segmented colormap
39+
vmax: R, G or B tuple from matplotlib segmented colormap
40+
41+
Returns:
42+
The interpolated R, G or B component
2343
"""
2444
interp = (val - vmin[0]) / (vmax[0] - vmin[0])
2545
return (1 - interp) * vmin[1] + interp * vmax[2]
2646

2747

2848
def std_segmented_cmap_to_hex_list(cmdata: dict[str, list[tuple[float, float, float]]]):
29-
"""Setup a CustomQwtLinearColorMap according to
30-
matplotlib's data
49+
"""Convert a matplotlib segmented colormap to a list of tuples with the position of
50+
the color and the color in hex format.
51+
52+
Args:
53+
cmdata: segmented colormap data
54+
55+
Returns:
56+
list of tuples with the position of the color and the color in hex format
3157
"""
3258
colors: list[tuple[float, str]] = []
3359
red = np.array(cmdata["red"])
@@ -51,14 +77,33 @@ def std_segmented_cmap_to_hex_list(cmdata: dict[str, list[tuple[float, float, fl
5177
def func_segmented_cmap_to_hex_list(
5278
n: int,
5379
cmap: pltc.LinearSegmentedColormap,
54-
):
80+
) -> list[tuple[float, str]]:
81+
"""Convert a matplotlib segmented colormap to a list of tuples with the position of
82+
the color and the color in hex format. The input colormap contains function for each
83+
color RGB component instead of a list of colors.
84+
85+
Args:
86+
n: number of colors to generate
87+
cmap: segmented colormap
88+
89+
Returns:
90+
list of tuples with the position of the color and the color in hex format
91+
"""
5592
colors = []
5693
arr = np.linspace(0, 1, n, dtype=float)
5794
colors = [(i, pltc.to_hex(rgba)) for i, rgba in zip(arr, cmap(arr))]
5895
return colors
5996

6097

61-
def interp_to_descrete_cmap(cmap: EditableColormap) -> EditableColormap:
98+
def continuous_to_descrete_cmap(cmap: EditableColormap) -> EditableColormap:
99+
"""Convert a continuous colormap to a descrete one.
100+
101+
Args:
102+
cmap: colormap to convert
103+
104+
Returns:
105+
descrete colormap
106+
"""
62107
raw_cmap: tuple[tuple[float, str], ...] = cmap.to_tuples()
63108
new_raw_cmap: list[tuple[float, str]] = [raw_cmap[0]]
64109
n = len(raw_cmap)
@@ -79,6 +124,7 @@ def main():
79124

80125
new_cmaps: dict[str, list[tuple[float, str]]] = {}
81126

127+
# Uniform colormaps with a .colors attribute that return a list of RGB colors
82128
cmaps_with_colors = [
83129
"magma",
84130
"viridis",
@@ -87,6 +133,8 @@ def main():
87133
"cividis",
88134
]
89135

136+
# Discrete colormaps, same as uniform colormaps but the colormap must be post
137+
# processed to become descrete
90138
descrete_cmaps = [
91139
"Pastel1",
92140
"Pastel2",
@@ -100,26 +148,31 @@ def main():
100148

101149
cmaps_with_colors.extend(descrete_cmaps)
102150

151+
# Colormaps with a _segmented_data attribute that contains the R, G and B components
152+
# as lists of tuples
153+
segmented_cmaps = [
154+
"coolwarm",
155+
"bwr",
156+
"seismic",
157+
]
158+
159+
# Colormaps with a _segmentdata attribute that contains the R, G and B components as
160+
# functions that return the color for a given position
161+
interp_cmaps = ["gnuplot2", "CMRmap", "rainbow", "turbo", "afmhot"]
162+
103163
for cm_name in cmaps_with_colors:
104164
cmap = plt.get_cmap(cm_name)
105165
new_cmaps[cm_name] = rgb_colors_to_hex_list(cmap.colors)
106166

107167
for cm_name in descrete_cmaps:
108168
cmap = EditableColormap.from_iterable(new_cmaps[cm_name], name=cm_name)
109-
new_cmaps[cm_name] = list(interp_to_descrete_cmap(cmap).to_tuples())
110-
111-
segmented_cmaps = [
112-
"coolwarm",
113-
"bwr",
114-
"seismic",
115-
]
169+
new_cmaps[cm_name] = list(continuous_to_descrete_cmap(cmap).to_tuples())
116170

117171
for cm_name in segmented_cmaps:
118172
cmap = plt.get_cmap(cm_name)
119173
new_cmaps[cm_name] = std_segmented_cmap_to_hex_list(cmap._segmentdata)
120174

121175
n = 128
122-
interp_cmaps = ["gnuplot2", "CMRmap", "rainbow", "turbo", "afmhot"]
123176

124177
for cm_name in interp_cmaps:
125178
cmap = plt.get_cmap(cm_name)

0 commit comments

Comments
 (0)