Skip to main content

Grayscale-safe color palettes from cubehelix.

Project description

cubehelix-studio

Cubehelix color palettes that survive grayscale, colorblindness, and print. Zero runtime dependencies; matplotlib integration available behind an optional extra.

Interactive playground: obang.pub/cubehelix-studio — adjust the parameters live in your browser.

Install

pip install cubehelix-studio            # core only
pip install "cubehelix-studio[viz]"     # with matplotlib adapter
# or with uv:
uv add cubehelix-studio
uv add "cubehelix-studio[viz]"

Usage

from cubehelix_studio import CubehelixParams, cubehelix, sample_sequential, to_hex

# Single point on the curve
mid = cubehelix(0.5, CubehelixParams())

# 9 evenly-spaced samples (endpoints included)
palette = sample_sequential(CubehelixParams(), 9)
[to_hex(c) for c in palette]
# => ['#000000', '#1c1338', ..., '#ffffff']

Matplotlib integration

from cubehelix_studio import CubehelixParams, to_matplotlib_colormap
import matplotlib.pyplot as plt
import numpy as np

cmap = to_matplotlib_colormap(CubehelixParams(rotations=-2.0, saturation=1.2))
plt.imshow(np.random.rand(64, 64), cmap=cmap)
plt.colorbar()
plt.show()

API

CubehelixParams

Frozen dataclass with fields:

  • start: float = 0.5 - starting hue position
  • rotations: float = -1.5 - rotations through the color wheel
  • saturation: float = 1.0 - chromatic amplitude
  • gamma: float = 1.0 - gamma correction

Note: matplotlib's cubehelix_palette and Dave Green's 2011 paper call the saturation field hue. It controls chromatic amplitude — when zero, the output is a pure greyscale ramp — so saturation is more accurate.

cubehelix(t, params) -> tuple[float, float, float]

Returns the (r, g, b) triple at fraction t in [0, 1]. Each channel is clamped to [0, 1].

sample_sequential(params, n) -> list[tuple[float, float, float]]

Returns n colors at evenly-spaced positions i / (n - 1). Requires n >= 2.

to_hex(rgb) -> str

Formats an RGB triple as #rrggbb.

to_rgb_255(rgb) -> tuple[int, int, int]

Returns the integer 0-255 form of an RGB triple.

contrast_ratio(a, b) -> float

WCAG 2.1 contrast ratio in [1.0, 21.0].

pick_text_color(bg, candidates=None) -> tuple[float, float, float]

Returns whichever candidate has the highest contrast against bg. Defaults to [white, black].

to_matplotlib_colormap(params, n=256, name="cubehelix_studio")

Returns a matplotlib.colors.LinearSegmentedColormap. Requires the [viz] extra.

Parity with @cubehelix-studio/core

This package's math is verified against the same JSON fixture that the TypeScript reference implementation generates (fixtures/parity.json at the repo root). Both implementations must agree to within 1e-12 per channel for every parameter combination in the fixture.

License

MIT

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

cubehelix_studio-0.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

cubehelix_studio-0.0.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file cubehelix_studio-0.0.1.tar.gz.

File metadata

  • Download URL: cubehelix_studio-0.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for cubehelix_studio-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4d75133e840f8c54e6af37e87e8e02d8c0bf71a1a1aa2c51e8e5b32d81886d36
MD5 229520a6e8f8c5f0454133966376821e
BLAKE2b-256 091660986eb47d19bdc919894880bf69d8245491b509c33450c97e8bdb70147d

See more details on using hashes here.

Provenance

The following attestation bundles were made for cubehelix_studio-0.0.1.tar.gz:

Publisher: release.yml on obangpub/cubehelix-studio

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

File details

Details for the file cubehelix_studio-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cubehelix_studio-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1354d2cc1dafe1e5a40de2b7a14ab898929ab3face4b6a20d38b20f2e5623ca3
MD5 b911ae4587c1069f805c106807959ad2
BLAKE2b-256 81cec4ef56abe76c41c3156210087a73e783e728643b28572e0170418e3de586

See more details on using hashes here.

Provenance

The following attestation bundles were made for cubehelix_studio-0.0.1-py3-none-any.whl:

Publisher: release.yml on obangpub/cubehelix-studio

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