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

Java examples for Swing:JComponent

Description

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

Demo Code


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.LinkedList;
import java.util.List;

public class Main{
    /**/*from ww w  .  j av a 2s .  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;
    /**
     * Precalculated u0 constant for CIE L*u*v* to CIE XYZ conversion. *
     */
    private static final double XYZ_R_D50_U0 = 4.0 * XYZ_R_D50[0]
            / (XYZ_R_D50[0] + 15.0 * XYZ_R_D50[1] + 3.0 * XYZ_R_D50[2]);
    /**
     * Precalculated v0 constant for CIE L*u*v* to CIE XYZ conversion. *
     */
    private static final double XYZ_R_D50_V0 = 9.0 * XYZ_R_D50[1]
            / (XYZ_R_D50[0] + 15.0 * XYZ_R_D50[1] + 3.0 * XYZ_R_D50[2]);
    /**
     * Convert color components from the CIE L*u*v* to the CIE XYZ color space.
     * If the <i>xyz</i> array is {@code null}, a new one will be created
     * with the same size as the <i>luv</i> array.
     *
     * See http://www.brucelindbloom.com/index.html?Eqn_Luv_to_XYZ.html
     *
     * @param luv Color components in the CIE L*u*v* color space
     * @param xyz Optional array to store color components in the CIE XYZ color
     *            space.
     * @return Color components in the CIE XYZ color space.
     */
    public static double[] luv2xyz(double[] luv, double[] xyz) {
        if (xyz == null) {
            xyz = new double[luv.length];
        }

        if (luv[0] > CIE_KAPPA * CIE_EPSILON) {
            xyz[1] = (luv[0] + 16.0) / 116.0;
            xyz[1] = xyz[1] * xyz[1] * xyz[1];
        } else {
            xyz[1] = luv[0] / CIE_KAPPA;
        }

        double a = (luv[0] != 0.0 || luv[1] != 0.0) ? ((52.0 * luv[0])
                / (luv[1] + 13.0 * luv[0] * XYZ_R_D50_U0) - 1.0) / 3.0
                : 0.0;
        double b = -5 * xyz[1];
        double c = -1.0 / 3.0;
        double d = (luv[0] != 0.0 || luv[2] != 0.0) ? xyz[1]
                * ((39.0 * luv[0])
                        / (luv[2] + 13.0 * luv[0] * XYZ_R_D50_V0) - 5.0)
                : 0.0;

        xyz[0] = !MathUtils.almostEqual(a, c, 1e-15) ? (d - b) / (a - c)
                : 0.0;
        xyz[2] = xyz[0] * a + b;

        return xyz;
    }
}

Related Tutorials