Java RGB Color Convert To rgbToXyz(int r, int g, int b)

Here you can find the source of rgbToXyz(int r, int g, int b)

Description

rgb To Xyz

License

Open Source License

Declaration

public static double[] rgbToXyz(int r, int g, int b) 

Method Source Code

//package com.java2s;
/**/*from w  ww  .j a  va  2 s  .  co  m*/
 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.md file.
 */

public class Main {
    public static double[] rgbToXyz(int r, int g, int b) {

        double[] rgb = scaleValues(0, 255, r, g, b);
        double red = rgb[0];
        double green = rgb[1];
        double blue = rgb[2];

        //Apply a gamma correction to the RGB values, which makes the color more vivid
        double _red = (red > 0.04045f) ? Math.pow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
        double _green = (green > 0.04045f) ? Math.pow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
        double _blue = (blue > 0.04045f) ? Math.pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);

        //Convert the RGB values to XYZ using the Wide RGB D65 conversion formula
        double X = _red * 0.649926f + _green * 0.103455f + _blue * 0.197109f;
        double Y = _red * 0.234327f + _green * 0.743075f + _blue * 0.022598f;
        double Z = _red * 0.0000000f + _green * 0.053077f + _blue * 1.035763f;

        return new double[] { X, Y, Z };
    }

    final static double[] scaleValues(double min, double max, double... vals) {
        double[] result = new double[vals.length];
        double scaleFactor = max - min;
        for (int x = 0; x < vals.length; x++) {
            result[x] = ((vals[x] - min) / scaleFactor);
        }
        return result;
    }
}

Related

  1. rgbToShort(int rgb)
  2. rgbToString(float r, float g, float b)
  3. rgbToText(int red, int green, int blue)
  4. rgbToUnscaledYUV(int[][] rgb)
  5. RGBtoXYZ(float r, float g, float b)
  6. rgbToY(int rgb)