oklab 0.1.1__tar.gz
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.1/.gitignore +218 -0
- oklab-0.1.1/LICENSE +21 -0
- oklab-0.1.1/PKG-INFO +81 -0
- oklab-0.1.1/graph.png +0 -0
- oklab-0.1.1/oklab.py +156 -0
- oklab-0.1.1/old.py +23 -0
- oklab-0.1.1/pyproject.toml +35 -0
- oklab-0.1.1/readme.md +64 -0
- oklab-0.1.1/test_oklab.py +39 -0
- oklab-0.1.1/tox.ini +6 -0
oklab-0.1.1/.gitignore
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
# Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
# poetry.lock
|
|
109
|
+
# poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
# pdm.lock
|
|
116
|
+
# pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
# pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# Redis
|
|
135
|
+
*.rdb
|
|
136
|
+
*.aof
|
|
137
|
+
*.pid
|
|
138
|
+
|
|
139
|
+
# RabbitMQ
|
|
140
|
+
mnesia/
|
|
141
|
+
rabbitmq/
|
|
142
|
+
rabbitmq-data/
|
|
143
|
+
|
|
144
|
+
# ActiveMQ
|
|
145
|
+
activemq-data/
|
|
146
|
+
|
|
147
|
+
# SageMath parsed files
|
|
148
|
+
*.sage.py
|
|
149
|
+
|
|
150
|
+
# Environments
|
|
151
|
+
.env
|
|
152
|
+
.envrc
|
|
153
|
+
.venv
|
|
154
|
+
env/
|
|
155
|
+
venv/
|
|
156
|
+
ENV/
|
|
157
|
+
env.bak/
|
|
158
|
+
venv.bak/
|
|
159
|
+
|
|
160
|
+
# Spyder project settings
|
|
161
|
+
.spyderproject
|
|
162
|
+
.spyproject
|
|
163
|
+
|
|
164
|
+
# Rope project settings
|
|
165
|
+
.ropeproject
|
|
166
|
+
|
|
167
|
+
# mkdocs documentation
|
|
168
|
+
/site
|
|
169
|
+
|
|
170
|
+
# mypy
|
|
171
|
+
.mypy_cache/
|
|
172
|
+
.dmypy.json
|
|
173
|
+
dmypy.json
|
|
174
|
+
|
|
175
|
+
# Pyre type checker
|
|
176
|
+
.pyre/
|
|
177
|
+
|
|
178
|
+
# pytype static type analyzer
|
|
179
|
+
.pytype/
|
|
180
|
+
|
|
181
|
+
# Cython debug symbols
|
|
182
|
+
cython_debug/
|
|
183
|
+
|
|
184
|
+
# PyCharm
|
|
185
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
186
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
188
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
189
|
+
# .idea/
|
|
190
|
+
|
|
191
|
+
# Abstra
|
|
192
|
+
# Abstra is an AI-powered process automation framework.
|
|
193
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
194
|
+
# Learn more at https://abstra.io/docs
|
|
195
|
+
.abstra/
|
|
196
|
+
|
|
197
|
+
# Visual Studio Code
|
|
198
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
199
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
200
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
201
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
202
|
+
# .vscode/
|
|
203
|
+
# Temporary file for partial code execution
|
|
204
|
+
tempCodeRunnerFile.py
|
|
205
|
+
|
|
206
|
+
# Ruff stuff:
|
|
207
|
+
.ruff_cache/
|
|
208
|
+
|
|
209
|
+
# PyPI configuration file
|
|
210
|
+
.pypirc
|
|
211
|
+
|
|
212
|
+
# Marimo
|
|
213
|
+
marimo/_static/
|
|
214
|
+
marimo/_lsp/
|
|
215
|
+
__marimo__/
|
|
216
|
+
|
|
217
|
+
# Streamlit
|
|
218
|
+
.streamlit/secrets.toml
|
oklab-0.1.1/LICENSE
ADDED
|
@@ -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-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: oklab
|
|
3
|
+
Version: 0.1.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
|
+
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
|
+
- [ ] python 3.8–3.14 compatibility
|
|
53
|
+
- [ ] clamping (in native oklab space via chroma reduction)
|
|
54
|
+
- [ ] configurable RGB primaries (P3, rec2020, …)
|
|
55
|
+
❌ CMYK support (scope creep)
|
|
56
|
+
|
|
57
|
+
# sources
|
|
58
|
+
|
|
59
|
+
- function names inspired by [hsluv](https://github.com/hsluv/hsluv-python)
|
|
60
|
+
- outputs sanity-checked against [oklch.com](https://oklch.com/) and [wikipedia](https://en.wikipedia.org/wiki/Oklab_color_space)
|
|
61
|
+
- `M0` sourced from [Björn's CSSWG comment](https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-945714988)
|
|
62
|
+
- `M2_inv` sourced from [Björn's blogpost](https://bottosson.github.io/posts/oklab/)
|
|
63
|
+
- `XYZ_TO_RGB`, `RGB_TO_XYZ` sourced from [colour science](https://www.colour-science.org/)
|
|
64
|
+
- `M1`, `M1_inv`, `M2`, `RGB_TO_LMS`, `LMS_TO_RGB` derived numerically using [numpy](https://github.com/numpy/numpy)
|
|
65
|
+
- `test_oklab.test_xyz` sourced from [Björn's blogpost](https://bottosson.github.io/posts/oklab/)
|
|
66
|
+
|
|
67
|
+
# conversion graph
|
|
68
|
+
|
|
69
|
+
here is a graph of the atomic conversions:
|
|
70
|
+
|
|
71
|
+

|
|
72
|
+
|
|
73
|
+
- lab: oklab cartesian space
|
|
74
|
+
- lch: oklch cylindrical space
|
|
75
|
+
- lms: intermediate lms-like space
|
|
76
|
+
- xyz: CIEXYZ space
|
|
77
|
+
- rgb: sRGB
|
|
78
|
+
- hex: 8-bit sRGB in hexadecimal
|
|
79
|
+
|
|
80
|
+
all composed conversions are derived via shortest path in this graph
|
|
81
|
+
|
oklab-0.1.1/graph.png
ADDED
|
Binary file
|
oklab-0.1.1/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))))
|
oklab-0.1.1/old.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
def oetf_gamma(c, γ):
|
|
2
|
+
return c ** γ
|
|
3
|
+
|
|
4
|
+
def eotf_gamma(c, γ):
|
|
5
|
+
return root(c, γ)
|
|
6
|
+
|
|
7
|
+
def oetf_prophotorgb(c, *, e = 0.001953125):
|
|
8
|
+
return c / 16 if c <= 16 * e else c ** 1.8
|
|
9
|
+
|
|
10
|
+
def eotf_prophotorgb(c, *, e = 0.001953125):
|
|
11
|
+
return 16 * c if c <= e else root(c, 1.8)
|
|
12
|
+
|
|
13
|
+
def oetf_rec2020(c, *, α = 1.0992968268094157, β = 0.018053968510807):
|
|
14
|
+
return c / 4.5 if abs(c) < β * 4.5 else root((abs(c) + α - 1) / α, 0.45)
|
|
15
|
+
|
|
16
|
+
def eotf_rec2020(c, *, α = 1.0992968268094157, β = 0.018053968510807):
|
|
17
|
+
return 4.5 * c if c < β else α * c ** 0.45 - (α - 1)
|
|
18
|
+
|
|
19
|
+
def oetf_L_star(c, *, k = 24389 / 27):
|
|
20
|
+
return (100 * c) / k if c <= 0.08 else ((c + 0.16) / 1.16) ** 3
|
|
21
|
+
|
|
22
|
+
def eotf_L_star(c, *, e = 216 / 24389, k = 24389 / 27):
|
|
23
|
+
return (c * k) / 100 if c <= e else 1.16 * math.cbrt(c) - 0.16
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ['hatchling']
|
|
3
|
+
build-backend = 'hatchling.build'
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = 'oklab'
|
|
7
|
+
version = '0.1.1'
|
|
8
|
+
description = "lightweight python implementation of Björn Ottosson's oklab colour space"
|
|
9
|
+
readme = 'readme.md'
|
|
10
|
+
license = 'MIT'
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = 'deftasparagusanaconda' }
|
|
13
|
+
]
|
|
14
|
+
keywords = [
|
|
15
|
+
'color',
|
|
16
|
+
'colour',
|
|
17
|
+
'oklab',
|
|
18
|
+
'oklch',
|
|
19
|
+
]
|
|
20
|
+
dependencies = []
|
|
21
|
+
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Operating System :: OS Independent",
|
|
26
|
+
"Topic :: Multimedia :: Graphics",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.wheel]
|
|
30
|
+
include = ["oklab.py"]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = 'https://github.com/deftasparagusanaconda/oklab'
|
|
34
|
+
Repository = 'https://github.com/deftasparagusanaconda/oklab'
|
|
35
|
+
Issues = 'https://github.com/deftasparagusanaconda/oklab/issues'
|
oklab-0.1.1/readme.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
lightweight python implementation of [Björn Ottosson](https://bottosson.github.io/)'s oklab/oklch colour space.
|
|
2
|
+
|
|
3
|
+
i like oklab but there isnt a straightforward python package for working with it. so i made it.
|
|
4
|
+
|
|
5
|
+
# install
|
|
6
|
+
|
|
7
|
+
install it from [PyPI](https://pypi.org/project/oklab/):
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
python -m pip install oklab
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
# how to use?
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import oklab
|
|
17
|
+
|
|
18
|
+
oklab.lch_to_hex((0.75, 0.125, 310.0))
|
|
19
|
+
# '#c697e9'
|
|
20
|
+
|
|
21
|
+
oklab.hex_to_lch('#ff0000')
|
|
22
|
+
# (0.6279536182521783, 0.25762679148825324, 29.227131291763712)
|
|
23
|
+
|
|
24
|
+
oklab.lab_to_xyz((1.0, 0.0, 0.0))
|
|
25
|
+
# (0.9504559270516719, 0.9999999999999998, 1.0890577507598782)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
# features
|
|
29
|
+
|
|
30
|
+
- [x] 6 formats: `rgb`, `hex`, `lab`, `lch`, `xyz`, `lms`
|
|
31
|
+
- [x] 30 conversion functions (complete graph)
|
|
32
|
+
- [x] numerical accuracy + roundtrip precision
|
|
33
|
+
- [x] source correctness
|
|
34
|
+
- [x] zero dependencies
|
|
35
|
+
- [ ] python 3.8–3.14 compatibility
|
|
36
|
+
- [ ] clamping (in native oklab space via chroma reduction)
|
|
37
|
+
- [ ] configurable RGB primaries (P3, rec2020, …)
|
|
38
|
+
❌ CMYK support (scope creep)
|
|
39
|
+
|
|
40
|
+
# sources
|
|
41
|
+
|
|
42
|
+
- function names inspired by [hsluv](https://github.com/hsluv/hsluv-python)
|
|
43
|
+
- outputs sanity-checked against [oklch.com](https://oklch.com/) and [wikipedia](https://en.wikipedia.org/wiki/Oklab_color_space)
|
|
44
|
+
- `M0` sourced from [Björn's CSSWG comment](https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-945714988)
|
|
45
|
+
- `M2_inv` sourced from [Björn's blogpost](https://bottosson.github.io/posts/oklab/)
|
|
46
|
+
- `XYZ_TO_RGB`, `RGB_TO_XYZ` sourced from [colour science](https://www.colour-science.org/)
|
|
47
|
+
- `M1`, `M1_inv`, `M2`, `RGB_TO_LMS`, `LMS_TO_RGB` derived numerically using [numpy](https://github.com/numpy/numpy)
|
|
48
|
+
- `test_oklab.test_xyz` sourced from [Björn's blogpost](https://bottosson.github.io/posts/oklab/)
|
|
49
|
+
|
|
50
|
+
# conversion graph
|
|
51
|
+
|
|
52
|
+
here is a graph of the atomic conversions:
|
|
53
|
+
|
|
54
|
+

|
|
55
|
+
|
|
56
|
+
- lab: oklab cartesian space
|
|
57
|
+
- lch: oklch cylindrical space
|
|
58
|
+
- lms: intermediate lms-like space
|
|
59
|
+
- xyz: CIEXYZ space
|
|
60
|
+
- rgb: sRGB
|
|
61
|
+
- hex: 8-bit sRGB in hexadecimal
|
|
62
|
+
|
|
63
|
+
all composed conversions are derived via shortest path in this graph
|
|
64
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import oklab, math, random
|
|
2
|
+
|
|
3
|
+
def test_xyz():
|
|
4
|
+
'https://bottosson.github.io/posts/oklab/#table-of-example-xyz-and-oklab-pairs'
|
|
5
|
+
|
|
6
|
+
table_xyz = [(+0.950, +1.000, +1.089),
|
|
7
|
+
(+1.000, +0.000, +0.000),
|
|
8
|
+
(+0.000, +1.000, +0.000),
|
|
9
|
+
(+0.000, +0.000, +1.000)]
|
|
10
|
+
table_lab = [(+1.000, +0.000, +0.000),
|
|
11
|
+
(+0.450, +1.236, -0.019),
|
|
12
|
+
(+0.922, -0.671, +0.263),
|
|
13
|
+
(+0.153, -1.415, -0.449)]
|
|
14
|
+
|
|
15
|
+
for xyz, lab in zip(table_xyz, table_lab):
|
|
16
|
+
for expected, computed in zip(xyz, oklab.lab_to_xyz(lab)):
|
|
17
|
+
assert math.isclose(expected, computed, abs_tol=0.005)
|
|
18
|
+
|
|
19
|
+
for expected, computed in zip(lab, oklab.xyz_to_lab(xyz)):
|
|
20
|
+
assert math.isclose(expected, computed, abs_tol=0.005)
|
|
21
|
+
|
|
22
|
+
def test_forward_inverse_pairs():
|
|
23
|
+
pairs = [
|
|
24
|
+
(oklab.lch_to_lab, oklab.lab_to_lch),
|
|
25
|
+
(oklab.lab_to_lms, oklab.lms_to_lab),
|
|
26
|
+
(oklab.rgb_to_lms, oklab.lms_to_rgb),
|
|
27
|
+
(oklab.xyz_to_lms, oklab.lms_to_xyz),
|
|
28
|
+
(oklab.rgb_to_xyz, oklab.xyz_to_rgb),
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
for forward, inverse in pairs:
|
|
32
|
+
for _ in range(1000):
|
|
33
|
+
tuple_in = tuple(random.random() for _ in range(3))
|
|
34
|
+
tuple_out1 = inverse(forward(tuple_in))
|
|
35
|
+
tuple_out2 = forward(inverse(tuple_in))
|
|
36
|
+
|
|
37
|
+
for expected, computed1, computed2 in zip(tuple_in, tuple_out1, tuple_out2):
|
|
38
|
+
assert math.isclose(expected, computed1)
|
|
39
|
+
assert math.isclose(expected, computed2)
|