Example #1
0
		public HSLColor(RgbColor color)
		{
			m_hue = 0;
			m_saturation = 1;
			m_lightness = 1;
			FromRGB(color);
		}
Example #2
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);
            }
        }
Example #3
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);
		}
Example #4
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);
        }
 /// <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);
 }
 /// <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);
 }
 /// <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);
 }
Example #8
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);
 }
Example #9
0
        /// <summary>
        /// Froms the rgb value from int components.
        /// </summary>
        /// <returns>The rgb.</returns>
        /// <param name="r">The red component.</param>
        /// <param name="g">The green component.</param>
        /// <param name="b">The blue component.</param>
        public static RgbColor FromRgb(int r, int g, int b)
        {
            RgbColor color = new RgbColor();
            color.R = (byte)r;
            color.G = (byte)g;
            color.B = (byte)b;

            return color;
        }
 /// <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);
 }
 /// <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);
 }
Example #12
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);
 }
Example #13
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);
 }
Example #14
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);
 }
Example #15
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);
 }
Example #16
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);
 }
Example #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);
 }
Example #18
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);
 }
 /// <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);
 }
 /// <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));
 }
 /// <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);
 }
 /// <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));
 }
 /// <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);
 }
Example #24
0
 /// <summary>
 /// Froms the color of the GDK color.
 /// </summary>
 /// <returns>The gdk color.</returns>
 /// <param name="r">The red component.</param>
 /// <param name="g">The green component.</param>
 /// <param name="b">The blue component.</param>
 public static RgbColor FromGdkColor(ushort r, ushort g, ushort b)
 {
     RgbColor color = new RgbColor();
     
     color.R = (byte)(r / 0x100);
     color.G = (byte)(g / 0x100);
     color.B = (byte)(b / 0x100);
     
     return color;
 }
 /// <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);
 }
Example #26
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;
        }
 /// <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);
 }
Example #28
0
 /// <summary>
 /// Get black color.
 /// </summary>
 public static RgbColor Black()
 {
     return(RgbColor.FromRgb(0, 0, 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);
 }
Example #30
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]));
        }