Convert Lab to XYZ

2.9k Views Asked by At

I need to be able to convert a Lab color to an XYZ color. I have followed this page's explanation, based on recommendation I found on someone else's question: http://www.brucelindbloom.com/index.html?Eqn_Lab_to_XYZ.html

I have followed it, but my X and Z values are way off, but the Y is correct (I've tested it against a couple of online converters (which don't show how they convert it...)). I've been staring at that Lab to XYZ website for a while now trying to find a mistake with my math.

The Lab color I test it with: Lab[41, 48, -19]

The XYZ values I get: XYZ[5676.0352, 0.1186, 4845.2021] (the website said the results should be in the range [0, 1]

The referenceX, referenceY and referenceZ are my reference white color values.

Here's my code (Java):

    // https://www.mathworks.com/help/images/ref/whitepoint.html
    // a: 1.0985, 1, 0.3556
    // c: 0.9807, 1, 1.1822
    // e: 1,1, 1
    // d50: 0.9642, 1, 0.8251
    // d55: 0.9568, 1, 0.9214
    // d65: 0.9504, 1, 1.0888
    // icc: 0.9642, 1, 0.8249 (what we're using)
    double referenceX = 0.9642;
    double referenceY = 1;
    double referenceZ = 0.8249;

    double fy = (this.lightness + 16) / 116;
    double fx = this.a / 500 + fy;
    double fz = fy - this.b / 200;
    //todo make these constants
    double kappa = 216 / 24389.0;
    double epsilon = 24389 / 27.0;

    double fx3 = fx * fx * fx;
    double fz3 = fz * fz * fz;
    double xr = fx3 > epsilon ? fx3 : (116 * fx - 16) / kappa;
    double yr = this.lightness > kappa * epsilon ? Math.pow((this.lightness + 16) / 116, 3) : this.lightness / kappa;
    double zr = fz3 > epsilon ? fz3 : (116 * fz - 16) / kappa;

    double X = xr * referenceX;
    double Y = yr * referenceY;
    double Z = zr * referenceZ;

So I guess my question is of course, what's wrong with my math? Or can anyone find another website with a way to do the conversion (I can't seem to find any)?

Thanks. If you need any clarifications or have any questions please ask.

1

There are 1 best solutions below

3
On

Not sure what went wrong here (if someone knows, please say!), but I ended up using the Wikipedia conversion (https://en.wikipedia.org/wiki/Lab_color_space#RGB_and_CMYK_conversions, see Reverse Transformation). I don't know why I didn't see this at first.