Skip to main content

lightweight python implementation of Björn Ottosson's oklab colour space

Project description

lightweight python implementation of Björn Ottosson's oklab/oklch colour space.

i like oklab but there isnt a straightforward python package for working with it. so i made it.

gamut fixed chroma demo gamut hull demo gamut clamp demo

install

install it from PyPI:

python -m pip install oklab

how to use?

use it in python:

import oklab

oklab.lch_to_hex((0.75, 0.125, 310.0))
# '#c697e9'

oklab.hex_to_lch('#ff0000')
# (0.6279536182521783, 0.25762679148825324, 29.227131291763712)

oklab.lab_to_xyz((1.0, 0.0, 0.0))
# (0.9504559270516719, 0.9999999999999998, 1.0890577507598782)

or use it in the terminal:

> oklab -i lch -o hex 0.75 0.125 310
#c697e9

> oklch ff0000
(0.6279536182521783 0.25762679148825324 29.227131291763712)

> oklab -i lab -o xyz 1.0 0.0 0.0
(0.9504559270516719, 0.9999999999999998, 1.0890577507598782)

try this fancy demo!

import os, oklab
C, R = os.get_terminal_size()
for row in range(R - 1, -1, -1):
    string = []
    for col in range(C):
        l = row / (R - 1) * 0.85 + 0.15
        c = 0.125
        h = col / (C - 1) * 360
        colour = oklab.lch_to_rgb((l, c, h))
        if not all(0 < c < 1 for c in colour):
			# out of gamut
            string.append(f' ')
            continue
        r, g, b = tuple(round(c * 255) for c in colour)
        string.append(f'\x1b[38;2;{r};{g};{b}m█')
    print(''.join(string))

features

  • ✅ 6 formats: rgb, hex, lab, lch, xyz, lms
  • ✅ 30 conversion functions (complete graph)
  • ✅ numerical accuracy + roundtrip correctness
  • ✅ source correctness
  • ✅ zero dependencies
  • ✅ CLI tool
  • ✅ python ≥3.8 compatibility
  • ✅ sRGB support
  • ✅ clamping with point projection in oklab
  • 🚧 P3 & rec2020 support
  • 🚧 clamping with chroma reduction in oklch
  • ❌ CMYK support (scope creep w/non-RGB spaces)

sources

conversion graph

here is a graph of the atomic conversions:

graph of formats and conversions

  • lab: oklab cartesian space
  • lch: oklch cylindrical space
  • lms: intermediate lms-like space
  • xyz: CIEXYZ space
  • rgb: sRGB
  • hex: 8-bit sRGB in hexadecimal

all composed conversions are derived via shortest path in this graph

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

oklab-0.3.0.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

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

oklab-0.3.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file oklab-0.3.0.tar.gz.

File metadata

  • Download URL: oklab-0.3.0.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for oklab-0.3.0.tar.gz
Algorithm Hash digest
SHA256 39fdd4d92d4e3c6cd4dd4cb6445ca4cfc46305d57ffd0da5ab041ca7c0facddc
MD5 24da1fac188fc8c0ee0a785aa7b47d82
BLAKE2b-256 e0fedaec9b2b15eacc7de4797ee86d499d4a5edc98a32dc5b789a7af12a800ab

See more details on using hashes here.

File details

Details for the file oklab-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: oklab-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for oklab-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38b4b3c65b68bbee090faa58d4ac56cc62368f14962818217d5de22d708f90ca
MD5 be73d760f4be7ee451eebf4975d2922d
BLAKE2b-256 2222c89b9f1003241bfe0a26c946e366baab706a3622376251ebc5136f377070

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