Boardy
Boardy

Reputation: 36205

Convert XY Values to RGB

I am using the Android Philips Hue SDK and I am currently having an issue with converting the light bulbs XY value to RGB.

I have looked at this code provided in a forum on Philips Hue website and the code has been provided by someone from Hue Support.

I have the following function using this code from the forum:

public static int[] convertXYToRGB(float[] xy, String lightModel)
    {
        int color = PHUtilities.colorFromXY(xy, lightModel);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        return new int[] {r, g, b};
    }

And I am calling it like:

int hue = lightState.getHue();
float[] xy = PHUtilities.calculateXY(hue, item.light.getModelNumber());

int[] rgb = Utilities.convertXYToRGB(xy, item.light.getModelNumber());

Looking at the RGB value I get back it seems to be the wrong colour. For example, using the official app, I set one of my light bulbs to red. When I run my app, the RGB value that comes back is a pale yellow.

Has anyone else experienced this or know how to resolve this issue?

Upvotes: 2

Views: 2757

Answers (1)

Matt
Matt

Reputation: 13943

I had a similar issue while programming a desktop application using the same Java SDK (login required). Interestingly, a plain red turned into a fade yellow, exactly how you describe it. A possible solution is to use the xy-values directly instead of the conversion from hue-values. That finally solved the problem for me. You can get the xy-values from the PHLightState object using the methods .getX() and .getY(). After that, use colorFromXY as in your code to get the RGB-values (as android color value = int).

PHLightState s = light.getLastKnownLightState();

float xy[] = new float[] {s.getX(), s.getY()};
int combRGB = PHUtilities.colorFromXY(xy, light.getModelNumber());

On Android, convert combRGB as you already do. Make sure to include android.graphics.Color. If you are testing on non-Android systems you can use the following code:

Color theColor = new Color(combRGB);
int[] sepRGB = {theColor.getRed(), theColor.getGreen(), theColor.getBlue()};

Note: The lights can only address a certain color gamut depending on the type. This is explained into detail here. The 'normal' bulbs with a color gamut B have quite some limitations. For example: most greens turn into yellows and the blues contain a certain amount of red.

Example values: The following overall conversions are tested on my live system with LCT001-blubs. I used PHUtilities.calculateXYFromRGB() to convert the input, then I set the xy-values of the new light state with .setX() and .setY() and finally sent it to the bridge. The values are then extracted from the light cache in the application as soon as it gets the next update.

255   0   0   ->   254   0   0
  0 255   0   ->   237 254   0
  0   0 255   ->    90   0 254
200   0 200   ->   254   0 210
255 153   0   ->   254 106   0
255 153 153   ->   254  99 125

Upvotes: 1

Related Questions