/// <summary> /// Converts a WPF HSL color to an RGB color /// </summary> /// <param name="hslColor">The HSL color to convert.</param> /// <returns>An RGB color object equivalent to the HSL color object passed in.</returns> public static Color HlsToRgb(ColorHls hlsColor) { return(HlsToRgb(hlsColor.H, hlsColor.L, hlsColor.S, hlsColor.A)); }
public static ColorHls RgbToHls(Color rgbColor) { // Initialize result var hlsColor = new ColorHls(); // Convert RGB values to percentages double r = (double)rgbColor.R / 255; var g = (double)rgbColor.G / 255; var b = (double)rgbColor.B / 255; var a = (double)rgbColor.A / 255; // Find min and max RGB values var min = Math.Min(r, Math.Min(g, b)); var max = Math.Max(r, Math.Max(g, b)); var delta = max - min; /* If max and min are equal, that means we are dealing with * a shade of gray. So we set H and S to zero, and L to either * max or min (it doesn't matter which), and then we exit. */ //Special case: Gray if (max == min) { hlsColor.H = 0; hlsColor.S = 0; hlsColor.L = max; return hlsColor; } /* If we get to this point, we know we don't have a shade of gray. */ // Set L hlsColor.L = (min + max) / 2; // Set S if (hlsColor.L < 0.5) { hlsColor.S = delta / (max + min); } else { hlsColor.S = delta / (2.0 - max - min); } // Set H if (r == max) hlsColor.H = (g - b) / delta; if (g == max) hlsColor.H = 2.0 + (b - r) / delta; if (b == max) hlsColor.H = 4.0 + (r - g) / delta; hlsColor.H *= 60; if (hlsColor.H < 0) hlsColor.H += 360; // Set A hlsColor.A = a; // Set return value return hlsColor; }
public static ColorHls RgbToHls(Color rgbColor) { // Initialize result var hlsColor = new ColorHls(); // Convert RGB values to percentages double r = (double)rgbColor.R / 255; var g = (double)rgbColor.G / 255; var b = (double)rgbColor.B / 255; var a = (double)rgbColor.A / 255; // Find min and max RGB values var min = Math.Min(r, Math.Min(g, b)); var max = Math.Max(r, Math.Max(g, b)); var delta = max - min; /* If max and min are equal, that means we are dealing with * a shade of gray. So we set H and S to zero, and L to either * max or min (it doesn't matter which), and then we exit. */ //Special case: Gray if (max == min) { hlsColor.H = 0; hlsColor.S = 0; hlsColor.L = max; return(hlsColor); } /* If we get to this point, we know we don't have a shade of gray. */ // Set L hlsColor.L = (min + max) / 2; // Set S if (hlsColor.L < 0.5) { hlsColor.S = delta / (max + min); } else { hlsColor.S = delta / (2.0 - max - min); } // Set H if (r == max) { hlsColor.H = (g - b) / delta; } if (g == max) { hlsColor.H = 2.0 + (b - r) / delta; } if (b == max) { hlsColor.H = 4.0 + (r - g) / delta; } hlsColor.H *= 60; if (hlsColor.H < 0) { hlsColor.H += 360; } // Set A hlsColor.A = a; // Set return value return(hlsColor); }
/// <summary> /// Converts a WPF HSL color to an RGB color /// </summary> /// <param name="hslColor">The HSL color to convert.</param> /// <returns>An RGB color object equivalent to the HSL color object passed in.</returns> public static Color HlsToRgb(ColorHls hlsColor) { return HlsToRgb(hlsColor.H, hlsColor.L, hlsColor.S, hlsColor.A); }