Beispiel #1
0
        public static void FromRGB(RGB rgb, 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;

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

            if (delta == 0)
            {
                hsl.Hue        = 0;
                hsl.Saturation = 0.0f;
            }
            else
            {
                hsl.Saturation = (hsl.Luminance <= 0.5) ? (delta / (max + min)) : (delta / (2 - max - min));

                float hue;

                if (r == max)
                {
                    hue = ((g - b) / 6) / delta;
                }
                else if (g == max)
                {
                    hue = (1.0f / 3) + ((b - r) / 6) / delta;
                }
                else
                {
                    hue = (2.0f / 3) + ((r - g) / 6) / delta;
                }

                if (hue < 0)
                {
                    hue += 1;
                }
                if (hue > 1)
                {
                    hue -= 1;
                }

                hsl.Hue = (int)(hue * 360);
            }
        }
Beispiel #2
0
        public static void ToRGB(HSL hsl, RGB rgb)
        {
            if (hsl.Saturation == 0)
            {
                // gray values
                rgb.Red = rgb.Green = rgb.Blue = (byte)(hsl.Luminance * 255);
            }
            else
            {
                float v1, v2;
                float hue = (float)hsl.Hue / 360;

                v2 = (hsl.Luminance < 0.5) ?
                     (hsl.Luminance * (1 + hsl.Saturation)) :
                     ((hsl.Luminance + hsl.Saturation) - (hsl.Luminance * hsl.Saturation));
                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;
        }