/// <summary> /// Converts a colour from HSL to RGB /// </summary> /// <remarks>Adapted from the algoritm in Foley and Van-Dam</remarks> /// <param name="hsl">The HSL value</param> /// <returns>A Color structure containing the equivalent RGB values</returns> public static Color HSL_to_RGB(HSL hsl) { int Max, Mid, Min; double q; Max = Round(hsl.L * 255); Min = Round((1.0 - hsl.S) * (hsl.L / 1.0) * 255); q = (double)(Max - Min) / 255; if (hsl.H >= 0 && hsl.H <= (double)1 / 6) { Mid = Round(((hsl.H - 0) * q) * 1530 + Min); return(Color.FromArgb(Max, Mid, Min)); } else if (hsl.H <= (double)1 / 3) { Mid = Round(-((hsl.H - (double)1 / 6) * q) * 1530 + Max); return(Color.FromArgb(Mid, Max, Min)); } else if (hsl.H <= 0.5) { Mid = Round(((hsl.H - (double)1 / 3) * q) * 1530 + Min); return(Color.FromArgb(Min, Max, Mid)); } else if (hsl.H <= (double)2 / 3) { Mid = Round(-((hsl.H - 0.5) * q) * 1530 + Max); return(Color.FromArgb(Min, Mid, Max)); } else if (hsl.H <= (double)5 / 6) { Mid = Round(((hsl.H - (double)2 / 3) * q) * 1530 + Min); return(Color.FromArgb(Mid, Min, Max)); } else if (hsl.H <= 1.0) { Mid = Round(-((hsl.H - (double)5 / 6) * q) * 1530 + Max); return(Color.FromArgb(Max, Min, Mid)); } else { return(Color.FromArgb(0, 0, 0)); } }
/// <summary> /// Converts RGB to HSL /// </summary> /// <remarks>Takes advantage of whats already built in to .NET by using the Color.GetHue, Color.GetSaturation and Color.GetBrightness methods</remarks> /// <param name="c">A Color to convert</param> /// <returns>An HSL value</returns> public static HSL RGB_to_HSL(Color c) { HSL hsl = new HSL(); int Max, Min, Diff, Sum; // Of our RGB values, assign the highest value to Max, and the Smallest to Min if (c.R > c.G) { Max = c.R; Min = c.G; } else { Max = c.G; Min = c.R; } if (c.B > Max) { Max = c.B; } else if (c.B < Min) { Min = c.B; } Diff = Max - Min; Sum = Max + Min; // Luminance - a.k.a. Brightness - Adobe photoshop uses the logic that the // site VBspeed regards (regarded) as too primitive = superior decides the // level of brightness. hsl.L = (double)Max / 255; // Saturation if (Max == 0) { hsl.S = 0; // Protecting from the impossible operation of division by zero. } else { hsl.S = (double)Diff / Max; // The logic of Adobe Photoshops is this simple. } // Hue R is situated at the angel of 360 eller noll degrees; // G vid 120 degrees // B vid 240 degrees double q; if (Diff == 0) { q = 0; // Protecting from the impossible operation of division by zero. } else { q = (double)60 / Diff; } if (Max == c.R) { if (c.G < c.B) { hsl.H = (double)(360 + q * (c.G - c.B)) / 360; } else { hsl.H = (double)(q * (c.G - c.B)) / 360; } } else if (Max == c.G) { hsl.H = (double)(120 + q * (c.B - c.R)) / 360; } else if (Max == c.B) { hsl.H = (double)(240 + q * (c.R - c.G)) / 360; } else { hsl.H = 0.0; } return(hsl); }