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))); } }
public static HSL RgbToHsl(RGB rgb) { HSL hsl = new HSL(); RgbToHsl(rgb, hsl); return hsl; }
public static RGB HslToRgb(HSL hsl) { RGB rgb = new RGB(); HslToRgb(hsl, rgb); return rgb; }
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); } }