Qualpal
Repositories

qualpal.Qualpal

Python · python · 1.1.0

Qualpal(
    colors: Sequence[str] | None = None,
    colorspace: dict[str, tuple[float, float]] | None = None,
    palette: str | None = None,
    space: str = "hsl",
    cvd: dict[str, float] | None = None,
    metric: str = "ciede2000",
    background: str | None = None,
    max_memory: float = 1.0,
    colorspace_size: int = 1000
)

Generate qualitative color palettes with distinct colors.

The Qualpal class provides a stateful builder for generating color palettes. You can specify colors, colorspace, or palette as input, and configure parameters like CVD simulation, distance metric, etc.

__init__

__init__(
    colors: Sequence[str] | None = None,
    colorspace: dict[str, tuple[float, float]] | None = None,
    palette: str | None = None,
    space: str = "hsl",
    cvd: dict[str, float] | None = None,
    metric: str = "ciede2000",
    background: str | None = None,
    max_memory: float = 1.0,
    colorspace_size: int = 1000
) -> None

Initialize Qualpal object.

Parameters

colors : Sequence[str] | None

List of hex color strings to use as starting point. Mutually exclusive with colorspace and palette.

colorspace : dict[str, tuple[float, float]] | None

Colorspace specification with ranges for each dimension. Keys depend on ‘space’ parameter: - ‘hsl’: ‘h’, ‘s’, ‘l’ - ‘lchab’: ‘h’, ‘c’, ‘l’ Each value is a (min, max) tuple. Mutually exclusive with colors and palette.

palette : str | None

Named palette in format ‘source:name’ (e.g., ‘ColorBrewer:Set1’). Mutually exclusive with colors and colorspace.

space : str

Color space to use: ‘hsl’ (default) or ‘lchab’.

cvd : dict[str, float] | None

Color vision deficiency simulation. Keys: ‘protan’, ‘deutan’, ‘tritan’. Values: 0.0 (normal) to 1.0 (complete deficiency).

metric : str

Color difference metric: ‘ciede2000’ (default), ‘din99d’, or ‘cie76’.

background : str | None

Background color as hex string (e.g., ‘#ffffff’).

max_memory : float

Maximum memory to use in GB (default: 1.0).

colorspace_size : int

Number of colors to sample from colorspace (default: 1000).

Raises

ValueError

If multiple of colors/colorspace/palette are provided, or if any parameter has invalid values.

TypeError

If parameter types are incorrect.

cvd

cvd(self) -> dict[str, float] | None

Get color vision deficiency simulation settings.

metric

metric(self) -> str

Get color difference metric.

background

background(self) -> str | None

Get background color.

max_memory

max_memory(self) -> float

Get maximum memory in GB.

colorspace_size

colorspace_size(self) -> int

Get colorspace size.

generate

generate(n: int) -> qualpal.Palette

Generate a color palette with n distinct colors.

Parameters

n : int

Number of colors to generate.

Returns

Palette

A Palette object containing n Color objects.

Raises

RuntimeError

If palette generation fails (e.g., C++ algorithm error).

ValueError

If n is not positive.

TypeError

If n is not an integer.

Notes

Behavior depends on input mode:

  • colorspace mode: Samples n colors from the continuous color space

  • colors mode: Selects n most distinct colors from the provided list

  • palette mode: Loads named palette and selects n most distinct colors