Skip to content

Instantly share code, notes, and snippets.

@facelessuser
Created March 21, 2024 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save facelessuser/01b001178fc742965a011643030357e8 to your computer and use it in GitHub Desktop.
Save facelessuser/01b001178fc742965a011643030357e8 to your computer and use it in GitHub Desktop.
Okhsl and Okhsv for wide gamuts
# pragma: init
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
P3L_TO_LMS = [
[0.4813798527499543, 0.4621183710113182, 0.05650177623872754],
[0.2288319418112447, 0.6532168193835677, 0.11795123880518772],
[0.08394575232299314, 0.22416527097756647, 0.6918889766994405]
]
LMS_TO_P3L = [
[3.127768971361874, -2.2571357625916395, 0.12936679122976516],
[-1.0910090184377979, 2.413331710306922, -0.32232269186912466],
[-0.02601080193857028, -0.508041331704167, 1.5340521336427373]
]
REC2020L_TO_LMS = [
[0.6167557848654442, 0.3601984012264634, 0.023045813908092294],
[0.2651330593926367, 0.6358393720678492, 0.0990275685395141],
[0.10010262952034829, 0.20390652261661446, 0.6959908478630372]
]
LMS_TO_REC2020L = [
[2.1399067304346517, -1.2463894937606181, 0.10648276332596672],
[-0.8847358357577675, 2.1632309383612007, -0.27849510260343346],
[-0.04857374640044416, -0.454503149714096, 1.5030768961145402]
]
P3L_COEFF = [
# Red
[
# Limit
[-1.77234393, -0.82075874],
# `Kn` coefficients
[1.19414018, 1.7629812, 0.59585994, 0.75759997, 0.5681685]
],
# Green
[
# Limit
[1.80319872, -1.1932814],
# `Kn` coefficients
[0.73956682, -0.4595428, 0.08285309, 0.12541165, -0.14503291]
],
# Blue
[
# Limit
[0.08970488, 1.90327747],
# `Kn` coefficients
[1.36509441, -0.0139623, -1.14523051, -0.50259879, 0.00317471]
]
]
REC2020L_COEFF = [
# Red
[
# Limit
[-1.36834899, -0.46664773],
# `Kn` coefficients
[1.2572445, 1.71580176, 0.5648733, 0.79507316, 0.58716363]
],
# Green
[
# Limit
[2.01150796, -2.0379096],
# `Kn` coefficients
[0.74087755, -0.4586733, 0.08182977, 0.12598705, -0.14570327]
],
# Blue
[
# Limit
[0.06454093, 2.29709336],
# `Kn` coefficients
[1.36920484e+00, -1.64666735e-02, -1.14197870e+00, -5.01064768e-01, 1.19905985e-03]
]
]
class OkhslP3(Okhsl):
"""Okhsl P3 class."""
NAME = "okhsl-p3"
SERIALIZE = ("--okhsl-p3",)
def to_base(self, coords):
"""To Oklab from Okhsl."""
return okhsl_to_oklab(coords, LMS_TO_P3L, P3L_COEFF)
def from_base(self, coords):
"""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):
"""To Oklab from Okhsv."""
return okhsv_to_oklab(okhsv, LMS_TO_P3L, P3L_COEFF)
def from_base(self, oklab):
"""From Oklab to Okhsv."""
return oklab_to_okhsv(oklab, LMS_TO_P3L, P3L_COEFF)
class Okhsl2020(Okhsl):
"""Okhsl Rec. 2020 class."""
NAME = "okhsl-2020"
SERIALIZE = ("--okhsl-2020",)
def to_base(self, coords):
"""To Oklab from Okhsl."""
return okhsl_to_oklab(coords, LMS_TO_REC2020L, REC2020L_COEFF)
def from_base(self, coords):
"""From Oklab to Okhsl."""
return oklab_to_okhsl(coords, LMS_TO_REC2020L, REC2020L_COEFF)
class Okhsv2020(Okhsv):
"""Okhsv Rec. 2020 class."""
NAME = "okhsv-2020"
SERIALIZE = ("--okhsv-2020",)
def to_base(self, okhsv):
"""To Oklab from Okhsv."""
return okhsv_to_oklab(okhsv, LMS_TO_REC2020L, REC2020L_COEFF)
def from_base(self, oklab):
"""From Oklab to Okhsv."""
return oklab_to_okhsv(oklab, LMS_TO_REC2020L, REC2020L_COEFF)
from coloraide.everything import ColorAll as Base
class Color(Base):
FIT = 'oklch-raytrace'
Color.register([OkhslP3(), OkhsvP3(), Okhsl2020(), Okhsv2020()])
# pragma: init
Steps([c.convert('okhsl') for c in [Color('srgb', [1, 0, 0]), Color('srgb', [0, 1, 0]), Color('srgb', [0, 0, 1])]])
Steps([c.convert('okhsl-p3') for c in [Color('display-p3', [1, 0, 0]), Color('display-p3', [0, 1, 0]), Color('display-p3', [0, 0, 1])]])
Steps([c.convert('okhsl-2020') for c in [Color('rec2020', [1, 0, 0]), Color('rec2020', [0, 1, 0]), Color('rec2020', [0, 0, 1])]])
Color.interpolate(['color(--okhsl 0 1 0.5)', 'color(--okhsl 0 1 0.5)'], space='okhsl', hue='longer')
Color.interpolate(['color(--okhsl-p3 0 1 0.5)', 'color(--okhsl-p3 0 1 0.5)'], space='okhsl-p3', hue='longer')
Color.interpolate(['color(--okhsl-2020 0 1 0.5)', 'color(--okhsl-2020 0 1 0.5)'], space='okhsl-2020', hue='longer')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment