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?!?!

有帮助吗?

解决方案

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top