示例#1
0
        public static void HslToRgb(HSL hsl, RGB rgb)
        {
            if (hsl.Saturation == 0)
            {
                rgb.R = rgb.G = rgb.B = (byte)(hsl.Luminance * 255);
            }
            else
            {
                double v1, v2;
                double hue = (double)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.R = (byte)(255 * HueToRGB(v1, v2, hue + (1.0 / 3)));
                rgb.G = (byte)(255 * HueToRGB(v1, v2, hue));
                rgb.B = (byte)(255 * HueToRGB(v1, v2, hue - (1.0 / 3)));
            }
        }
示例#2
0
 public static HSL RgbToHsl(RGB rgb)
 {
     HSL hsl = new HSL();
     RgbToHsl(rgb, hsl);
     return hsl;
 }
示例#3
0
 public static RGB HslToRgb(HSL hsl)
 {
     RGB rgb = new RGB();
     HslToRgb(hsl, rgb);
     return rgb;
 }
示例#4
0
        public static void RgbToHsl(RGB rgb, HSL hsl)
        {
            double r = (rgb.R / 255.0);
            double g = (rgb.G / 255.0);
            double b = (rgb.G / 255.0);

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

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

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

                double del_r = (((max - r) / 6) + (delta / 2)) / delta;
                double del_g = (((max - g) / 6) + (delta / 2)) / delta;
                double del_b = (((max - b) / 6) + (delta / 2)) / delta;
                double hue;

                if (r == max)
                {
                    hue = del_b - del_g;
                }
                else if (g == max)
                {
                    hue = (1.0 / 3) + del_r - del_b;
                }
                else
                {
                    hue = (2.0 / 3) + del_g - del_r;
                }

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

                hsl.Hue = (int)(hue * 360);
            }
        }