CSharp examples for System.Drawing:Color Convert
Converts RGB to HSL
using System.Windows.Media; using System.Threading.Tasks; using System.Text; using System.Linq; using System.Collections.Generic; using System;//from w w w. jav a 2 s . c o m public class Main{ // /// <summary> /// Converts RGB to HSL /// </summary> /// <returns>An HSL value</returns> public static HSL ToHsl(this Color c) { HSL hsl = new HSL(); hsl.H = c.GetHue() / 360.0; // we store hue as 0-1 as opposed to 0-360 hsl.L = c.GetBrightness(); hsl.S = c.GetSaturation(); return hsl; } public static float GetBrightness(this Color c) { byte minval = Math.Min(c.R, Math.Min(c.G, c.B)); byte maxval = Math.Max(c.R, Math.Max(c.G, c.B)); return (float)(maxval + minval) / 510; } public static float GetSaturation(this Color c) { byte minval = (byte)Math.Min(c.R, Math.Min(c.G, c.B)); byte maxval = (byte)Math.Max(c.R, Math.Max(c.G, c.B)); if (maxval == minval) return 0.0f; int sum = maxval + minval; if (sum > 255) sum = 510 - sum; return (float)(maxval - minval) / sum; } public static float GetHue(this Color c) { var r = c.R; var g = c.G; var b = c.B; byte minval = (byte)Math.Min(r, Math.Min(g, b)); byte maxval = (byte)Math.Max(r, Math.Max(g, b)); if (maxval == minval) return 0.0f; float diff = (float)(maxval - minval); float rnorm = (maxval - r) / diff; float gnorm = (maxval - g) / diff; float bnorm = (maxval - b) / diff; float hue = 0.0f; if (r == maxval) hue = 60.0f * (6.0f + bnorm - gnorm); if (g == maxval) hue = 60.0f * (2.0f + rnorm - bnorm); if (b == maxval) hue = 60.0f * (4.0f + gnorm - rnorm); if (hue > 360.0f) hue = hue - 360.0f; return hue; } }