Skip to content

Palette

This module provides color palettes mapping colors and names to indices.

Colors can be specified as:

  • The name of a global color (e.g., black, white, grey-50, transparent).

  • The name of a color in a standard palette (e.g., c64.red, spectrum.bright-blue).

  • An integer in the format 0xAARRGGBB (where A value 0 represents opaque).

  • A tuple of RGB(A) values (where A value 0 represents transparent).

Returned color values are in the format 0xAARRGGBB.

Palette(colors, names=None)

A palette mapping colors and names to indices.

PARAMETER DESCRIPTION
colors

A list or dictionary of colors mapping color values to indices.

TYPE: dict[int | str, int | None] | list[int | str]

names

A list or dictionary mapping names to indices.

TYPE: dict[str, int] | list[str] | None DEFAULT: None

__contains__(color)

Check if the palette contains the given color.

PARAMETER DESCRIPTION
color

color to check.

TYPE: str | int | tuple[int, ...] | float | None

RETURNS DESCRIPTION
bool

True if the color is in the palette, False otherwise.

__copy__()

Return a deep copy of the palette.

__getitem__(color)

Return the index of the given color or name.

PARAMETER DESCRIPTION
color

color or name to look up.

TYPE: str | int | tuple[int, ...] | float | None

RETURNS DESCRIPTION
int | None

The index of the color or name.

RAISES DESCRIPTION
KeyError

If the color or name is not found in the palette.

__len__()

Return the number of colors in the palette.

add_colors(colors)

Add multiple colors to the palette.

PARAMETER DESCRIPTION
colors

A list or dictionary of colors mapping color values to indices.

TYPE: dict[int | str, int | None] | list[int | str]

add_names(names)

Add multiple names to the palette.

PARAMETER DESCRIPTION
names

A list or dictionary mapping names to indices.

TYPE: dict[str, int] | list[str]

bit_length()

Return the number of bits needed to represent the highest index.

get_colors(color)

Return all color values matching the given index or name.

PARAMETER DESCRIPTION
color

name or index to look up.

TYPE: str | int

RETURNS DESCRIPTION
list[int]

List of matching color values.

RAISES DESCRIPTION
KeyError

If the name or index is not found in the palette.

get_index(name)

Return the index of the given name.

PARAMETER DESCRIPTION
name

name to look up.

TYPE: str

RETURNS DESCRIPTION
int

The index of the name.

RAISES DESCRIPTION
KeyError

If the name is not found in the palette.

normalize_color(color)

Normalize a color to the 0xAARRGGBB format.

PARAMETER DESCRIPTION
color

color to normalize.

TYPE: int | tuple[int, ...] | float

RETURNS DESCRIPTION
int

The normalized color.

RAISES DESCRIPTION
RuntimeError

If the color specification is invalid.

get_color(color)

Return the first color value matching the given color specification.

PARAMETER DESCRIPTION
color

color to look up.

TYPE: str | int | tuple[int, ...] | float

RETURNS DESCRIPTION
int

The first matching color value.

RAISES DESCRIPTION
KeyError

If the color name or palette is not found.

RuntimeError

If the color specification is invalid.

get_colors(color)

Return all color values matching the given color specification.

PARAMETER DESCRIPTION
color

color to look up.

TYPE: str | int | tuple[int, ...] | float

RETURNS DESCRIPTION
list[int]

List of matching color values.

RAISES DESCRIPTION
KeyError

If the color name or palette is not found.

RuntimeError

If the color specification is invalid.