michielpost / Q42.HueApi

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

Error on HueColorConverter.HexColorFromState(State state, string model) #128

Closed sebastiankpunkt closed 6 years ago

sebastiankpunkt commented 6 years ago

Hi!

First, I'd like to thank you for your HueApi. I'm using it since some years for my own home automation - and it works great. :)

There might be a bug in HueColorConverter.HexColorFromState(State state, string model) in 486: The R, G and B values range from 0-1, not from 0-255. Therefore the HEX conversion failes.

But the color object already has it's own ToHex() function, so it's easy to fix.

But even if it's working, it does ignore the brightness. For me I changed the function a little so that it does not ignore the state objects brightness for the light:

                public static string HexColorFromState(State state, string model)
        {
            if (state == null)
                throw new ArgumentNullException(nameof(state));
            if (state.On == false || state.Brightness <= 1) //Off or low brightness (changed from 5 to 1)
                return "000000";
            if (state.ColorCoordinates != null && state.ColorCoordinates.Length == 2) //Based on XY value
            {
                RGBColor color = ColorFromXY(new CGPoint(state.ColorCoordinates[0], state.ColorCoordinates[1]), model);

                //Brightness of state (0.0-1.0)
                double b = Convert.ToDouble(state.Brightness) / 255;
                //color with brightness
                RGBColor colorWithB = new RGBColor(color.R * b, color.G * b, color.B * b);
                //ToHex() because it works :)
                return colorWithB.ToHex();
            }

            return "FFFFFF"; //White
        }
michielpost commented 6 years ago

Thanks! I used your code and Q42.HueApi.ColorConverters version 3.4.1 is now on NuGet