Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion between oklch and okhsl #247

Open
adithyaappu opened this issue Nov 17, 2024 · 4 comments
Open

Conversion between oklch and okhsl #247

adithyaappu opened this issue Nov 17, 2024 · 4 comments

Comments

@adithyaappu
Copy link

I was playing around with the library and converter. When attempted, conversion from oklch to okhsl yields weird results. In the documentation I haven't seen any specifics about the the color spaces supported by converter. So I assume it is expected to work.

This is my converter function.

function colorConverter(mode, colorObject) {
  let convertFunction = converter(mode);
  let convertedColor = convertFunction(colorObject);

  return convertedColor;
}

I have added,

useParser("--oklch", "oklch");

The function call is,

let elemColour = colorConverter(colorModes[i], defaultColour);

In that colorModes is

let colorModes = ["okhsl", "oklch"];

and, defaultColour is,

let defaultColour = {
  mode: "oklch",
  l: 0.2,
  c: 0.1,
  h: 230,
};

this is the results I'm getting,

image

the values are,

Object { mode: "okhsl", l: 0.09907971332700131, s: -0.14929222272569598, h: 229.99999999999997 }
Object { mode: "oklch", l: 0.2, c: 0.1, h: 230 }  

Is there anything I'm missing? Else it looks like there is some bug with converter function

@danburzo
Copy link
Collaborator

Converting from Oklch to Okhsl passes through sRGB as the base color space. The color oklch(0.2 0.1 230) is out of gamut for sRGB, and converting sRGB to Okhsl is not well defined outside the gamut. Leaving the issue open, maybe there’s something we can do to improve the algorithm.

@adithyaappu
Copy link
Author

Oh. May I ask why sRGB, and not XYZ D65 is used as base for conversion? I'm very new to this area. I couldn't understand why it is selected.

@danburzo
Copy link
Collaborator

In general, CIEXYZ D65 is a better base for conversions. Going all the way to sRGB and back is a bit more inefficient, but should not introduce significant error. It was a design choice at the beginning of the project. In a future version of the library, we may switch to more efficient conversion chains that have XYZ at the root.

In the case of Okhsl / Okhsv, they’re ultimately derived from Oklab, but it starts with the assumption of covering the sRGB gamut. You can shortcut the conversion chain with the code below, but it results in the same color:

culori.okhsl(culori.oklab('oklch(...)'))

@adithyaappu
Copy link
Author

Understood. Thank you so much for spending your time to explain the things. I really appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants