Skip to main content

Generate perceptually uniform colour ramps for data visualisation.

Project description

datahues

Generate perceptually uniform colour ramps for data visualisation.

Test Coverage Status License: MIT

Overview

datahues creates smooth, visually consistent colour gradients from a start colour to an end colour. Unlike simple RGB or HSL interpolation, which can produce perceptually uneven ramps with visible banding, this library leverages the Oklab colour space — a perceptually uniform space where equal mathematical changes correspond to equal perceptual colour differences.

Core Functions

generate_hex_list(start_hex, end_hex, n_stops=512)

Between start and end colours, generates a list of hex colour codes representing points along a smooth colour ramp. It is assumed the user wants a discrete number (n_stops) of points, so no warning is given when n_stops is small (unlike in generate_cmap below). However, if these hexes are being used to create a continuous colour ramp, it is recommended to use n_stops>=128.

  • Parameters:
    • start_hex: Starting colour as hex code (e.g., "#FF0000")
    • end_hex: Ending colour as hex code (e.g., "#0000FF")
    • n_stops: Number of colour stops in the ramp (default: 512)
  • Returns: List of hex colour codes representing the gradient

generate_cmap(start_hex, end_hex, n_stops=512, name="interp_ramp")

Betweem start and end colours, creates a Matplotlib LinearSegmentedColormap object, forming a smooth colour ramp.

  • Parameters:
    • start_hex, end_hex, n_stops: As above
    • name: The "name" of the colour ramp for matplotlib internal purposes (default: "interp_ramp")
  • Returns: LinearSegmentedColormap object ready to use in matplotlib visualisation
  • Warning: n_stops < 128 may produce visible banding; use larger values for smoother results

How It Works

The library converts colours through multiple colour spaces to achieve perceptual uniformity:

  1. Hex → RGB — Parse hex codes into normalised [0.0, 1.0] RGB values
  2. RGB → XYZ — Apply gamma correction and convert to CIE XYZ (D65 illuminant)
  3. XYZ → Oklab — Convert through LMS intermediate space to Oklab (perceptually uniform)
  4. Interpolate in Oklab — Create evenly-spaced values in perceptual space
  5. Oklab → RGB → Hex — Reverse conversion back to hex color codes

This approach ensures that visual transitions between colours feel smooth and natural across the entire gradient.

To Do

  • #TODO git tag versioning
  • #TODO publish to pypi
  • #TODO publish to conda-forge
  • #TODO example (with picture!) in README
  • #TODO blogpost

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

datahues-0.0.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

datahues-0.0.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file datahues-0.0.0.tar.gz.

File metadata

  • Download URL: datahues-0.0.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for datahues-0.0.0.tar.gz
Algorithm Hash digest
SHA256 35ce6e4f88dab3b28b22eb6a36139f67e73be3075268efeafae27d9ae246575c
MD5 f3239b690bceccf8170aec47722daf8e
BLAKE2b-256 b0a1b8edd735a0b6a36e60eedc1701653f4fb10fcf99befbf2cbd62bee820307

See more details on using hashes here.

File details

Details for the file datahues-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: datahues-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for datahues-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0b337a6a3c5353e47472d43ad1e82994e08aaf0025b69b50bbde85071d9d9ac
MD5 506ed0d9a714538e1b7d6d861abdde1e
BLAKE2b-256 2ee941ea3f399f82662f2f02069c033802184e0744e465df70a2cccaa11e3e0e

See more details on using hashes here.

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