/// <summary> /// Converts a color from HSL to RGB. </summary> /// <remarks>Adapted from the algorithm in Foley and Van-Dam.</remarks> /// <param name="colorToConvert">The HSL value.</param> /// <returns>A Color structure containing the equivalent RGB values.</returns> public static GUIColor HSLToRGB(HSL colorToConvert) { #if DEBUG if (colorToConvert == null) { throw new ArgumentNullException("colorToConvert"); } #endif // ReSharper disable JoinDeclarationAndInitializer int max, mid, min; double q; // ReSharper restore JoinDeclarationAndInitializer max = Round(colorToConvert.L * 255); min = Round((1.0 - colorToConvert.S) * (colorToConvert.L / 1.0) * 255); q = (double)(max - min) / 255; if (colorToConvert.H >= 0 && colorToConvert.H <= (double)1 / 6) { mid = Round((((colorToConvert.H - 0) * q) * 1530) + min); return(GUIColor.FromARGB(max, mid, min)); } if (colorToConvert.H <= (double)1 / 3) { mid = Round((-((colorToConvert.H - ((double)1 / 6)) * q) * 1530) + max); return(GUIColor.FromARGB(mid, max, min)); } if (colorToConvert.H <= 0.5) { mid = Round((((colorToConvert.H - ((double)1 / 3)) * q) * 1530) + min); return(GUIColor.FromARGB(min, max, mid)); } if (colorToConvert.H <= (double)2 / 3) { mid = Round((-((colorToConvert.H - 0.5) * q) * 1530) + max); return(GUIColor.FromARGB(min, mid, max)); } if (colorToConvert.H <= (double)5 / 6) { mid = Round((((colorToConvert.H - ((double)2 / 3)) * q) * 1530) + min); return(GUIColor.FromARGB(mid, min, max)); } if (colorToConvert.H <= 1.0) { mid = Round((-((colorToConvert.H - ((double)5 / 6)) * q) * 1530) + max); return(GUIColor.FromARGB(max, min, mid)); } return(GUIColor.FromARGB(0, 0, 0)); }
/// <summary>Converts CMYK to RGB.</summary> /// <param name="colorToConvert">A color to convert.</param> /// <returns>A Color object.</returns> public static GUIColor CMYKToRGB(CMYK colorToConvert) { #if DEBUG if (colorToConvert == null) { throw new ArgumentNullException("colorToConvert"); } #endif int red, green, blue; red = Round(255 - (255 * colorToConvert.Cyan)); green = Round(255 - (255 * colorToConvert.Magenta)); blue = Round(255 - (255 * colorToConvert.Yellow)); return(GUIColor.FromARGB(red, green, blue)); }
/// <summary> /// Contrasts the color. /// </summary> /// <param name="color">The color.</param> /// <returns>The contrasted color.</returns> public static GUIColor ContrastColor(GUIColor color) { int d; // Counting the perceptive luminance - human eye favors green color... var ar = 0.299 * color.R; var ag = 0.587 * color.G; var ab = 0.114 * color.B; var a = 1 - ((ar + ag + ab) / 255); if (a < 0.5) { d = 0; // bright colors - black font } else { d = 255; // dark colors - white font } return(GUIColor.FromARGB(d, d, d)); }