colour icon indicating copy to clipboard operation
colour copied to clipboard

[FEATURE]: Add "Oklch" colourspace.

Open dofuuz opened this issue 1 year ago • 8 comments

Description

Oklab and Oklch has been added to CSS Color Module Level 4. But, only Oklab is available for Colour.

Adding Oklch will be great for convenience.

dofuuz avatar Jul 19 '23 04:07 dofuuz

Hi @dofuuz,

There is already an issue for this here: https://github.com/colour-science/colour/issues/868

Cheers,

Thomas

KelSolaar avatar Jul 19 '23 04:07 KelSolaar

@KelSolaar Oklch is different from Okhsl or Okhsv. So it's not a duplicate.

dofuuz avatar Jul 19 '23 04:07 dofuuz

Ah yes sorry, my bad! I misread the space name... We probably won't implement this one because it is a simple polar representation that we can perform on any "Jab" like spaces: https://github.com/colour-science/colour/blob/develop/colour/models/common.py#L163

my_oklch = colour.models.Jab_to_JCh(my_oklab)

KelSolaar avatar Jul 19 '23 04:07 KelSolaar

@KelSolaar Oh, I didn't know that conversion function exists. I searched for 'lab to lch'. 🤣

Anyway, providing 'Oklch' would be nice for convenience. I'll leave the decision to the contributors.

dofuuz avatar Jul 19 '23 04:07 dofuuz

This is a good discussion. If we do it for Oklab or JzAzBz, we might as well do it for IPT, ICtCp, ICaCb, ProLab, etc... we have so much opponent-based spaces that it becomes quite a maintenance burden as we ideally need to write tests, docs, etc.., this is why I rolled the colour.models.Jab_to_JCh and its inverse definitions.

KelSolaar avatar Jul 19 '23 04:07 KelSolaar

i agree that there is value in doing this. But I wonder if it can be done by decoration with a "metaclass" or some other meta programming. (please forgive the reference to a very specific C++ concept in a much more general meaning)

tjdcs avatar Jul 19 '23 06:07 tjdcs

Something along those lines would work for example but it gets close to a steam factory rapidly:

import colour
import sys
from colour.models import Jab_to_JCh, JCh_to_Jab
from colour.utilities import copy_definition

__all__ = []

CALLABLES_LCH = {
    "Oklab_to_Oklch": (Jab_to_JCh, "The docstring..."),
    "Oklch_to_Oklab": (JCh_to_Jab, "The docstring..."),
}

for name, (callable_, docstring) in CALLABLES_LCH.items():
    callable_.__doc__ = docstring
    _module = sys.modules["colour.models"]
    setattr(_module, name, copy_definition(callable_, name))

print(colour.models.JCh_to_Jab)
print(colour.models.JCh_to_Jab.__name__)

print(colour.models.Oklch_to_Oklab)
print(colour.models.Oklch_to_Oklab.__name__)
<function JCh_to_Jab at 0x13c204220>
JCh_to_Jab
<function JCh_to_Jab at 0x11ce60680>
Oklch_to_Oklab

We have something magic like that here actually: https://github.com/colour-science/colour-datasets/blob/develop/colour_datasets/loaders/kuopio.py#L498

KelSolaar avatar Jul 19 '23 07:07 KelSolaar

I think there is something you could do in the oklab module to automatically define the variable names too. Something like

add_polar_conversion_this_module("Oklab", "Oklch")

tjdcs avatar Jul 19 '23 14:07 tjdcs

i use oklch quite regularly, and if i was provided a full list of spaces that fall under this category i would be willing to add them to the graph manually Oklab, JzAzBz, IPT, ICtCp, ICaCb, ProLab were mentioned so far

Aonodensetsu avatar Jun 10 '24 10:06 Aonodensetsu

This was implemented! :)

KelSolaar avatar Jun 15 '24 03:06 KelSolaar

Great, can this be expected to appear on the README graph and on PyPi? I tried making a graph with networkx/matplotlib but the nodes overlap, so my guess is another tool was used for it prior. I updated my pyproject to pull the library from github, so no pressure on publishing.

Aonodensetsu avatar Jun 15 '24 07:06 Aonodensetsu

The graph is generated here: https://github.com/colour-science/colour/blob/develop/utilities/generate_plots.py#L166

That being said, I can see that the last build failed on RtD. I will check what is going on.

Examples_Colour_Automatic_Conversion_Graph

KelSolaar avatar Jun 15 '24 08:06 KelSolaar

Note that I will refine some names in a future commit, Oklch won't change though!

KelSolaar avatar Jun 15 '24 08:06 KelSolaar