public static void HslToRgb(HSL hsl, RGB rgb) { if (hsl.Saturation == 0.0) { rgb.R = rgb.G = rgb.B = (byte) (hsl.Luminance * 255.0); } else { double hue = ((double) hsl.Hue) / 360.0; double v2 = (hsl.Luminance < 0.5) ? (hsl.Luminance * (1.0 + hsl.Saturation)) : ((hsl.Luminance + hsl.Saturation) - (hsl.Luminance * hsl.Saturation)); double v1 = (2.0 * hsl.Luminance) - v2; rgb.R = (byte) (255.0 * HueToRGB(v1, v2, hue + 0.33333333333333331)); rgb.G = (byte) (255.0 * HueToRGB(v1, v2, hue)); rgb.B = (byte) (255.0 * HueToRGB(v1, v2, hue - 0.33333333333333331)); } }
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))); } }
private void SetColor(Point mousePoint) { int hue; double sat, lum; switch (_selectMode) { default: hue = (int)(((double)mousePoint.X / Width) * 360); sat = _drawHsl.Saturation; lum = 1 - (double)mousePoint.Y / Height; break; } SelectedHsl = new HSL(hue, sat, lum); }
public static void RgbToHsl(RGB rgb, HSL hsl) { double r = ((double) rgb.R) / 255.0; double g = ((double) rgb.G) / 255.0; double b = ((double) 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.0; if (delta == 0.0) { hsl.Hue = 0; hsl.Saturation = 0.0; } else { double hue; hsl.Saturation = (hsl.Luminance < 0.5) ? (delta / (max + min)) : (delta / ((2.0 - max) - min)); double del_r = (((max - r) / 6.0) + (delta / 2.0)) / delta; double del_g = (((max - g) / 6.0) + (delta / 2.0)) / delta; double del_b = (((max - b) / 6.0) + (delta / 2.0)) / delta; if (r == max) { hue = del_b - del_g; } else if (g == max) { hue = (0.33333333333333331 + del_r) - del_b; } else { hue = (0.66666666666666663 + del_g) - del_r; } if (hue < 0.0) { hue++; } if (hue > 1.0) { hue--; } hsl.Hue = (int) (hue * 360.0); } }
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); } }
public static Color HSLToColor(HSL hsl) { byte r; byte g; byte b; if (hsl.Saturation == 0) { // gray values r = g = 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; r = (byte)(255 * HueToRGB(v1, v2, hue + (1.0d / 3))); g = (byte)(255 * HueToRGB(v1, v2, hue)); b = (byte)(255 * HueToRGB(v1, v2, hue - (1.0d / 3))); } return Color.FromArgb(r, g, b); }