Example #1
0
        /// <summary>
        ///     Convert from RGB to HSL color space.
        /// </summary>
        /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
        /// <param name="hsl">Destination color in <b>HSL</b> color space.</param>
        /// <remarks>
        ///     <para>
        ///         See
        ///         <a href="http://en.wikipedia.org/wiki/HSI_color_space#Conversion_from_RGB_to_HSL_or_HSV">HSL and HSV Wiki</a>
        ///         for information about the algorithm to convert from RGB to HSL.
        ///     </para>
        /// </remarks>
        public static void FromRGB([NotNull] Rgb rgb, [NotNull] Hsl hsl)
        {
            float r = rgb.Red / 255.0f;
            float g = rgb.Green / 255.0f;
            float b = rgb.Blue / 255.0f;

            float min   = Math.Min(Math.Min(r, g), b);
            float max   = Math.Max(Math.Max(r, g), b);
            float delta = max - min;

            // get luminance value
            hsl.Luminance = (max + min) / 2;

            if (Math.Abs(delta) < float.Epsilon)
            {
                // gray color
                hsl.Hue        = 0;
                hsl.Saturation = 0.0f;
            }
            else
            {
                // get saturation value
                hsl.Saturation = hsl.Luminance <= 0.5
                                                                        ? delta / (max + min)
                                                                        : delta / (2 - max - min);

                // get hue value
                float hue = Math.Abs(r - max) < float.Epsilon
                                                                ? (g - b) / 6 / delta
                                                                : Math.Abs(g - max) < float.Epsilon
                                                                        ? 1.0f / 3 + (b - r) / 6 / delta
                                                                        : 2.0f / 3 + (r - g) / 6 / delta;

                // correct hue if needed
                if (hue < 0)
                {
                    hue += 1;
                }
                if (hue > 1)
                {
                    hue -= 1;
                }
                hsl.Hue = (int)(hue * 360);
            }
        }
Example #2
0
        /// <summary>
        ///     Convert from HSL to RGB color space.
        /// </summary>
        /// <param name="hsl">Source color in <b>HSL</b> color space.</param>
        /// <param name="rgb">Destination color in <b>RGB</b> color space.</param>
        public static void ToRGB([NotNull] Hsl hsl, [NotNull] Rgb rgb)
        {
            if (Math.Abs(hsl.Saturation) < float.Epsilon)
            {
                // gray values
                rgb.Red = rgb.Green = rgb.Blue = (byte)(hsl.Luminance * 255);
            }
            else
            {
                float hue = (float)hsl.Hue / 360;
                float v2  = hsl.Luminance < 0.5
                                                                ? hsl.Luminance * (1 + hsl.Saturation)
                                                                : hsl.Luminance + hsl.Saturation - hsl.Luminance * hsl.Saturation;
                float v1 = 2 * hsl.Luminance - v2;
                rgb.Red   = (byte)(255 * Hue_2_RGB(v1, v2, hue + 1.0f / 3));
                rgb.Green = (byte)(255 * Hue_2_RGB(v1, v2, hue));
                rgb.Blue  = (byte)(255 * Hue_2_RGB(v1, v2, hue - 1.0f / 3));
            }

            rgb.Alpha = 255;
        }