Exemplo n.º 1
0
        void FromRGB(RgbColor cc)
        {
            double r = (double)cc.R / 255d;
            double g = (double)cc.G / 255d;
            double b = (double)cc.B / 255d;

            double min = Math.Min(Math.Min(r, g), b);
            double max = Math.Max(Math.Max(r, g), b);

            // calulate hue according formula given in
            // "Conversion from RGB to HSL or HSV"
            m_hue = 0;
            if (min != max)
            {
                if (r == max && g >= b)
                {
                    m_hue = 60 * ((g - b) / (max - min)) + 0;
                }
                else
                if (r == max && g < b)
                {
                    m_hue = 60 * ((g - b) / (max - min)) + 360;
                }
                else
                if (g == max)
                {
                    m_hue = 60 * ((b - r) / (max - min)) + 120;
                }
                else
                if (b == max)
                {
                    m_hue = 60 * ((r - g) / (max - min)) + 240;
                }
            }
            // find lightness
            m_lightness = (min + max) / 2;

            // find saturation
            if (m_lightness == 0 || min == max)
            {
                m_saturation = 0;
            }
            else
            if (m_lightness > 0 && m_lightness <= 0.5)
            {
                m_saturation = (max - min) / (2 * m_lightness);
            }
            else
            if (m_lightness > 0.5)
            {
                m_saturation = (max - min) / (2 - 2 * m_lightness);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts HEX string or name of the RGB color. For example #123456, blue, red, orange
        /// </summary>
        /// <returns>The string.</returns>
        /// <param name="color">Color string.</param>
        public static RgbColor FromString(String color)
        {
            RgbColor result = new RgbColor();

            if (_NamesToHex.ContainsKey(color.ToLower()))
            {
                color = _NamesToHex[color.ToLower()];
            }
            else
            {
                if (!IsValidColor(color))
                {
                    throw new Exception("Color value is invalid");
                }
            }

            result.R = Convert.ToByte(color.Substring(1, 2), 16);
            result.G = Convert.ToByte(color.Substring(3, 2), 16);
            result.B = Convert.ToByte(color.Substring(5, 2), 16);

            return(result);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Get black color.
 /// </summary>
 public static RgbColor Black()
 {
     return(RgbColor.FromRgb(0, 0, 0));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Blink the LED.
 /// </summary>
 /// <param name="color">Must be in #rrggbb format or named color ("red", "green", "blue")</param>
 /// <param name="repeats">How many times to repeat (default 1)</param>
 /// <param name="delay">Delay delay between on/off sequences (default 500)</param>
 public void Blink(RgbColor color, int repeats = 1, int delay = 500)
 {
     this.Blink(0, 0, color, repeats, delay);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Blink the LED on BlinkStick Pro.
 /// </summary>
 /// <param name="channel">Channel (0 - R, 1 - G, 2 - B)</param>
 /// <param name="index">Index of the LED</param>
 /// <param name="color">Must be in #rrggbb format or named color ("red", "green", "blue")</param>
 /// <param name="repeats">How many times to repeat (default 1)</param>
 /// <param name="delay">Delay delay between on/off sequences (default 500)</param>
 public void Blink(byte channel, byte index, string color, int repeats = 1, int delay = 500)
 {
     this.Blink(channel, index, RgbColor.FromString(color), repeats, delay);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Blink the LED on BlinkStick Pro.
 /// </summary>
 /// <param name="channel">Channel (0 - R, 1 - G, 2 - B)</param>
 /// <param name="index">Index of the LED</param>
 /// <param name="color">Color parameter as RgbColor class instance</param>
 /// <param name="repeats">How many times to repeat (default 1)</param>
 /// <param name="delay">Delay delay between on/off sequences (default 500)</param>
 public void Blink(byte channel, byte index, RgbColor color, int repeats = 1, int delay = 500)
 {
     this.Blink(channel, index, color.R, color.G, color.B, repeats, delay);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Sets the color of the led.
 /// </summary>
 /// <param name="channel">Channel (0 - R, 1 - G, 2 - B)</param>
 /// <param name="index">Index of the LED</param>
 /// <param name="color">Color parameter as RgbColor class instance</param>
 public void SetColor(byte channel, byte index, RgbColor color)
 {
     SetColor(channel, index, color.R, color.G, color.B);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Morph from current color to new color on BlinkStick Pro.
 /// </summary>
 /// <param name="channel">Channel (0 - R, 1 - G, 2 - B)</param>
 /// <param name="index">Index of the LED</param>
 /// <param name="color">Must be in #rrggbb format or named color ("red", "green", "blue")</param>
 /// <param name="duration">How long should the morph last</param>
 /// <param name="steps">How many steps for color changes</param>
 public void Morph(byte channel, byte index, string color, int duration = 1000, int steps = 50)
 {
     this.Morph(channel, index, RgbColor.FromString(color), duration, steps);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Sets the color of the led.
 /// </summary>
 /// <param name="color">Color as RgbColor class.</param>
 public void SetColor(RgbColor color)
 {
     SetColor(color.R, color.G, color.B);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Sets the color of the led.
 /// </summary>
 /// <param name="color">Must be in #rrggbb format</param>
 public void SetColor(String color)
 {
     SetColor(RgbColor.FromString(color));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Pulse specified color.
 /// </summary>
 /// <param name="color">Must be in #rrggbb format or named color ("red", "green", "blue")</param>
 /// <param name="duration">How long should the morph last</param>
 /// <param name="steps">How many steps for color changes</param>
 public void Pulse(RgbColor color, int repeats = 1, int duration = 1000, int steps = 50)
 {
     this.Pulse(0, 0, color, repeats, duration, steps);
 }
Exemplo n.º 12
0
 /// <summary>
 /// Pulse specified color on BlinkStick Pro.
 /// </summary>
 /// <param name="channel">Channel (0 - R, 1 - G, 2 - B)</param>
 /// <param name="index">Index of the LED</param>
 /// <param name="color">Must be in #rrggbb format or named color ("red", "green", "blue")</param>
 /// <param name="duration">How long should the morph last</param>
 /// <param name="steps">How many steps for color changes</param>
 public void Pulse(byte channel, byte index, string color, int repeats = 1, int duration = 1000, int steps = 50)
 {
     this.Pulse(channel, index, RgbColor.FromString(color), repeats, duration, steps);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Pulse specified color on BlinkStick Pro.
 /// </summary>
 /// <param name="channel">Channel (0 - R, 1 - G, 2 - B)</param>
 /// <param name="index">Index of the LED</param>
 /// <param name="color">Color parameter as RgbColor class instance</param>
 /// <param name="duration">How long should the morph last</param>
 /// <param name="steps">How many steps for color changes</param>
 public void Pulse(byte channel, byte index, RgbColor color, int repeats = 1, int duration = 1000, int steps = 50)
 {
     this.Pulse(channel, index, color.R, color.G, color.B, repeats, duration, steps);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Morph from current color to new color.
 /// </summary>
 /// <param name="color">Must be in #rrggbb format or named color ("red", "green", "blue")</param>
 /// <param name="duration">How long should the morph last</param>
 /// <param name="steps">How many steps for color changes</param>
 public void Morph(RgbColor color, int duration = 1000, int steps = 50)
 {
     this.Morph(0, 0, color, duration, steps);
 }
Exemplo n.º 15
0
        RgbColor ToRGB()
        {
            // convert to RGB according to
            // "Conversion from HSL to RGB"

            double r = m_lightness;
            double g = m_lightness;
            double b = m_lightness;

            if (m_saturation == 0)
            {
                return(RgbColor.FromRgb((int)(r * 255), (int)(g * 255), (int)(b * 255)));
            }

            double q = 0;

            if (m_lightness < 0.5)
            {
                q = m_lightness * (1 + m_saturation);
            }
            else
            {
                q = m_lightness + m_saturation - (m_lightness * m_saturation);
            }
            double p  = 2 * m_lightness - q;
            double hk = m_hue / 360;

            // r,g,b colors
            double[] tc = new double[3] {
                hk + (1d / 3d), hk, hk - (1d / 3d)
            };
            double[] colors = new double[3] {
                0, 0, 0
            };

            for (int color = 0; color < colors.Length; color++)
            {
                if (tc[color] < 0)
                {
                    tc[color] += 1;
                }
                if (tc[color] > 1)
                {
                    tc[color] -= 1;
                }

                if (tc[color] < (1d / 6d))
                {
                    colors[color] = p + ((q - p) * 6 * tc[color]);
                }
                else
                if (tc[color] >= (1d / 6d) && tc[color] < (1d / 2d))
                {
                    colors[color] = q;
                }
                else
                if (tc[color] >= (1d / 2d) && tc[color] < (2d / 3d))
                {
                    colors[color] = p + ((q - p) * 6 * (2d / 3d - tc[color]));
                }
                else
                {
                    colors[color] = p;
                }

                colors[color] *= 255;                 // convert to value expected by Color
            }
            return(RgbColor.FromRgb((int)colors[0], (int)colors[1], (int)colors[2]));
        }
Exemplo n.º 16
0
 /// <summary>
 /// Sets the color of the led.
 /// </summary>
 /// <param name="channel">Channel (0 - R, 1 - G, 2 - B)</param>
 /// <param name="index">Index of the LED</param>
 /// <param name="color">Must be in #rrggbb format or named color ("red", "green", "blue")</param>
 public void SetColor(byte channel, byte index, string color)
 {
     SetColor(channel, index, RgbColor.FromString(color));
 }
Exemplo n.º 17
0
 /// <summary>
 /// Morph from current color to new color on BlinkStick Pro.
 /// </summary>
 /// <param name="channel">Channel (0 - R, 1 - G, 2 - B)</param>
 /// <param name="index">Index of the LED</param>
 /// <param name="color">Color parameter as RgbColor class instance</param>
 /// <param name="duration">How long should the morph last</param>
 /// <param name="steps">How many steps for color changes</param>
 public void Morph(byte channel, byte index, RgbColor color, int duration = 1000, int steps = 50)
 {
     this.Morph(channel, index, color.R, color.G, color.B, duration, steps);
 }