oklab 0.1.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oklab-0.1.0.dist-info/METADATA +80 -0
- oklab-0.1.0.dist-info/RECORD +5 -0
- oklab-0.1.0.dist-info/WHEEL +5 -0
- oklab-0.1.0.dist-info/licenses/LICENSE +21 -0
- oklab.py +156 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: oklab
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: lightweight python implementation of Björn Ottosson's oklab colour space
|
|
5
|
+
Project-URL: Homepage, https://github.com/deftasparagusanaconda/oklab
|
|
6
|
+
Project-URL: Repository, https://github.com/deftasparagusanaconda/oklab
|
|
7
|
+
Project-URL: Issues, https://github.com/deftasparagusanaconda/oklab/issues
|
|
8
|
+
Author: deftasparagusanaconda
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: color,colour,oklab,oklch
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Multimedia :: Graphics
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
lightweight python implementation of [Björn Ottosson](https://bottosson.github.io/)'s oklab/oklch colour space.
|
|
19
|
+
|
|
20
|
+
i like oklab but there isnt a straightforward python package for working with it. so i made it.
|
|
21
|
+
|
|
22
|
+
# install
|
|
23
|
+
|
|
24
|
+
install it from [PyPI](https://pypi.org/project/oklab/):
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
python -m pip install oklab
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
# how to use?
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import oklab
|
|
34
|
+
|
|
35
|
+
oklab.lch_to_hex((0.75, 0.125, 310.0))
|
|
36
|
+
# '#c697e9'
|
|
37
|
+
|
|
38
|
+
oklab.hex_to_lch('#ff0000')
|
|
39
|
+
# (0.6279536182521783, 0.25762679148825324, 29.227131291763712)
|
|
40
|
+
|
|
41
|
+
oklab.lab_to_xyz((1.0, 0.0, 0.0))
|
|
42
|
+
# (0.9504559270516719, 0.9999999999999998, 1.0890577507598782)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
# features
|
|
46
|
+
|
|
47
|
+
- [x] 6 formats: `rgb`, `hex`, `lab`, `lch`, `xyz`, `lms`
|
|
48
|
+
- [x] 30 conversion functions (complete graph)
|
|
49
|
+
- [x] numerical accuracy + roundtrip precision
|
|
50
|
+
- [x] source correctness
|
|
51
|
+
- [x] zero dependencies
|
|
52
|
+
- [ ] clamping (in native oklab space via chroma reduction)
|
|
53
|
+
- [ ] configurable RGB primaries (P3, rec2020, …)
|
|
54
|
+
- [❌] CMYK support (scope creep)
|
|
55
|
+
|
|
56
|
+
# sources
|
|
57
|
+
|
|
58
|
+
- function names inspired by [hsluv](https://github.com/hsluv/hsluv-python)
|
|
59
|
+
- outputs sanity-checked against [oklch.com](https://oklch.com/) and [wikipedia](https://en.wikipedia.org/wiki/Oklab_color_space)
|
|
60
|
+
- `M0` sourced from [Björn's CSSWG comment](https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-945714988)
|
|
61
|
+
- `M2_inv` sourced from [Björn's blogpost](https://bottosson.github.io/posts/oklab/)
|
|
62
|
+
- `XYZ_TO_RGB`, `RGB_TO_XYZ` sourced from [colour science](https://www.colour-science.org/)
|
|
63
|
+
- `M1`, `M1_inv`, `M2`, `RGB_TO_LMS`, `LMS_TO_RGB` derived numerically using [numpy](https://github.com/numpy/numpy)
|
|
64
|
+
- tests sourced from [Björn's blogpost](https://bottosson.github.io/posts/oklab/)
|
|
65
|
+
|
|
66
|
+
# conversion graph
|
|
67
|
+
|
|
68
|
+
here is a graph of the atomic conversions:
|
|
69
|
+
|
|
70
|
+

|
|
71
|
+
|
|
72
|
+
- lab: oklab cartesian space
|
|
73
|
+
- lch: oklch cylindrical space
|
|
74
|
+
- lms: intermediate lms-like space
|
|
75
|
+
- xyz: CIEXYZ space
|
|
76
|
+
- rgb: sRGB
|
|
77
|
+
- hex: 8-bit sRGB in hexadecimal
|
|
78
|
+
|
|
79
|
+
all composed conversions are derived via shortest path in this graph
|
|
80
|
+
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
oklab.py,sha256=bOveTff8-JzSqXfcqMwqtuDP0d7JuiglXN5ezqOc2gE,6871
|
|
2
|
+
oklab-0.1.0.dist-info/METADATA,sha256=1VwoZX53haecoZtzkR0cfekHOYff7N5C0xEk168iWg0,2773
|
|
3
|
+
oklab-0.1.0.dist-info/WHEEL,sha256=VX-VJ7c6dw9Ge3EqJIbA6W3pOUbz24SnnGGFNr55jY4,105
|
|
4
|
+
oklab-0.1.0.dist-info/licenses/LICENSE,sha256=VB2xlcQdtOxvalPfPY4FykGdH2Aqqjr0NO9YeF_3cyk,1078
|
|
5
|
+
oklab-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lalremruata Chongmang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
oklab.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"lightweight python implementation of Björn Ottosson's oklab colour space"
|
|
2
|
+
|
|
3
|
+
# typehinting tuple[float, float, float] everywhere is messy
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
|
|
7
|
+
def matvec(matrix, vector):
|
|
8
|
+
'matrix × vector'
|
|
9
|
+
return tuple(math.sumprod(row, vector) for row in matrix)
|
|
10
|
+
|
|
11
|
+
def root(a, b):
|
|
12
|
+
'a ** (1 / b)'
|
|
13
|
+
|
|
14
|
+
# this is proven (from daamath) to be bad
|
|
15
|
+
#return math.exp(math.log(a) / b)
|
|
16
|
+
|
|
17
|
+
return a ** (1 / b)
|
|
18
|
+
|
|
19
|
+
# mysterious numbers from https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-945714988
|
|
20
|
+
M0 = ((+0.77849780, +0.34399940, -0.12249720),
|
|
21
|
+
(+0.03303601, +0.93076195, +0.03620204),
|
|
22
|
+
(+0.05092917, +0.27933344, +0.66973739))
|
|
23
|
+
|
|
24
|
+
# source of truth: https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab
|
|
25
|
+
M2_inv = ((+1.0 , +0.3963377774, +0.2158037573),
|
|
26
|
+
(+1.0 , -0.1055613458, -0.0638541728),
|
|
27
|
+
(+1.0 , -0.0894841775, -1.2914855480))
|
|
28
|
+
|
|
29
|
+
# M0 / numpy.outer(numpy.dot(M0, ((3127/3290, 1, (10000-3127-3290)/3290))), (1, 1, 1))
|
|
30
|
+
M1 = ((+0.819022437996703 , +0.3619062600528904, -0.1288737815209879 ),
|
|
31
|
+
(+0.03298365393238847, +0.9292868615863434, +0.03614466635064236),
|
|
32
|
+
(+0.04817718935962421, +0.2642395317527308, +0.6335478284694309 ))
|
|
33
|
+
|
|
34
|
+
# numpy.linalg.inv(M2_inv)
|
|
35
|
+
M2 = ((+0.21045426827458136 , +0.7936177747300267, -0.004072043004608034),
|
|
36
|
+
(+1.9779985323885083 , -2.428592241936286 , +0.450593709547778 ),
|
|
37
|
+
(+0.025904042487658187, +0.7827717124269178, -0.808675754914576 ))
|
|
38
|
+
|
|
39
|
+
# numpy.linalg.inv(M1)
|
|
40
|
+
M1_inv = ((+1.2268798758459243 , -0.5578149944602171, +0.2813910456659647 ),
|
|
41
|
+
(-0.04057574521480085, +1.112286803280317 , -0.07171105806551636),
|
|
42
|
+
(-0.07637293667466007, -0.4214933324022432, +1.5869240198367816 ))
|
|
43
|
+
|
|
44
|
+
def xyz_to_lms(xyz):
|
|
45
|
+
'convert (x, y, z) to (l, m, s)'
|
|
46
|
+
return matvec(M1, xyz)
|
|
47
|
+
|
|
48
|
+
def lms_to_xyz(lms):
|
|
49
|
+
'convert (l, m, s) to (x, y ,z)'
|
|
50
|
+
return matvec(M1_inv, lms)
|
|
51
|
+
|
|
52
|
+
def lms_to_lab(lms):
|
|
53
|
+
'convert (l, m, s) to (l, a, b)'
|
|
54
|
+
return matvec(M2, tuple(math.cbrt(cone) for cone in lms))
|
|
55
|
+
|
|
56
|
+
def lab_to_lms(lab):
|
|
57
|
+
'convert (l, a, b) to (l, m, s)'
|
|
58
|
+
return tuple(cone ** 3 for cone in matvec(M2_inv, lab))
|
|
59
|
+
|
|
60
|
+
# extra ------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
# for convenience, we also want lch, rgb (srgb), hex (srgb), and p3, rec2020 in the future
|
|
63
|
+
# totally we have a directed graph of 8 formats and 56 possible conversions
|
|
64
|
+
|
|
65
|
+
def lch_to_lab(lch, *, degrees: bool = True):
|
|
66
|
+
'convert (l, c, h) to (l, a, b)'
|
|
67
|
+
l, c, h = lch
|
|
68
|
+
h = math.radians(h) if degrees else h
|
|
69
|
+
a = c * math.cos(h)
|
|
70
|
+
b = c * math.sin(h)
|
|
71
|
+
return l, a, b
|
|
72
|
+
|
|
73
|
+
def lab_to_lch(lab, *, degrees: bool = True):
|
|
74
|
+
'convert (l, a, b) to (l, c, h)'
|
|
75
|
+
l, a, b = lab
|
|
76
|
+
c = math.hypot(a, b)
|
|
77
|
+
h = math.atan2(b, a) % math.tau
|
|
78
|
+
h = math.degrees(h) if degrees else h
|
|
79
|
+
return l, c, h
|
|
80
|
+
|
|
81
|
+
def rgb_to_hex(rgb: tuple[float, float, float]) -> str:
|
|
82
|
+
"convert (r, g, b) to '#rrggbb' (rounding to 8 bits)"
|
|
83
|
+
#return '#' + ''.join(f'{round(channel * 255):02x}' for channel in rgb)
|
|
84
|
+
return f'#{round(rgb[0] * 255):02x}{round(rgb[1] * 255):02x}{round(rgb[2] * 255):02x}'
|
|
85
|
+
|
|
86
|
+
def hex_to_rgb(hex: str) -> tuple[float, float, float]:
|
|
87
|
+
"convert '#rrggbb' to (r, g, b)"
|
|
88
|
+
return tuple(int(hex.lstrip('#')[i:i+2], base=16) / 255 for i in (0, 2, 4))
|
|
89
|
+
|
|
90
|
+
# from colour_science: colour.RGB_COLOURSPACES['sRGB']._derived_matrix_RGB_to_XYZ
|
|
91
|
+
RGB_TO_XYZ = ((+0.41239079926595934 , +0.35758433938387796, +0.1804807884018343 ),
|
|
92
|
+
(+0.2126390058715103 , +0.7151686787677559 , +0.07219231536073371),
|
|
93
|
+
(+0.019330818715591825, +0.11919477979462595, +0.9505321522496606 ))
|
|
94
|
+
|
|
95
|
+
# from colour_science: colour.RGB_COLOURSPACES['sRGB']._derived_matrix_XYZ_to_RGB
|
|
96
|
+
XYZ_TO_RGB = ((+3.2409699419045226 , -1.537383177570094 , -0.49861076029300344),
|
|
97
|
+
(-0.9692436362808798 , +1.8759675015077206 , +0.04155505740717563),
|
|
98
|
+
(+0.05563007969699364 , -0.20397695888897655, +1.0569715142428786 ))
|
|
99
|
+
|
|
100
|
+
def eotf_srgb(x, *, A = 12.92, C = 0.055, γ = 2.4, X = 0.04045):
|
|
101
|
+
'convert gamma-encoded sRGB to linear sRGB'
|
|
102
|
+
return x / A if x <= X else ((x + C) / (1 + C)) ** γ
|
|
103
|
+
|
|
104
|
+
def oetf_srgb(y, *, A = 12.92, C = 0.055, γ = 2.4, Y = 0.0031308):
|
|
105
|
+
'convert linear sRGB to gamma-encoded sRGB'
|
|
106
|
+
return y * A if y <= Y else math.fma(1 + C, root(y, γ), -C)
|
|
107
|
+
|
|
108
|
+
def rgb_to_xyz(rgb):
|
|
109
|
+
'convert (r, g, b) in sRGB to (x, y, z)'
|
|
110
|
+
return matvec(RGB_TO_XYZ, tuple(map(eotf_srgb, rgb)))
|
|
111
|
+
|
|
112
|
+
def xyz_to_rgb(xyz):
|
|
113
|
+
'convert (x, y, z) to (r, g, b) in sRGB'
|
|
114
|
+
return tuple(map(oetf_srgb, matvec(XYZ_TO_RGB, xyz)))
|
|
115
|
+
|
|
116
|
+
# numpy.matmul(M1, _RGB_TO_XYZ)
|
|
117
|
+
RGB_TO_LMS = ((+0.4121764591770371 , +0.536273974269589 , +0.05144037229550145),
|
|
118
|
+
(+0.2119091995880486 , +0.680717870982313 , +0.10739984387775395),
|
|
119
|
+
(+0.08834481407213206 , +0.28185396309857735, +0.6302808688015095 ))
|
|
120
|
+
|
|
121
|
+
# numpy.matmul(_XYZ_TO_RGB, M1_inv)
|
|
122
|
+
LMS_TO_RGB = ((+4.077186823717315 , -3.307622521664363 , +0.2308591954879519 ),
|
|
123
|
+
(-1.2685764914005104 , +2.609687114485009 , -0.34115574866072784),
|
|
124
|
+
(-0.004196542231656323, -0.7033996761010274 , +1.7067960338654136 ))
|
|
125
|
+
|
|
126
|
+
def rgb_to_lms(rgb):
|
|
127
|
+
'convert (r, g, b) in sRGB to (l, m, s)'
|
|
128
|
+
return matvec(RGB_TO_LMS, tuple(map(eotf_srgb, rgb)))
|
|
129
|
+
|
|
130
|
+
def lms_to_rgb(lms):
|
|
131
|
+
'convert (l, m, s) to (r, g, b) in sRGB'
|
|
132
|
+
return tuple(map(oetf_srgb, matvec(LMS_TO_RGB, lms)))
|
|
133
|
+
|
|
134
|
+
# conveniences -----------------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
# with 6 formats, we now have 12 conversions out of a total of 6 * (6 - 1) = 30
|
|
137
|
+
# lets derive the remaining 18
|
|
138
|
+
|
|
139
|
+
def lab_to_xyz(lab): return lms_to_xyz(lab_to_lms(lab))
|
|
140
|
+
def xyz_to_lab(xyz): return lms_to_lab(xyz_to_lms(xyz))
|
|
141
|
+
def lch_to_lms(lch): return lab_to_lms(lch_to_lab(lch))
|
|
142
|
+
def lms_to_lch(lms): return lab_to_lch(lms_to_lab(lms))
|
|
143
|
+
def hex_to_lms(hex): return rgb_to_lms(hex_to_rgb(hex))
|
|
144
|
+
def lms_to_hex(lms): return rgb_to_hex(lms_to_rgb(lms))
|
|
145
|
+
def xyz_to_hex(xyz): return lms_to_hex(xyz_to_lms(xyz))
|
|
146
|
+
def hex_to_xyz(hex): return lms_to_xyz(hex_to_lms(hex))
|
|
147
|
+
def rgb_to_lab(rgb): return lms_to_lab(rgb_to_lms(rgb))
|
|
148
|
+
def lab_to_rgb(lab): return lms_to_rgb(lab_to_lms(lab))
|
|
149
|
+
def rgb_to_lch(rgb): return lab_to_lch(lms_to_lab(rgb_to_lms(rgb)))
|
|
150
|
+
def lch_to_rgb(lch): return lms_to_rgb(lab_to_lms(lch_to_lab(lch)))
|
|
151
|
+
def xyz_to_lch(xyz): return lab_to_lch(lms_to_lab(xyz_to_lms(xyz)))
|
|
152
|
+
def lch_to_xyz(lch): return lms_to_xyz(lab_to_lms(lch_to_lab(lch)))
|
|
153
|
+
def lab_to_hex(lab): return rgb_to_hex(lms_to_rgb(lab_to_lms(lab)))
|
|
154
|
+
def hex_to_lab(hex): return lms_to_lab(rgb_to_lms(hex_to_rgb(hex)))
|
|
155
|
+
def lch_to_hex(lch): return rgb_to_hex(lms_to_rgb(lab_to_lms(lch_to_lab(lch))))
|
|
156
|
+
def hex_to_lch(hex): return lab_to_lch(lms_to_lab(rgb_to_lms(hex_to_rgb(hex))))
|