Skip to content

ferrum.color

Programmatic access to ferrum's color palettes — categorical, sequential, and diverging.

Programmatic access to ferrum's color palettes.

Provides functions to retrieve hex color arrays from named categorical, sequential, and diverging palettes. The Rust palette registry (crates/ferrum-core/src/render/palette.rs + crates/ferrum-core/src/render/color/continuous.rs) is the single source of truth: this module consumes it through the ferrum._core accessors (list_palettes, palette_kind, palette_colors, palette_sample) rather than re-declaring hex tables. Continuous palettes are sampled from the same interpolator the renderer uses, so color.palette("viridis") returns the colors that actually render.

palette

palette(name: str, n: int | None = None) -> list[str]

Return hex colors from a named palette.

Parameters:

Name Type Description Default
name str

Palette name (e.g., "tableau10", "okabe_ito", "viridis").

required
n int

Number of colors. For categorical palettes, wraps cyclically if n exceeds the palette length. For continuous palettes, interpolates n evenly-spaced colors (render-truth samples).

None

Returns:

Type Description
list[str]

Hex color strings (e.g., ["#1f77b4", ...]).

Raises:

Type Description
ValueError

If name is not a recognized palette.

Examples:

>>> import ferrum
>>> ferrum.color.palette("tableau10")[:3]
['#4c78a8', '#f58e18', '#e45756']
>>> ferrum.color.palette("viridis", n=3)
['#440154', '#20908c', '#fde725']

to_hex

to_hex(color: tuple[float, ...] | str, *, scale: Literal['unit', 'byte'] | None = None) -> str

Convert a color to a hex string.

Parameters:

Name Type Description Default
color tuple or str

An RGB tuple with values in [0, 1] (unit) or [0, 255] (byte) — the first 3 components are R/G/B; an optional 4th component is alpha, in the same scale as R/G/B, and is honored into the hex output (see Returns) rather than silently dropped, EXCEPT when it is fully opaque -- a fully-opaque alpha normalizes away on every input form (matching the historical, still-current string behavior), so to_hex((1.0, 0.0, 0.0, 1.0)) and to_hex((1.0, 0.0, 0.0)) both return "#ff0000" — or a color string: a CSS named color (e.g. "steelblue"), hex (#rgb/#rgba/#rrggbb/#rrggbbaa), or a functional rgb()/rgba() form. String input is validated and normalized by ferrum's single Rust color parser (parse_color), so this function accepts the exact vocabulary every other ferrum color boundary accepts, alpha included.

required
scale ('unit', 'byte')

Explicit interpretation of an RGB tuple's component range. "unit" treats components as floats in [0, 1]; "byte" treats them as integers in [0, 255]. When None (default) the range is inferred: any component greater than 1 forces byte interpretation; otherwise unit interpretation is used. This makes integer-valued floats (1.0) and integers (1) behave identically. Ignored for string input.

"unit"

Returns:

Type Description
str

Normalized hex string: "#rrggbb" when there is no alpha component, or the alpha is fully opaque, and "#rrggbbaa" when there is a non-opaque alpha. This is one rule shared by both input forms with no per-form drift (#122 residual, close-remediation cycle 2): a string carrying alpha (rgba(), 4/8-digit hex) keeps it in the output only when it is not fully opaque, and a tuple's 4th component follows the identical rule — so to_hex((1.0, 0.0, 0.0, 1.0)), to_hex("#ff0000ff"), to_hex("#f00f"), and to_hex("rgba(255,0,0,1)") all return "#ff0000", and to_hex is idempotent over its own output (to_hex(to_hex(x)) == to_hex(x)) for every opaque color.

Raises:

Type Description
ValueError

If a string input is not a recognized CSS color name, hex literal, or rgb()/rgba() form (the message names the accepted forms); if the string is a paint-clearing sentinel ("none"/"transparent", trimmed and case-insensitive) — those spellings clear paint at the mark/selection boundaries (MarkBase.__init__, selection.py) and have no hex form here, so the message says that instead of the generic accepted-forms text, which would be self-contradicting for "transparent" (a real CSS Color 4 keyword); if a tuple input's shape is not recognized, or any of its read components is not numeric (e.g. ("a", "b", "c") — previously an unexplained TypeError escaped from the internal range comparison; it is now this typed ValueError); or if scale is not one of "unit", "byte", or None.

Examples:

>>> import ferrum
>>> ferrum.color.to_hex((1.0, 0.0, 0.0))
'#ff0000'
>>> ferrum.color.to_hex((255, 0, 0))
'#ff0000'
>>> ferrum.color.to_hex((128, 128, 128), scale="byte")
'#808080'
>>> ferrum.color.to_hex((1.0, 0.0, 0.0, 0.5))
'#ff000080'
>>> ferrum.color.to_hex("steelblue")
'#4682b4'
>>> ferrum.color.to_hex("rgb(70, 130, 180)")
'#4682b4'

sequential

sequential(name: str, n: int = 256) -> list[str]

Return n interpolated colors from a sequential palette.

Parameters:

Name Type Description Default
name str

Sequential palette name (e.g., "viridis", "plasma", "magma", "inferno", "cividis", "blues", "cool_blue", "warm_ochre", "night_blue", "electric_lime", "signal_blue", "ember_orange").

required
n int

Number of interpolated colors.

256

Returns:

Type Description
list[str]

Hex color strings (render-truth samples).

Raises:

Type Description
ValueError

If name is not a recognized sequential palette.

Examples:

>>> import ferrum
>>> colors = ferrum.color.sequential("viridis", n=5)
>>> len(colors)
5

diverging

diverging(name: str, n: int = 11) -> list[str]

Return n colors from a diverging palette, centered.

Parameters:

Name Type Description Default
name str

Diverging palette name (e.g., "rdbu", "blue_to_red", "cyan_to_amber", "blue_to_violet").

required
n int

Number of colors (odd recommended for a distinct center point).

11

Returns:

Type Description
list[str]

Hex color strings (render-truth samples).

Raises:

Type Description
ValueError

If name is not a recognized diverging palette.

Examples:

>>> import ferrum
>>> colors = ferrum.color.diverging("rdbu", n=5)
>>> len(colors)
5