Java RGB Color Convert To rgb2luv(double[] rgb, double[] luv)

Here you can find the source of rgb2luv(double[] rgb, double[] luv)

Description

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

License

Open Source License

Parameter

Parameter Description
rgb Color components in the sRGB color space.
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.

Declaration

public static double[] rgb2luv(double[] rgb, double[] luv) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  w w w  .j a  va 2 s  .  com
     * 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 };
    /**
     * sRGB to CIE XYZ conversion matrix. See
     * http://www.brucelindbloom.com/index.html?WorkingSpaceInfo.html#Specifications
     * *
     */
    private static final double[] MATRIX_SRGB2XYZ_D50 = { 0.436052025, 0.385081593, 0.143087414, 0.222491598,
            0.716886060, 0.060621486, 0.013929122, 0.097097002, 0.714185470 };

    /**
     * Converts color components from the CIE L*u*v* to the sRGB color space.
     * A D50 white point is assumed for the sRGB conversion. If the <i>luv</i>
     * array is {@code null}, a new one will be created with the same
     * size as the <i>rgb</i> array.
     *
     * @param rgb Color components in the sRGB 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[] rgb2luv(double[] rgb, double[] luv) {
        double[] xyz = rgb2xyz(rgb, null);
        return xyz2luv(xyz, luv);
    }

    /**
     * Converts color components from the sRGB to the CIE XYZ color space.
     * A D50 white point is assumed for the sRGB conversion. If the <i>xyz</i>
     * array is {@code null}, a new one will be created with the same
     * size as the <i>rgb</i> array.
     *
     * See http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html
     *
     * @param rgb Color components in the sRGB 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[] rgb2xyz(double[] rgb, double[] xyz) {
        if (xyz == null) {
            xyz = new double[rgb.length];
        }

        // Remove sRGB companding to make RGB components linear
        double[] rgbLin = new double[rgb.length];
        for (int i = 0; i < rgb.length; i++) {
            if (rgb[i] <= 0.04045) {
                rgbLin[i] = rgb[i] / 12.92;
            } else {
                rgbLin[i] = Math.pow((rgb[i] + 0.055) / 1.055, 2.4);
            }
        }

        // Convert linear sRGB with D50 white point to CIE XYZ
        for (int i = 0; i < xyz.length; i++) {
            xyz[i] = MATRIX_SRGB2XYZ_D50[i * 3 + 0] * rgbLin[0] + MATRIX_SRGB2XYZ_D50[i * 3 + 1] * rgbLin[1]
                    + MATRIX_SRGB2XYZ_D50[i * 3 + 2] * rgbLin[2];
        }

        return xyz;
    }

    /**
     * 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

  1. rgb2hsl(int[] rgb)
  2. rgb2hsv(int r, int g, int b)
  3. rgb2int(final int[] color)
  4. rgb2intval(int r, int g, int b)
  5. rgb2lab(int R, int G, int B)
  6. rgb2xyz(double[] RGB)
  7. rgb2xyz(double[] rgb, double[] xyz)
  8. RGB2YCbCr(int r, int g, int b, boolean useBT601)
  9. RGB2YCbCr(int[] rgb, float[][] Y, float[][] Cb, float[][] Cr, int imageWidth, int imageHeight)