oklab 0.2.1__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.
cli.py ADDED
@@ -0,0 +1,29 @@
1
+ import argparse, oklab
2
+
3
+ def oklab_main():
4
+ parser = argparse.ArgumentParser()
5
+ parser.add_argument("-i", required=True)
6
+ parser.add_argument("-o", required=True)
7
+ parser.add_argument("colour", nargs="+")
8
+
9
+ args = parser.parse_args()
10
+
11
+ func = getattr(oklab, f"{args.i}_to_{args.o}")
12
+
13
+ if len(args.colour) == 1:
14
+ colour = args.colour[0]
15
+ else:
16
+ colour = tuple(map(float, args.colour))
17
+
18
+ print(func(colour))
19
+
20
+ def oklch_main():
21
+ parser = argparse.ArgumentParser()
22
+ parser.add_argument("colour", nargs="+")
23
+
24
+ args = parser.parse_args()
25
+
26
+ if len(args.colour) == 1:
27
+ print(*oklab.hex_to_lch(args.colour[0]))
28
+ else:
29
+ print(oklab.lch_to_hex(tuple(map(float, args.colour))))
@@ -0,0 +1,98 @@
1
+ Metadata-Version: 2.4
2
+ Name: oklab
3
+ Version: 0.2.1
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
+ Requires-Python: >=3.8
17
+ Description-Content-Type: text/markdown
18
+
19
+ lightweight python implementation of [Björn Ottosson](https://bottosson.github.io/)'s oklab/oklch colour space.
20
+
21
+ i like oklab but there isnt a straightforward python package for working with it. so i made it.
22
+
23
+ # install
24
+
25
+ install it from [PyPI](https://pypi.org/project/oklab/):
26
+
27
+ ```sh
28
+ python -m pip install oklab
29
+ ```
30
+
31
+ # how to use?
32
+
33
+ use it in python:
34
+
35
+ ```python
36
+ import oklab
37
+
38
+ oklab.lch_to_hex((0.75, 0.125, 310.0))
39
+ # '#c697e9'
40
+
41
+ oklab.hex_to_lch('#ff0000')
42
+ # (0.6279536182521783, 0.25762679148825324, 29.227131291763712)
43
+
44
+ oklab.lab_to_xyz((1.0, 0.0, 0.0))
45
+ # (0.9504559270516719, 0.9999999999999998, 1.0890577507598782)
46
+ ```
47
+
48
+ or use it in the terminal:
49
+
50
+ ```shell
51
+ > oklab -i lch -o hex 0.75 0.125 310
52
+ #c697e9
53
+
54
+ > oklch ff0000
55
+ (0.6279536182521783 0.25762679148825324 29.227131291763712)
56
+
57
+ > oklab -i lab -o xyz 1.0 0.0 0.0
58
+ (0.9504559270516719, 0.9999999999999998, 1.0890577507598782)
59
+ ```
60
+
61
+ # features
62
+
63
+ - ✅ 6 formats: `rgb`, `hex`, `lab`, `lch`, `xyz`, `lms`
64
+ - ✅ 30 conversion functions (complete graph)
65
+ - ✅ numerical accuracy + roundtrip correctness
66
+ - ✅ source correctness
67
+ - ✅ zero dependencies
68
+ - ✅ CLI tool
69
+ - ✅ python ≥3.8 compatibility
70
+ - 🚧 clamping (in native oklab space via chroma reduction)
71
+ - 🚧 configurable RGB primaries (P3, rec2020, …)
72
+ - ❌ CMYK support (scope creep)
73
+
74
+ # sources
75
+
76
+ - function names inspired by [hsluv](https://github.com/hsluv/hsluv-python)
77
+ - outputs sanity-checked against [oklch.com](https://oklch.com/) and [wikipedia](https://en.wikipedia.org/wiki/Oklab_color_space)
78
+ - `M0` sourced from [Björn's CSSWG comment](https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-945714988)
79
+ - `M2_inv` sourced from [Björn's blogpost](https://bottosson.github.io/posts/oklab/)
80
+ - `XYZ_TO_RGB`, `RGB_TO_XYZ` sourced from [colour science](https://www.colour-science.org/)
81
+ - `M1`, `M1_inv`, `M2`, `RGB_TO_LMS`, `LMS_TO_RGB` derived numerically using [numpy](https://github.com/numpy/numpy)
82
+ - `test_oklab.test_xyz` sourced from [Björn's blogpost](https://bottosson.github.io/posts/oklab/)
83
+
84
+ # conversion graph
85
+
86
+ here is a graph of the atomic conversions:
87
+
88
+ ![graph of formats and conversions](https://github.com/deftasparagusanaconda/oklab/blob/main/graph.png)
89
+
90
+ - lab: oklab cartesian space
91
+ - lch: oklch cylindrical space
92
+ - lms: intermediate lms-like space
93
+ - xyz: CIEXYZ space
94
+ - rgb: sRGB
95
+ - hex: 8-bit sRGB in hexadecimal
96
+
97
+ all composed conversions are derived via shortest path in this graph
98
+
@@ -0,0 +1,7 @@
1
+ cli.py,sha256=5xy7Xd-efaONOY8IEDSrrG_VcQ9oRM5a9RmEMPnBTrU,735
2
+ oklab.py,sha256=IeEfZy2NAsd6_8qwwHApgQ3X32NR5Iqj1ghtlOme8pE,6693
3
+ oklab-0.2.1.dist-info/METADATA,sha256=CMfF54F8xlz5OQ5chAJY8RTQDns21dAQ0dTeC041JvA,3143
4
+ oklab-0.2.1.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
5
+ oklab-0.2.1.dist-info/entry_points.txt,sha256=rDxGmHOiqqD_qkVez4TbFPsmuJ_p5VP2xS1q2YHdnTM,64
6
+ oklab-0.2.1.dist-info/licenses/LICENSE,sha256=VB2xlcQdtOxvalPfPY4FykGdH2Aqqjr0NO9YeF_3cyk,1078
7
+ oklab-0.2.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ oklab = cli:oklab_main
3
+ oklch = cli:oklch_main
@@ -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,148 @@
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(sum(a * b for a, b in zip(row, vector)) for row in matrix)
10
+
11
+ # mysterious numbers from https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-945714988
12
+ M0 = ((+0.77849780, +0.34399940, -0.12249720),
13
+ (+0.03303601, +0.93076195, +0.03620204),
14
+ (+0.05092917, +0.27933344, +0.66973739))
15
+
16
+ # source of truth: https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab
17
+ M2_inv = ((+1.0 , +0.3963377774, +0.2158037573),
18
+ (+1.0 , -0.1055613458, -0.0638541728),
19
+ (+1.0 , -0.0894841775, -1.2914855480))
20
+
21
+ # M0 / numpy.outer(numpy.dot(M0, ((3127/3290, 1, (10000-3127-3290)/3290))), (1, 1, 1))
22
+ M1 = ((+0.819022437996703 , +0.3619062600528904, -0.1288737815209879 ),
23
+ (+0.03298365393238847, +0.9292868615863434, +0.03614466635064236),
24
+ (+0.04817718935962421, +0.2642395317527308, +0.6335478284694309 ))
25
+
26
+ # numpy.linalg.inv(M2_inv)
27
+ M2 = ((+0.21045426827458136 , +0.7936177747300267, -0.004072043004608034),
28
+ (+1.9779985323885083 , -2.428592241936286 , +0.450593709547778 ),
29
+ (+0.025904042487658187, +0.7827717124269178, -0.808675754914576 ))
30
+
31
+ # numpy.linalg.inv(M1)
32
+ M1_inv = ((+1.2268798758459243 , -0.5578149944602171, +0.2813910456659647 ),
33
+ (-0.04057574521480085, +1.112286803280317 , -0.07171105806551636),
34
+ (-0.07637293667466007, -0.4214933324022432, +1.5869240198367816 ))
35
+
36
+ def xyz_to_lms(xyz):
37
+ 'convert (x, y, z) to (l, m, s)'
38
+ return _matvec(M1, xyz)
39
+
40
+ def lms_to_xyz(lms):
41
+ 'convert (l, m, s) to (x, y ,z)'
42
+ return _matvec(M1_inv, lms)
43
+
44
+ def lms_to_lab(lms):
45
+ 'convert (l, m, s) to (l, a, b)'
46
+ return _matvec(M2, tuple(cone ** (1 / 3) for cone in lms))
47
+
48
+ def lab_to_lms(lab):
49
+ 'convert (l, a, b) to (l, m, s)'
50
+ return tuple(cone ** 3 for cone in _matvec(M2_inv, lab))
51
+
52
+ # extra ------------------------------------------------------------------------
53
+
54
+ # for convenience, we also want lch, rgb (srgb), hex (srgb), and p3, rec2020 in the future
55
+ # totally we have a directed graph of 8 formats and 56 possible conversions
56
+
57
+ def lch_to_lab(lch, *, degrees: bool = True):
58
+ 'convert (l, c, h) to (l, a, b)'
59
+ l, c, h = lch
60
+ h = math.radians(h) if degrees else h
61
+ a = c * math.cos(h)
62
+ b = c * math.sin(h)
63
+ return l, a, b
64
+
65
+ def lab_to_lch(lab, *, degrees: bool = True):
66
+ 'convert (l, a, b) to (l, c, h)'
67
+ l, a, b = lab
68
+ c = math.hypot(a, b)
69
+ h = math.atan2(b, a) % math.tau
70
+ h = math.degrees(h) if degrees else h
71
+ return l, c, h
72
+
73
+ def rgb_to_hex(rgb: tuple) -> str:
74
+ "convert (r, g, b) to '#rrggbb' (rounding to 8 bits)"
75
+ #return '#' + ''.join(f'{round(channel * 255):02x}' for channel in rgb)
76
+ return f'#{round(rgb[0] * 255):02x}{round(rgb[1] * 255):02x}{round(rgb[2] * 255):02x}'
77
+
78
+ def hex_to_rgb(hex: str) -> tuple:
79
+ "convert '#rrggbb' to (r, g, b)"
80
+ return tuple(int(hex.lstrip('#')[i:i+2], base=16) / 255 for i in (0, 2, 4))
81
+
82
+ # from colour_science: colour.RGB_COLOURSPACES['sRGB']._derived_matrix_RGB_to_XYZ
83
+ RGB_TO_XYZ = ((+0.41239079926595934 , +0.35758433938387796, +0.1804807884018343 ),
84
+ (+0.2126390058715103 , +0.7151686787677559 , +0.07219231536073371),
85
+ (+0.019330818715591825, +0.11919477979462595, +0.9505321522496606 ))
86
+
87
+ # from colour_science: colour.RGB_COLOURSPACES['sRGB']._derived_matrix_XYZ_to_RGB
88
+ XYZ_TO_RGB = ((+3.2409699419045226 , -1.537383177570094 , -0.49861076029300344),
89
+ (-0.9692436362808798 , +1.8759675015077206 , +0.04155505740717563),
90
+ (+0.05563007969699364 , -0.20397695888897655, +1.0569715142428786 ))
91
+
92
+ def eotf_srgb(x, *, A = 12.92, C = 0.055, γ = 2.4, X = 0.04045):
93
+ 'convert gamma-encoded sRGB to linear sRGB'
94
+ return x / A if x <= X else ((x + C) / (1 + C)) ** γ
95
+
96
+ def oetf_srgb(y, *, A = 12.92, C = 0.055, γ = 2.4, Y = 0.0031308):
97
+ 'convert linear sRGB to gamma-encoded sRGB'
98
+ return y * A if y <= Y else (1 + C) * y ** (1 / γ) - C
99
+
100
+ def rgb_to_xyz(rgb):
101
+ 'convert (r, g, b) in sRGB to (x, y, z)'
102
+ return _matvec(RGB_TO_XYZ, tuple(map(eotf_srgb, rgb)))
103
+
104
+ def xyz_to_rgb(xyz):
105
+ 'convert (x, y, z) to (r, g, b) in sRGB'
106
+ return tuple(map(oetf_srgb, _matvec(XYZ_TO_RGB, xyz)))
107
+
108
+ # numpy.matmul(M1, _RGB_TO_XYZ)
109
+ RGB_TO_LMS = ((+0.4121764591770371 , +0.536273974269589 , +0.05144037229550145),
110
+ (+0.2119091995880486 , +0.680717870982313 , +0.10739984387775395),
111
+ (+0.08834481407213206 , +0.28185396309857735, +0.6302808688015095 ))
112
+
113
+ # numpy.matmul(_XYZ_TO_RGB, M1_inv)
114
+ LMS_TO_RGB = ((+4.077186823717315 , -3.307622521664363 , +0.2308591954879519 ),
115
+ (-1.2685764914005104 , +2.609687114485009 , -0.34115574866072784),
116
+ (-0.004196542231656323, -0.7033996761010274 , +1.7067960338654136 ))
117
+
118
+ def rgb_to_lms(rgb):
119
+ 'convert (r, g, b) in sRGB to (l, m, s)'
120
+ return _matvec(RGB_TO_LMS, tuple(map(eotf_srgb, rgb)))
121
+
122
+ def lms_to_rgb(lms):
123
+ 'convert (l, m, s) to (r, g, b) in sRGB'
124
+ return tuple(map(oetf_srgb, _matvec(LMS_TO_RGB, lms)))
125
+
126
+ # conveniences -----------------------------------------------------------------
127
+
128
+ # with 6 formats, we now have 12 conversions out of a total of 6 * (6 - 1) = 30
129
+ # lets derive the remaining 18
130
+
131
+ def lab_to_xyz(lab): return lms_to_xyz(lab_to_lms(lab))
132
+ def xyz_to_lab(xyz): return lms_to_lab(xyz_to_lms(xyz))
133
+ def lch_to_lms(lch): return lab_to_lms(lch_to_lab(lch))
134
+ def lms_to_lch(lms): return lab_to_lch(lms_to_lab(lms))
135
+ def hex_to_lms(hex): return rgb_to_lms(hex_to_rgb(hex))
136
+ def lms_to_hex(lms): return rgb_to_hex(lms_to_rgb(lms))
137
+ def xyz_to_hex(xyz): return lms_to_hex(xyz_to_lms(xyz))
138
+ def hex_to_xyz(hex): return lms_to_xyz(hex_to_lms(hex))
139
+ def rgb_to_lab(rgb): return lms_to_lab(rgb_to_lms(rgb))
140
+ def lab_to_rgb(lab): return lms_to_rgb(lab_to_lms(lab))
141
+ def rgb_to_lch(rgb): return lab_to_lch(lms_to_lab(rgb_to_lms(rgb)))
142
+ def lch_to_rgb(lch): return lms_to_rgb(lab_to_lms(lch_to_lab(lch)))
143
+ def xyz_to_lch(xyz): return lab_to_lch(lms_to_lab(xyz_to_lms(xyz)))
144
+ def lch_to_xyz(lch): return lms_to_xyz(lab_to_lms(lch_to_lab(lch)))
145
+ def lab_to_hex(lab): return rgb_to_hex(lms_to_rgb(lab_to_lms(lab)))
146
+ def hex_to_lab(hex): return lms_to_lab(rgb_to_lms(hex_to_rgb(hex)))
147
+ def lch_to_hex(lch): return rgb_to_hex(lms_to_rgb(lab_to_lms(lch_to_lab(lch))))
148
+ def hex_to_lch(hex): return lab_to_lch(lms_to_lab(rgb_to_lms(hex_to_rgb(hex))))