0
# Color Extraction and Analysis
1
2
Extract colors from colormaps and analyze their properties for data visualization workflows. These functions help understand colormap characteristics and extract specific colors for use in plots.
3
4
## Capabilities
5
6
### Color Extraction
7
8
Extract equally spaced colors from any colormap with flexible output formats.
9
10
```python { .api }
11
def take_cmap_colors(
12
cmap: str | Colormap,
13
N: int | None,
14
*,
15
cmap_range: tuple[float, float] = (0, 1),
16
return_fmt: str = "float"
17
) -> list:
18
"""
19
Takes N equally spaced colors from the provided colormap.
20
21
Parameters:
22
- cmap: Colormap name or object
23
- N: Number of colors to extract (None for all colors)
24
- cmap_range: Normalized range to extract from (0, 1)
25
- return_fmt: Output format - 'float'/'norm', 'int'/'8bit', 'str'/'hex'
26
27
Returns:
28
list: Colors in requested format
29
"""
30
```
31
32
#### Usage Examples
33
34
```python
35
import cmasher as cmr
36
37
# Extract 5 colors as normalized RGB tuples
38
colors = cmr.take_cmap_colors('cmr.rainforest', 5)
39
# [(0.0, 0.0, 0.0), (0.226, 0.125, 0.563), ...]
40
41
# Extract colors as hex strings
42
hex_colors = cmr.take_cmap_colors('cmr.ocean', 3, return_fmt='hex')
43
# ['#000000', '#0E8474', '#FFFFFF']
44
45
# Extract colors as 8-bit RGB
46
rgb_colors = cmr.take_cmap_colors('cmr.iceburn', 4, return_fmt='int')
47
# [(0, 0, 0), (58, 32, 144), (181, 184, 21), (255, 255, 255)]
48
49
# Extract from specific range of colormap
50
partial_colors = cmr.take_cmap_colors(
51
'cmr.wildfire', 5,
52
cmap_range=(0.2, 0.8),
53
return_fmt='hex'
54
)
55
56
# Extract all colors from colormap
57
all_colors = cmr.take_cmap_colors('cmr.lilac', None)
58
```
59
60
### Colormap Type Analysis
61
62
Determine the type and characteristics of colormaps for appropriate usage.
63
64
```python { .api }
65
def get_cmap_type(cmap: str | Colormap) -> str:
66
"""
67
Determines the colormap type through perceptual analysis.
68
69
Parameters:
70
- cmap: Colormap name or object
71
72
Returns:
73
str: One of 'sequential', 'diverging', 'cyclic', 'qualitative', 'misc'
74
"""
75
```
76
77
#### Usage Examples
78
79
```python
80
import cmasher as cmr
81
82
# Check type of CMasher colormaps
83
print(cmr.get_cmap_type('cmr.rainforest')) # 'sequential'
84
print(cmr.get_cmap_type('cmr.iceburn')) # 'diverging'
85
print(cmr.get_cmap_type('cmr.seasons')) # 'cyclic'
86
87
# Check type of matplotlib colormaps
88
print(cmr.get_cmap_type('viridis')) # 'sequential'
89
print(cmr.get_cmap_type('coolwarm')) # 'diverging'
90
print(cmr.get_cmap_type('hsv')) # 'cyclic'
91
print(cmr.get_cmap_type('tab10')) # 'qualitative'
92
```
93
94
### Colormap Listing
95
96
Get lists of available colormaps filtered by type.
97
98
```python { .api }
99
def get_cmap_list(cmap_type: str = "all") -> list[str]:
100
"""
101
Returns list of CMasher colormap names by type.
102
103
Parameters:
104
- cmap_type: Type filter - 'all', 'sequential'/'seq'/'s',
105
'diverging'/'div'/'d', 'cyclic'/'cyc'/'c'
106
107
Returns:
108
list[str]: List of colormap names (without 'cmr.' prefix)
109
"""
110
```
111
112
#### Usage Examples
113
114
```python
115
import cmasher as cmr
116
117
# Get all CMasher colormaps
118
all_cmaps = cmr.get_cmap_list()
119
print(f"Total colormaps: {len(all_cmaps)}")
120
121
# Get sequential colormaps only
122
sequential = cmr.get_cmap_list('sequential')
123
print(f"Sequential colormaps: {len(sequential)}")
124
125
# Using short forms
126
diverging = cmr.get_cmap_list('div')
127
cyclic = cmr.get_cmap_list('c')
128
129
# Print available colormaps by type
130
for cmap_type in ['sequential', 'diverging', 'cyclic']:
131
cmaps = cmr.get_cmap_list(cmap_type)
132
print(f"\n{cmap_type.title()} ({len(cmaps)}):")
133
for cmap in cmaps[:5]: # Show first 5
134
print(f" {cmap}")
135
if len(cmaps) > 5:
136
print(f" ... and {len(cmaps) - 5} more")
137
```
138
139
### Colormap Analysis Helpers
140
141
Internal functions for detailed colormap analysis.
142
143
```python { .api }
144
def _get_cmap_lightness_rank(cmap: Colormap) -> tuple:
145
"""
146
Returns lightness profile ranking data for colormap sorting.
147
148
Returns:
149
tuple: (L_slope, L_type, L_start, L_rng, L_rmse, name)
150
"""
151
152
def _get_cmap_perceptual_rank(cmap: Colormap) -> tuple:
153
"""
154
Returns perceptual and lightness profile data for colormap analysis.
155
156
Returns:
157
tuple: (*L_rank, P_rng, name)
158
"""
159
```
160
161
### Practical Applications
162
163
#### Creating Discrete Color Palettes
164
165
```python
166
import cmasher as cmr
167
import matplotlib.pyplot as plt
168
import numpy as np
169
170
# Create discrete color palette for categorical data
171
categories = ['A', 'B', 'C', 'D', 'E']
172
colors = cmr.take_cmap_colors('cmr.tropical', len(categories), return_fmt='hex')
173
174
# Use in bar plot
175
values = np.random.rand(len(categories))
176
plt.bar(categories, values, color=colors)
177
plt.title('Categorical Data with CMasher Colors')
178
plt.show()
179
```
180
181
#### Colormap Selection Workflow
182
183
```python
184
import cmasher as cmr
185
186
# Find appropriate colormap for data type
187
def suggest_colormap(data_type, preference=None):
188
if data_type == 'sequential':
189
cmaps = cmr.get_cmap_list('sequential')
190
elif data_type == 'diverging':
191
cmaps = cmr.get_cmap_list('diverging')
192
elif data_type == 'cyclic':
193
cmaps = cmr.get_cmap_list('cyclic')
194
195
if preference:
196
# Filter by color preference
197
matching = [c for c in cmaps if preference.lower() in c.lower()]
198
return matching if matching else cmaps[:3]
199
200
return cmaps[:3]
201
202
# Get colormap suggestions
203
ocean_cmaps = suggest_colormap('sequential', 'ocean')
204
print(f"Ocean-themed sequential: {ocean_cmaps}")
205
```