michielpost / Q42.HueApi

C# helper library to talk to the Philips Hue bridge
MIT License
409 stars 114 forks source link

wrong RGB #70

Closed becio closed 8 years ago

becio commented 8 years ago

If I change the brightness of a light I get the same color from ToRgb(). I have noticed that the color coordinates do not change, only the brightness property changes so I guess that method does not take account of this member.

CharlyTheKid commented 8 years ago

In fact I found the inbuilt color conversion poorly works at all. I had several problems especially while trying to receive the correct color code (RGB or Hex) of the lamps via the actual light state. The conversion tools of the api try to convert based on the CIE XY values. You need to keep in mind that CIE 1931 XY is based on coordinates and the conversion to common color systems like RGB can be very inaccurate.

I ended up to set colors per Hue, Saturation and Brightness (HSB). It's more easy and accurate to convert colors between HSB and RGB, but there are a few things you need to know about HSB:

Sorry for my bad english, hope this will help you.

michielpost commented 8 years ago

The latest update of the color conversion was in this PR: https://github.com/Q42/Q42.HueApi/pull/46

It would be very helpful to receive more PRs with improvements to the color conversions by someone that really understands this stuff. @CharlyTheKid any chance you have some custom color conversion code to contribute?

CharlyTheKid commented 8 years ago

@michielpost as requested i can give you my code for color conversion. You can get my example project from the following dropbox folder. https://www.dropbox.com/sh/2xobws18ta5kga6/AAAKwPtkNRpJPb8USCe5aXOxa?dl=0

To set color for hue lamp use it like this:

var rgbColor = new RGB(){ R = 250, G = 100, B = 0};
var hsbColor = rgb.GetHSB();
var command = new LightCommand();
command.Hue = hsb.Hue;
command.Saturation = hsb.Saturation;
command.Brightness = (byte)hsb.Brightness;
...

To get actual color from hue state could be like this:

var rgbColor = new HSB(){Hue = light.State.Hue, Saturation = light.State.Saturation, Brightness = light.State.Brightness }.GetRGB();

Some basic explanation:

If there is any problem with the code or the dropbox link let me know.

michielpost commented 8 years ago

Thanks for the code. I'm currently moving the ColorConversion code to a separate assembly and adding the HSB colorconversion from @CharlyTheKid. The Q42.HueApi.ColorConversions project will contain multiple colorconversion.

With this setup it's also easy to add more colorconverters and you can use the ColorConverter that works best for your usecase. It's also less tied to the Q42.HueApi, which is essentially about communicating withthe Hue Bridge.

It's still work in progress and I'm going to add more unit tests.

Have a look at https://github.com/Q42/Q42.HueApi/pull/71 and let me know if this is the right direction.

michielpost commented 8 years ago

ColorConverters are now in a separate package, so you can pick your favorite ColorConverter. PRs and new Coloronverters ar welcome

Exergist commented 3 years ago

I'm late to the party, but I was hoping someone would enlighten me about why "FF0000" isn't possible. Is it because the color RGB (and perhaps HSB) ranges are limited to 0-254 instead of the standard 0-255?