Beispiel #1
0
        public LifxBulb(byte[] mac, IPhysicalLed led)
        {
            this.led = led;

            this.label = "Demo";

            this.Tags = new byte[LifxConst.BulbTagsLength];

            this.TagLabels = string.Empty;

            this.Address = BitHelper.ReadUInt64(mac, 0);

            this.colorHsbk = new LifxHsbkColor
            {
                Brightness = 1,
                Hue = 300,
                Saturation = 0,
                Kelvin = 2000
            };

            this.colorRgb = this.colorHsbk.ToRgb();

            this.Power = 65535;

            this.Location = "Home";
        }
Beispiel #2
0
        public static LifxHsbkColor ToHsbk(LifxRgbColor color)
        {
            var result = new LifxHsbkColor();

            int max = Math.Max(color.Red, Math.Max(color.Green, color.Blue));
            int min = Math.Min(color.Red, Math.Min(color.Green, color.Blue));

            result.Brightness = max; // v

            var delta = max - min;

            if (max > 0.0)
            {
                result.Saturation = delta / max; // s
            }
            else
            {
                // r = g = b = 0                        // s = 0, v is undefresulted
                result.Saturation = 0;
                result.Hue = -1; // NAN;                            // its now undefresulted
                return result;
            }

            if (color.Red >= max) // > is bogus, just keeps compilor happy
            {
                result.Hue = (color.Green - color.Blue) / delta; // between yellow & magenta
            }
            else if (color.Green >= max)
            {
                result.Hue = (ushort)(2.0 + (color.Blue - color.Red) / delta); // between cyan & yellow
            }
            else
            {
                result.Hue = (ushort)(4.0 + (color.Red - color.Green) / delta); // between magenta & cyan
            }

            result.Hue *= 60.0; // degrees

            if (result.Hue < 0.0)
            {
                result.Hue += 360.0;
            }

            return result;
        }
Beispiel #3
0
        public static LifxRgbColor ToRgb(long kelvin)
        {
            var result = new LifxRgbColor();
            var temperature = kelvin / 100;

            if (temperature <= 66)
            {
                result.Red = 255;
            }
            else
            {
                var red = (int)temperature - 60;
                red = (int)(329.698727446 * Math.Pow(red, -0.1332047592));

                if (red < 0)
                {
                    result.Red = 0;
                }
                else if (red > 255)
                {
                    result.Red = 255;
                }
                else
                {
                    result.Red = (byte)red;
                }
            }

            if (temperature <= 66)
            {
                var green = (int)temperature;
                green = (int)(99.4708025861 * Math.Log(green) - 161.1195681661);
                if (green < 0)
                {
                    result.Green = 0;
                }
                else if (result.Green > 255)
                {
                    result.Green = 255;
                }
                else
                {
                    result.Green = (byte)green;
                }
            }
            else
            {
                var green = (int)(temperature - 60);
                green = (int)(288.1221695283 * Math.Pow(green, -0.0755148492));
                if (green < 0)
                {
                    result.Green = 0;
                }
                else if (result.Green > 255)
                {
                    result.Green = 255;
                }
                else
                {
                    result.Green = (byte)green;
                }
            }

            if (temperature >= 66)
            {
                result.Blue = 255;
            }
            else
            {
                if (temperature <= 19)
                {
                    result.Blue = 0;
                }
                else
                {
                    var blue = (int)(temperature - 10);
                    blue = (int)(138.5177312231 * Math.Log(blue) - 305.0447927307);
                    if (blue < 0)
                    {
                        result.Blue = 0;
                    }
                    else if (blue > 255)
                    {
                        result.Blue = 255;
                    }
                    else
                    {
                        result.Blue = (byte)blue;
                    }
                }
            }

            return result;
        }
Beispiel #4
0
        private void FadeTo(LifxRgbColor toColor, TimeSpan delay)
        {
            this.fadingStep = 0;
            this.fadeFrom = this.CurrentColor;
            this.fadeTo = toColor;

            this.StartFading(this.FadeDelay);
        }
Beispiel #5
0
        private void Callback(object state)
        {
            this.color = Fade(this.fadeFrom, this.fadeTo, this.fadingStep, this.FadeSteps);
            //this.colorHsv = Fade(this.fadeFrom, this.fadeTo, this.fadingStep, this.FadeSteps)

            this.ApplyColor(this.color);

            this.fadingStep++;

            if (this.FadeSteps == this.fadingStep)
            {
                this.StopFading();

                switch (this.mode)
                {
                    case LifxBulbMode.RandomHue:
                        this.FadeTo(new LifxRgbColor(), this.FadeDelay);
                        break;

                    case LifxBulbMode.Fixed:
                        return;
                }
            }
        }
Beispiel #6
0
        private static LifxRgbColor Fade(LifxRgbColor from, LifxRgbColor to, int step, int steps)
        {
            var red = (byte)Fade(from.Red, to.Red, step, steps);
            var green = (byte)Fade(from.Red, to.Red, step, steps);
            var blue = (byte)Fade(from.Red, to.Red, step, steps);

            return new LifxRgbColor(red, green, blue);
        }
Beispiel #7
0
 protected abstract void ApplyColor(LifxRgbColor color);
Beispiel #8
0
 public void FadeTo(LifxRgbColor toColor)
 {
     this.FadeTo(toColor, TimeSpan.Zero);
 }