Skip to content

Instantly share code, notes, and snippets.

@facelessuser
Last active April 14, 2023 02:40
Show Gist options
  • Save facelessuser/2315c37741ae5253b8b42d2d41bcfa5a to your computer and use it in GitHub Desktop.
Save facelessuser/2315c37741ae5253b8b42d2d41bcfa5a to your computer and use it in GitHub Desktop.
Okhsl P3
# pragma: init
"""
Okhsl/Okhsv class.
Adapted to Python and ColorAide and adjusted to P3 by Isaac Muse 2023.
---- License ----
Copyright (c) 2021 Björn Ottosson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from __future__ import annotations
from coloraide.spaces.okhsl import Okhsl, okhsl_to_oklab, oklab_to_okhsl
from coloraide.spaces.okhsv import Okhsv, okhsv_to_oklab, oklab_to_okhsv
from coloraide.types import Vector
P3L_TO_LMS = [
[0.48137985274995426, 0.4621183710113182, 0.05650177623872754],
[0.22883194181124467, 0.6532168193835677, 0.11795123880518774],
[0.08394575232299313, 0.22416527097756647, 0.6918889766994406]
]
LMS_TO_P3L = [
[3.1277689713618755, -2.25713576259164, 0.1293667912297652],
[-1.0910090184377979, 2.413331710306922, -0.3223226918691246],
[-0.026010801938570277, -0.5080413317041669, 1.534052133642737]
]
P3L_COEFF = [
# Red
[
# Limit
[-1.77234393, -0.82075874],
# `Kn` coefficients
[1.19413664, 1.76298424, 0.59586087, 0.75759729, 0.56816791]
],
# Green
[
# Limit
[1.8031987, -1.1932814],
# `Kn` coefficients
[2.65372708, -3.76337813, 2.01565193, 1.56037817, -2.62199151]
],
# Blue
[
# Limit
[0.08970489, 1.90327747],
# `Kn` coefficients
[1.3650924, -0.01396104, -1.14523207, -0.50259958, 0.00317569]
]
] # type: list[list[Vector]]
class OkhslP3(Okhsl):
"""Okhsl P3 class."""
NAME = "okhsl-p3"
SERIALIZE = ("--okhsl-p3",)
def to_base(self, coords: Vector) -> Vector:
"""To Oklab from Okhsl."""
return okhsl_to_oklab(coords, LMS_TO_P3L, P3L_COEFF)
def from_base(self, coords: Vector) -> Vector:
"""From Oklab to Okhsl."""
return oklab_to_okhsl(coords, LMS_TO_P3L, P3L_COEFF)
class OkhsvP3(Okhsv):
"""Okhsv P3 class."""
NAME = "okhsv-p3"
SERIALIZE = ("--okhsv-p3",)
def to_base(self, okhsv: Vector) -> Vector:
"""To Oklab from Okhsv."""
return okhsv_to_oklab(okhsv, LMS_TO_P3L, P3L_COEFF)
def from_base(self, oklab: Vector) -> Vector:
"""From Oklab to Okhsv."""
return oklab_to_okhsv(oklab, LMS_TO_P3L, P3L_COEFF)
from coloraide.everything import ColorAll as Base
class Color(Base): ...
Color.register([OkhslP3(), OkhsvP3()])
# pragma: init
Color('display-p3', [1, 0, 0]).convert('oklch')
Color('display-p3', [1, 0, 0]).convert('okhsl-p3')
Color('display-p3', [0, 1, 0]).convert('okhsl-p3')
Color('display-p3', [0, 0, 1]).convert('okhsl-p3')
Color('display-p3', [1, 0, 0]).convert('okhsv-p3')
Color('display-p3', [0, 1, 0]).convert('okhsv-p3')
Color('display-p3', [0, 0, 1]).convert('okhsv-p3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment