Converts color components from the CIE XYZ to the CIE L*u*v* color space. - Java Swing

Java examples for Swing:JComponent

Description

Converts color components from the CIE XYZ to the CIE L*u*v* color space.

Demo Code


//package com.java2s;

public class Main {
    /**//www . j  a va2s. c o m
     * Constant for the CIE XYZ and CIE L*u*v* color spaces: (6/29)^3 *
     */
    private static final double CIE_EPSILON = 216.0 / 24389.0;
    /**
     * Constant for the CIE XYZ and CIE L*u*v* color spaces: (29/3)^3 *
     */
    private static final double CIE_KAPPA = 24389.0 / 27.0;
    /**
     * Xr, Yr, Zr constants with D50 white point used for CIE XYZ to
     * CIE L*u*v* conversion *
     */
    private static final double[] XYZ_R_D50 = { 0.964221, 1.000000,
            0.825211 };

    /**
     * Converts color components from the CIE XYZ to the CIE L*u*v* color
     * space. If the <i>luv</i> array is {@code null}, a new one will be
     * created with the same size as the <i>xyz</i> array.
     *
     * http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_Luv.html
     *
     * @param xyz Color components in the CIE XYZ color space.
     * @param luv Optional array for storing color components in the CIE L*u*v*
     *            color space.
     * @return Color components in the CIE L*u*v* color space.
     */
    public static double[] xyz2luv(double[] xyz, double[] luv) {
        double tmp = xyz[0] + 15.0 * xyz[1] + 3.0 * xyz[2];
        if (tmp == 0.0) {
            tmp = 1.0;
        }
        double u1 = 4.0 * xyz[0] / tmp;
        double v1 = 9.0 * xyz[1] / tmp;

        // Relative luminance
        double yr = xyz[1] / XYZ_R_D50[1];
        double ur = 4.0 * XYZ_R_D50[0]
                / (XYZ_R_D50[0] + 15.0 * XYZ_R_D50[1] + 3.0 * XYZ_R_D50[2]);
        double vr = 9.0 * XYZ_R_D50[1]
                / (XYZ_R_D50[0] + 15.0 * XYZ_R_D50[1] + 3.0 * XYZ_R_D50[2]);

        // Mapping relative luminance to lightness
        if (luv == null) {
            luv = new double[xyz.length];
        }
        if (yr > CIE_EPSILON) {
            luv[0] = 116.0 * Math.pow(yr, 1.0 / 3.0) - 16.0;
        } else {
            luv[0] = CIE_KAPPA * yr;
        }
        luv[1] = 13.0 * luv[0] * (u1 - ur);
        luv[2] = 13.0 * luv[0] * (v1 - vr);

        return luv;
    }
}

Related Tutorials