RGB to CIE color conversion [duplicate]

元气小坏坏 提交于 2020-01-13 12:08:23

问题


As I said before (RGB to Philips Hue (HSB)) I'm still not giving up my hopes for converting a simple RGB value to a value Philips Hue will accept.

The official App for iPhone and Android allows you to select a color from a random photo and the Hue will adjust itself accordingly. So there must be some sort of formula.

This time however I think I have just found this very formula in this interesting article online:

https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/blob/master/ApplicationDesignNotes/RGB%20to%20xy%20Color%20conversion.md

I have been trying to convert his explanation to Java language, but I'm not getting the desired results. Anybody knows what goes wrong?

For the record I'm using the Philips Hue Light Bulbs.

public static List<Double> getRGBtoXY(Color c) {
        // For the hue bulb the corners of the triangle are:
        // -Red: 0.675, 0.322
        // -Green: 0.4091, 0.518
        // -Blue: 0.167, 0.04
        double[] normalizedToOne = new double[3];
        float cred, cgreen, cblue;
        cred = c.getRed();
        cgreen = c.getGreen();
        cblue = c.getBlue();
        normalizedToOne[0] = (cred / 255);
        normalizedToOne[1] = (cgreen / 255);
        normalizedToOne[2] = (cblue / 255);
        float red, green, blue;

        // Make red more vivid
        if (normalizedToOne[0] > 0.04045) {
            red = (float) Math.pow(
                    (normalizedToOne[0] + 0.055) / (1.0 + 0.055), 2.4);
        } else {
            red = (float) (normalizedToOne[0] / 12.92);
        }

        // Make green more vivid
        if (normalizedToOne[1] > 0.04045) {
            green = (float) Math.pow((normalizedToOne[1] + 0.055)
                    / (1.0 + 0.055), 2.4);
        } else {
            green = (float) (normalizedToOne[1] / 12.92);
        }

        // Make blue more vivid
        if (normalizedToOne[2] > 0.04045) {
            blue = (float) Math.pow((normalizedToOne[2] + 0.055)
                    / (1.0 + 0.055), 2.4);
        } else {
            blue = (float) (normalizedToOne[2] / 12.92);
        }

        float X = (float) (red * 0.649926 + green * 0.103455 + blue * 0.197109);
        float Y = (float) (red * 0.234327 + green * 0.743075 + blue + 0.022598);
        float Z = (float) (red * 0.0000000 + green * 0.053077 + blue * 1.035763);

        float x = X / (X + Y + Z);
        float y = Y / (X + Y + Z);

        double[] xy = new double[2];
        xy[0] = x;
        xy[1] = y;
        List<Double> xyAsList = Doubles.asList(xy);
        return xyAsList;
    }

This is not a duplicate? Please read the referenced question to fully understand. This is a question to convert to XY. The referenced question is about converting RGB to HSB. How can this be the same?!?!


回答1:


Found one little mistake:

float Y = (float) (red * 0.234327 + green * 0.743075 + blue + 0.022598);

should be

float Y = (float) (red * 0.234327 + green * 0.743075 + blue * 0.022598);


来源:https://stackoverflow.com/questions/22623655/rgb-to-cie-color-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!