Skip to main content

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

i like oklab but there isnt a straightforward python package for it, so i made it

palette
hull
alpines

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

  • ✅ 8 formats: lab, lch, hex, rgb, dp3, rec, xyz, lms

  • ✅ 26 conversion functions

  • ✅ high numerical stability

  • ✅ zero dependencies

  • ✅ sRGB, Display P3, ITU-R BT.2020 (Rec. 2020) support

  • ✅ clamping: chroma reduction (default), point projection

  • 🚧 python ≥3.11 compatibility

  • 🚧 CLI convenience tools (still being polished)

  • 🚧 reference-grade correctness (not yet verified by Björn & CSSWG)

  • 🚧 clamping: MINDE, CSS-style JND approximation

  • ❌ CMYK support (non-RGB spaces and ICC profiles cause scope creep)

sources

conversions

the following conversions are maintained:

graph of formats and conversions

trivial non-oklab-related conversions such as rgb_to_xyz are not included

Release files for oklab 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for oklab 0.5.0
File Size Uploaded
oklab-0.5.0.tar.gz 25.8 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for oklab 0.5.0
File Interpreter ABI Platform
oklab-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.8 MB

Release files / oklab-0.5.0.tar.gz

Download URL oklab-0.5.0.tar.gz
Size 25.8 MB
Tags Source
SHA-256 checksum
How to use checksums
2ce9d8e74cb8f524f68a9f73b007ebc2190405367cce3ea39eb021e9aa082581
BLAKE2b-256 checksum
How to use checksums
4a884364259babebf77a3fda47ad7fa4fbd18468162f27cd16f84efb25c01ba2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.5

Release files / oklab-0.5.0-py3-none-any.whl

Download URL oklab-0.5.0-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
281aed2718203e8111a494002500f7e27e61f49393139ab492ae0139f01d0dee
BLAKE2b-256 checksum
How to use checksums
6ef75a26815d197a1b24139031f682cbf46ec7fc48a160d9be24e38b491ff458
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.5

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page