public static Color HslToRgb(HSL hsl) { hsl.H = hsl.H / 360d; Color rgb = new Color(); double hue = hsl.H; double saturation = hsl.S; double lightness = hsl.L; rgb.A = 255; if (saturation == 0) { rgb.R = rgb.G = rgb.B = (byte)(lightness * 255); } else { double tmp1, tmp2; if (lightness < 0.5) { tmp2 = lightness * (1d + saturation); } else { tmp2 = (lightness + saturation) - (saturation * lightness); } tmp1 = 2 * lightness - tmp2; rgb.R = (byte)Math.Round(255 * Hue2Rgb(tmp1, tmp2, hue + (1d / 3d))); rgb.G = (byte)Math.Round(255 * Hue2Rgb(tmp1, tmp2, hue)); rgb.B = (byte)Math.Round(255 * Hue2Rgb(tmp1, tmp2, hue - (1d / 3d))); } return(rgb); }
public static HSL RgbToHsl(Color color) { double r = color.R; double g = color.G; double b = color.B; double hue = 0; if ((r >= g) && (g >= b)) { hue = 60 * (g - b) / (r - b); } else if ((g > r) && (r >= b)) { hue = 60 * (2 - (r - b) / (g - b)); } else if ((g >= b) && (b > r)) { hue = 60 * (2 + (b - r) / (g - r)); } else if ((b > g) && (g > r)) { hue = 60 * (4 - (g - r) / (b - r)); } else if ((b > r) && (r >= g)) { hue = 60 * (4 + (r - g) / (b - g)); } else if ((r >= b) && (b > g)) { hue = 60 * (6 - (b - g) / (r - g)); } r = r / 255d; g = g / 255d; b = b / 255d; double var_Min = Math.Min(Math.Min(r, g), b); double var_Max = Math.Max(Math.Max(r, g), b); double del_Max = var_Max - var_Min; double l = ((var_Max + var_Min) / 2.0); double s; if (del_Max == 0) { hue = s = 0; } else { if (l < 0.5) { s = del_Max / (var_Max + var_Min); } else { s = (del_Max / (2.0 - var_Max - var_Min)); } } HSL result = new HSL(hue, s, l); return(result); }