Skip to content

palettes

palettes

Color palette toolkit for pydisplay.

Provides named color sets, RGB/HSV wheels, RGB cubes, and Material Design swatches. Palettes map integer indices to display-ready color values at several bit depths (4, 8, 16, or 24).

Example

from palettes import get_palette palette = get_palette(name="wheel", length=256, saturation=1.0) palette[0] palette.color_name(0)

Attributes:

Name Type Description
WIN16

Mapping of 0xRRGGBB values to Windows 16-color names. Used as the default name table for :class:Palette and :class:~palettes.wheel.WheelPalette.

MappedPalette(name, color_depth, swapped, color_map)

Bases: Palette

Palette backed by a flat RGB byte map.

Each color occupies three consecutive bytes (r, g, b) in color_map. Subclasses such as :class:~palettes.material_design.MDPalette supply a pre-built map and named-color attributes.

Parameters:

Name Type Description Default
name

Optional label stored in :attr:name.

required
color_depth

Output format for :meth:Palette.__getitem__.

required
swapped

Byte-swap 16-bit colors when True.

required
color_map

bytes or buffer of RGB triplets, length 3 * n_colors.

required

Palette(name='', color_depth=16, swapped=False, cached=False)

Indexed color palette with optional named color attributes.

Subclasses override :meth:_get_rgb to define how each index maps to red, green, and blue components. :meth:__getitem__ converts those components to the configured color_depth.

Named colors from the palette's name table (for example palette.RED) are attached as attributes during initialization.

Parameters:

Name Type Description Default
name

Optional label stored in :attr:name.

''
color_depth

Output format for :meth:__getitem__: 4 (24-bit index), 8 (RGB332), 16 (RGB565), or 24 (0xRRGGBB).

16
swapped

If True, byte-swap 16-bit colors (little-endian displays).

False
cached

If True, memoize computed index colors in an internal dict.

False

name property

Human-readable palette label.

brightness(index)

Average channel brightness of the color at index.

Parameters:

Name Type Description Default
index

Palette index.

required

Returns:

Type Description

Normalized brightness in 0.01.0.

color332(r, g=None, b=None)

Convert RGB to an 8-bit RGB332 value.

Parameters:

Name Type Description Default
r

Red component (0–255), a 24-bit 0xRRGGBB integer, or an (r, g, b) sequence.

required
g

Green component when r is passed separately.

None
b

Blue component when r is passed separately.

None

Returns:

Type Description

8-bit RGB332 color.

color565(r, g=None, b=None)

Convert RGB to a 16-bit RGB565 value.

Parameters:

Name Type Description Default
r

Red component (0–255), a 24-bit 0xRRGGBB integer, or an (r, g, b) sequence.

required
g

Green component when r is passed separately.

None
b

Blue component when r is passed separately.

None

Returns:

Type Description

16-bit color, optionally byte-swapped when swapped is True.

color_name(index)

Return the name of the color at index.

Parameters:

Name Type Description Default
index

Palette index (supports wrapping).

required

Returns:

Type Description

A name from the palette name table, or a "#RRGGBB" hex string

when no name matches.

color_rgb(color)

Expand a packed color to an (r, g, b) tuple.

Parameters:

Name Type Description Default
color

A 16-bit integer, or a 2- or 3-byte sequence in display byte order.

required

Returns:

Type Description

(red, green, blue) with each component in 0255.

luminance(index)

Perceived brightness of the color at index (ITU-R BT.601).

Parameters:

Name Type Description Default
index

Palette index.

required

Returns:

Type Description

Luminance in 0.0255.0.

rgb_name(r, g=None, b=None)

Look up a color name from RGB components.

Parameters:

Name Type Description Default
r

Red (0–255), a 24-bit integer, or an (r, g, b) sequence.

required
g

Green when r is passed separately.

None
b

Blue when r is passed separately.

None

Returns:

Type Description

Matching name from :attr:_names, or "#RRGGBB" if unknown.

get_palette(name='default', **kwargs)

Construct a palette by logical name.

Parameters:

Name Type Description Default
name

Palette type. One of "default" (Windows 16-color), "wheel", "cube", or "material_design". Unknown names fall back to :class:Palette.

'default'
**kwargs

Forwarded to the palette constructor (for example color_depth, length, size, saturation).

{}

Returns:

Name Type Description
A

class:Palette subclass instance.

Example

get_palette(name="cube", size=3, color_depth=16)