Skip to content

flixopt.color_processing

Simplified color handling for visualization.

This module provides clean color processing that transforms various input formats into a label-to-color mapping dictionary, without needing to know about the plotting engine.

Functions

process_colors

Python
process_colors(colors: None | str | list[str] | dict[str, str], labels: list[str], default_colorscale: str = 'turbo') -> dict[str, str]

Process color input and return a label-to-color mapping.

This function takes flexible color input and always returns a dictionary mapping each label to a specific color string. The plotting engine can then use this mapping as needed.

Parameters:

Name Type Description Default
colors None | str | list[str] | dict[str, str]

Color specification in one of four formats: - None: Use the default colorscale - str: Name of a colorscale (e.g., 'turbo', 'plasma', 'Set1', 'portland') - list[str]: List of color strings (hex, named colors, etc.) - dict[str, str]: Direct label-to-color mapping

required
labels list[str]

List of labels that need colors assigned

required
default_colorscale str

Fallback colorscale name if requested scale not found

'turbo'

Returns:

Type Description
dict[str, str]

Dictionary mapping each label to a color string

Examples:

Python Console Session
>>> # Using None - applies default colorscale
>>> process_colors(None, ['A', 'B', 'C'])
{'A': '#0d0887', 'B': '#7e03a8', 'C': '#cc4778'}
Python Console Session
>>> # Using a colorscale name
>>> process_colors('plasma', ['A', 'B', 'C'])
{'A': '#0d0887', 'B': '#7e03a8', 'C': '#cc4778'}
Python Console Session
>>> # Using a list of colors
>>> process_colors(['red', 'blue', 'green'], ['A', 'B', 'C'])
{'A': 'red', 'B': 'blue', 'C': 'green'}
Python Console Session
>>> # Using a pre-made mapping
>>> process_colors({'A': 'red', 'B': 'blue'}, ['A', 'B', 'C'])
{'A': 'red', 'B': 'blue', 'C': '#0d0887'}  # C gets color from default scale