Skip to main content

Fast, dependency-free OKLab categorical color palettes powered by Rust

Project description

okpalette

Categorical color palettes for Python.

Use okpalette when you need distinct, stable colors for labels, plots, dashboards, or reports.

pip install okpalette

With uv:

uv add okpalette
from okpalette import create_palette

colors = create_palette(8)

Create A Palette

create_palette() returns lowercase hex colors by default.

from okpalette import create_palette

colors = create_palette(10)
# ["#080050", "#e00800", "#1078ff", ...]

The same inputs produce the same colors, so category mappings stay stable across runs.

Use RGB tuples when that fits your plotting library better:

rgb = create_palette(5, format="rgb")
# [(8, 0, 80), (224, 8, 0), ...]

rgb01 = create_palette(5, format="rgb01")
# [(0.03137254901960784, 0.0, 0.3137254901960784), ...]

Extend Colors

Use extend_palette() when you already have brand colors or a small palette.

from okpalette import extend_palette

brand = ["#0057b8", "#ffd700"]
colors = extend_palette(brand, 12)

assert colors[:2] == ["#0057b8", "#ffd700"]
assert len(colors) == 12

Use existing colors as anchors without returning them:

new_colors = extend_palette(brand, 10, include_existing=False)

Tune Appearance

By default, white is treated as a background color to avoid.

colors = create_palette(
    32,
    background="#ffffff",
    lightness=(0.20, 0.75),
    chroma=(0.05, None),
)

Allow white when you need it:

colors = create_palette(8, background=None, lightness=None, chroma=None)

Avoid other colors:

colors = create_palette(
    16,
    avoid_colors=["#000000"],
    background="#ffffff",
)

Limit hue ranges:

warm = create_palette(10, hue=(330, 100))
cool = create_palette(10, hue=(150, 280))

Common constraints:

muted = create_palette(12, chroma=(0.02, 0.12))
bright = create_palette(12, chroma=(0.10, None))
mid_lightness = create_palette(12, lightness=(0.30, 0.80))

lightness is OKLab L in 0..1. hue is OKLCH degrees in 0..360; ranges can wrap around zero.

Preview And Save

from okpalette import create_palette, save_palette, view_palette

colors = create_palette(12)

view_palette(colors)
save_palette(colors, "palette.svg")
save_palette(colors, "palette.png")

view_palette() works in notebooks through _repr_svg_() and _repr_png_().

For raw preview bytes:

from okpalette import palette_png, palette_svg

svg = palette_svg(colors)
png = palette_png(colors)

Color Inputs

Accepted color inputs:

"#0fA"
"00ffaa"
(255, 128, 0)
(1.0, 0.5, 0.0)

Integer RGB tuples use 0..255. Normalized RGB tuples use floats in 0.0..1.0. Ambiguous integer tuples such as (1, 0, 0) are rejected; write (1.0, 0.0, 0.0) for normalized RGB.

Grid Size

grid_size controls how many candidate colors are searched.

quick = create_palette(24, grid_size="coarse")  # step 16
default = create_palette(24, grid_size="medium")  # step 8
fine = create_palette(24, grid_size="fine")  # step 4
custom = create_palette(24, grid_size=12)

If constraints leave too few candidates, okpalette raises ValueError with a hint to relax lightness, chroma, hue, or grid_size.

API

create_palette(
    palette_size,
    *,
    seed_colors=(),
    avoid_colors=None,
    background="#ffffff",
    lightness=(0.20, 0.90),
    chroma=(0.04, None),
    hue=None,
    grid_size="medium",
    lightness_weight=1.0,
    chroma_weight=1.0,
    format="hex",
)
extend_palette(
    colors,
    target_size,
    *,
    include_existing=True,
    **create_palette_options,
)
view_palette(palette, *, width=1246, height=154)
palette_svg(palette, *, width=1246, height=154)
palette_png(palette, *, width=1246, height=154)
save_palette(palette, path, *, width=1246, height=154)

How It Works

okpalette uses a greedy Glasbey-style algorithm. It starts with anchor colors such as seeds, avoid colors, and the background, then repeatedly chooses the candidate color that is farthest from the nearest anchor or selected color.

Distances are measured in OKLab. Lightness, chroma, and hue constraints are applied through OKLab and OKLCH before colors are selected.

The result is deterministic, fast, and stable when extending a palette. It is not a global optimizer.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

okpalette-0.1.0.tar.gz (47.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

okpalette-0.1.0-cp312-abi3-win_amd64.whl (235.9 kB view details)

Uploaded CPython 3.12+Windows x86-64

okpalette-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (403.9 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

okpalette-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (399.3 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

okpalette-0.1.0-cp312-abi3-macosx_11_0_arm64.whl (350.1 kB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

File details

Details for the file okpalette-0.1.0.tar.gz.

File metadata

  • Download URL: okpalette-0.1.0.tar.gz
  • Upload date:
  • Size: 47.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for okpalette-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9bc9599f232c234616d2a9b9d33d0c409894d9418126b70af53e0a50b9cfc31f
MD5 9eaac4f73d11b186cdaf5893b1642732
BLAKE2b-256 5667490ed7cbcea8c399c02069fff847af33f7ffb258d200a4d14c0fcb08e7c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for okpalette-0.1.0.tar.gz:

Publisher: release.yml on pmbaumgartner/okpalette

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file okpalette-0.1.0-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: okpalette-0.1.0-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 235.9 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for okpalette-0.1.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 68189a76bc05cbc18eff326338b657b45750129a010e37e386b65dfd3138b223
MD5 8d3b872dc3ee2660dca61354fa45f646
BLAKE2b-256 c89b51abf4ba8a7c347b4343a7f268d5f42b5ecda6c9fbf252f7db4e9ae5d3bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for okpalette-0.1.0-cp312-abi3-win_amd64.whl:

Publisher: release.yml on pmbaumgartner/okpalette

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file okpalette-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for okpalette-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 40a0c2c478954323273869d04f5f63e46d585e558d62eb16fefd8e8527f29830
MD5 9ad83de466fafd46cd2c79e0abf2fd08
BLAKE2b-256 807fe7fa139394927be787f71285cf42dbc0e57c97406250205dc13034223f4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for okpalette-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on pmbaumgartner/okpalette

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file okpalette-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for okpalette-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ae98a61767931cf95523ed28f6f7065a17b3949c71a7c995bce9b278f6d73cf3
MD5 6e122e884cae3466aefd1267c2fed49c
BLAKE2b-256 95780c34b909466845558f3a1f397886423088c257e449763662269ebc79723a

See more details on using hashes here.

Provenance

The following attestation bundles were made for okpalette-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on pmbaumgartner/okpalette

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file okpalette-0.1.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for okpalette-0.1.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5e885933903350a7b89ed97cf6bd6bf2d653e95bf071aaa15a8829985746e2a1
MD5 3ac6b898d23c82cf9542febb5aeca126
BLAKE2b-256 17b337854c65b20f61ab3462d0f65465f138c328c43b739844cdfd7ab6b81857

See more details on using hashes here.

Provenance

The following attestation bundles were made for okpalette-0.1.0-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on pmbaumgartner/okpalette

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page