CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cmasher

Scientific colormaps for making accessible, informative and 'cmashing' plots

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

color-analysis.mddocs/

Color Extraction and Analysis

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.

Capabilities

Color Extraction

Extract equally spaced colors from any colormap with flexible output formats.

def take_cmap_colors(
    cmap: str | Colormap,
    N: int | None,
    *,
    cmap_range: tuple[float, float] = (0, 1),
    return_fmt: str = "float"
) -> list:
    """
    Takes N equally spaced colors from the provided colormap.

    Parameters:
    - cmap: Colormap name or object
    - N: Number of colors to extract (None for all colors)
    - cmap_range: Normalized range to extract from (0, 1)
    - return_fmt: Output format - 'float'/'norm', 'int'/'8bit', 'str'/'hex'

    Returns:
    list: Colors in requested format
    """

Usage Examples

import cmasher as cmr

# Extract 5 colors as normalized RGB tuples
colors = cmr.take_cmap_colors('cmr.rainforest', 5)
# [(0.0, 0.0, 0.0), (0.226, 0.125, 0.563), ...]

# Extract colors as hex strings
hex_colors = cmr.take_cmap_colors('cmr.ocean', 3, return_fmt='hex')
# ['#000000', '#0E8474', '#FFFFFF']

# Extract colors as 8-bit RGB
rgb_colors = cmr.take_cmap_colors('cmr.iceburn', 4, return_fmt='int')
# [(0, 0, 0), (58, 32, 144), (181, 184, 21), (255, 255, 255)]

# Extract from specific range of colormap
partial_colors = cmr.take_cmap_colors(
    'cmr.wildfire', 5, 
    cmap_range=(0.2, 0.8), 
    return_fmt='hex'
)

# Extract all colors from colormap
all_colors = cmr.take_cmap_colors('cmr.lilac', None)

Colormap Type Analysis

Determine the type and characteristics of colormaps for appropriate usage.

def get_cmap_type(cmap: str | Colormap) -> str:
    """
    Determines the colormap type through perceptual analysis.

    Parameters:
    - cmap: Colormap name or object

    Returns:
    str: One of 'sequential', 'diverging', 'cyclic', 'qualitative', 'misc'
    """

Usage Examples

import cmasher as cmr

# Check type of CMasher colormaps
print(cmr.get_cmap_type('cmr.rainforest'))  # 'sequential'
print(cmr.get_cmap_type('cmr.iceburn'))     # 'diverging'
print(cmr.get_cmap_type('cmr.seasons'))     # 'cyclic'

# Check type of matplotlib colormaps
print(cmr.get_cmap_type('viridis'))    # 'sequential'
print(cmr.get_cmap_type('coolwarm'))   # 'diverging'
print(cmr.get_cmap_type('hsv'))        # 'cyclic'
print(cmr.get_cmap_type('tab10'))      # 'qualitative'

Colormap Listing

Get lists of available colormaps filtered by type.

def get_cmap_list(cmap_type: str = "all") -> list[str]:
    """
    Returns list of CMasher colormap names by type.

    Parameters:
    - cmap_type: Type filter - 'all', 'sequential'/'seq'/'s', 
                 'diverging'/'div'/'d', 'cyclic'/'cyc'/'c'

    Returns:
    list[str]: List of colormap names (without 'cmr.' prefix)
    """

Usage Examples

import cmasher as cmr

# Get all CMasher colormaps
all_cmaps = cmr.get_cmap_list()
print(f"Total colormaps: {len(all_cmaps)}")

# Get sequential colormaps only
sequential = cmr.get_cmap_list('sequential')
print(f"Sequential colormaps: {len(sequential)}")

# Using short forms
diverging = cmr.get_cmap_list('div')
cyclic = cmr.get_cmap_list('c')

# Print available colormaps by type
for cmap_type in ['sequential', 'diverging', 'cyclic']:
    cmaps = cmr.get_cmap_list(cmap_type)
    print(f"\n{cmap_type.title()} ({len(cmaps)}):")
    for cmap in cmaps[:5]:  # Show first 5
        print(f"  {cmap}")
    if len(cmaps) > 5:
        print(f"  ... and {len(cmaps) - 5} more")

Colormap Analysis Helpers

Internal functions for detailed colormap analysis.

def _get_cmap_lightness_rank(cmap: Colormap) -> tuple:
    """
    Returns lightness profile ranking data for colormap sorting.

    Returns:
    tuple: (L_slope, L_type, L_start, L_rng, L_rmse, name)
    """

def _get_cmap_perceptual_rank(cmap: Colormap) -> tuple:
    """
    Returns perceptual and lightness profile data for colormap analysis.

    Returns:
    tuple: (*L_rank, P_rng, name)
    """

Practical Applications

Creating Discrete Color Palettes

import cmasher as cmr
import matplotlib.pyplot as plt
import numpy as np

# Create discrete color palette for categorical data
categories = ['A', 'B', 'C', 'D', 'E']
colors = cmr.take_cmap_colors('cmr.tropical', len(categories), return_fmt='hex')

# Use in bar plot
values = np.random.rand(len(categories))
plt.bar(categories, values, color=colors)
plt.title('Categorical Data with CMasher Colors')
plt.show()

Colormap Selection Workflow

import cmasher as cmr

# Find appropriate colormap for data type
def suggest_colormap(data_type, preference=None):
    if data_type == 'sequential':
        cmaps = cmr.get_cmap_list('sequential')
    elif data_type == 'diverging':
        cmaps = cmr.get_cmap_list('diverging')
    elif data_type == 'cyclic':
        cmaps = cmr.get_cmap_list('cyclic')
    
    if preference:
        # Filter by color preference
        matching = [c for c in cmaps if preference.lower() in c.lower()]
        return matching if matching else cmaps[:3]
    
    return cmaps[:3]

# Get colormap suggestions
ocean_cmaps = suggest_colormap('sequential', 'ocean')
print(f"Ocean-themed sequential: {ocean_cmaps}")

Install with Tessl CLI

npx tessl i tessl/pypi-cmasher

docs

cli-tools.md

color-analysis.md

colormap-collections.md

index.md

integration.md

manipulation.md

registration.md

visualization.md

tile.json