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 it, so i made it
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:
oklab,oklch,hex,srgb,dp3,bt2020,xyz,lms -
✅ 26 conversion functions
-
✅ high numerical stability
-
✅ zero dependencies
-
✅ python ≥3.8 compatibility
-
✅ sRGB, Display P3, ITU-R BT.2020 (Rec. 2020) support
-
✅ clamping: chroma reduction (default), point projection
-
🚧 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
- function names inspired by hsluv
- outputs sanity-checked against oklch.com and wikipedia
M0sourced from Björn's CSSWG commentM2_invsourced from Björn's blogpost- matrices sourced from colour science
M1,M1_inv,M2, … derived numerically using numpytest_oklab.test_xyzsourced from Björn's blogpost
conversions
the following conversions are maintained:
trivial non-oklab-related conversions such as srgb_to_xyz are not included.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file oklab-0.4.0.tar.gz.
File metadata
- Download URL: oklab-0.4.0.tar.gz
- Upload date:
- Size: 2.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52e43d0e7a6ef61733505ea34270aa59ed42a49d472ff0224b5dd0fc43010148
|
|
| MD5 |
7a74145b19fefc2d9dacf638274f4c19
|
|
| BLAKE2b-256 |
6ddda92c514c79ec51e6b438b460308e57837cfa29224be1c7cf9fbf18be9863
|
File details
Details for the file oklab-0.4.0-py3-none-any.whl.
File metadata
- Download URL: oklab-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7897c1c069eca8bbd7cbc8d8d28f2cca48cdb88062a368aa4e4794415aaa40f1
|
|
| MD5 |
d29ab58eac578fc4f713eb28ccff5bcf
|
|
| BLAKE2b-256 |
2a32850c87f2ccaa9a7538ac1cbe17dc3b12bf6af35f373b73c21304c6f3ef64
|