How to find a maximum chroma value in the OKLCH color space for given hue and lightness?

120 Views Asked by At

Chroma value varies for different hues, and I want to understand how I can find the maximum value that is displayable for the given hue and lightness programmatically.

I saw a couple of color pickers that can limit the selection visually, but I don't have access to their code, but anyway, I think it should be possible somehow.

1

There are 1 best solutions below

0
On

It's not an elegant solution but if you can convert from Oklch to an RGB space, you can increment the chroma until the conversion results in an RGB that contains a value outside of the 0-1 range.

For example, with my Unicolour library I can do this in C#:

const double increment = 0.001;

var chroma = 0.0;
while (true)
{
    // by default assumes sRGB display
    var unicolour = new Unicolour(ColourSpace.Oklch, 0.5, chroma, 285);
    if (!unicolour.IsInDisplayGamut)
    {
        chroma -= increment;
        break;
    }
    
    chroma += increment;
}

Console.WriteLine(chroma); // 0.292