I'm writing some image processing code to do some creative color gamut limiting that I want to use for color grading applicationgs. I'm building to operate in the Davinci Wide Gamut color space, typically using the DaVinci Intermediate logarithmic encoding, since that is my typical working space in a color managed workflow. To do this, I want to convert the input to xyY, limit the value of xy to fit within my desired gamut and then convert back to DaVinci Wide Gamut/Intermediate.
These are the steps I'm following:
- Convert DaVinci Intermediate RGB to linear RGB
- Multiply by DWG to XYZ conversion matrix.
- Convert XYZ to xyY.
- Modify x and y to fall within gamut.
- Convert xyY to XYZ.
- Multiply by XYZ to DWG conversion matrix.
- Convert linear RGB to DaVinci Intermediate.
This works fine for most cases, I have also verified that roundtripping without modification of x and y works as intended. The problem occurs for for extremely saturated blues. Take the following RGB values in Davinci Wide Gamut/Intermediate as an example: [0, 0, 1.0]
Let's follow along:
- Convert to linear:
[0, 0, 100]
- Convert to XYZ:
[10.11,-14.78, 132.59]
- Convert to xyY:
[0.08,-0.12,-14.78]
- Modifying x and y to fit in my gamut:
[0.18, 0.07, -14.78]
- Convert back to XYZ:
[-35.64,-14.78,-149.08]
- Convert back to DWG linear:
[-27.99,-27.99,-117.44]
- Convert back to DaVinci Intermediate:
[-292.34, -292.34, -1226.52]
As you can see, the result is nonsensical, but only for these extreme blue values. To me, it seems pretty clear that this is caused by the blue primary in DaVinci Wide Gamut which has a negative y coordinate ([0.0790, -0.1155]
). This causes negative luminance (Y = -14.78
) and when messing with the xy values without dealing with the luminance at the same time, things go haywire.
How do I deal with negative values in xyY and XYZ when doing gamut limiting in this way?
Step 2
XYZ < 0
?? Afaik CIELAB XYZ should always be >= 0. Are any other of your XYZ s < 0 ?There are quite a few RGB <-> XYZ formulas and code snippets on the web, for example