public static HslColor FromColor(Color color) { double r = color.R / 255.0; double g = color.G / 255.0; double b = color.B / 255.0; var hsl = new HslColor {A = color.A / 255.0}; double v = Math.Max(r, g); v = Math.Max(v, b); double m = Math.Min(r, g); m = Math.Min(m,b); hsl.L = (m + v) / 2.0; if (hsl.L <= 0.0) return hsl; double vm = v - m; hsl.S = vm; if (hsl.S > 0.0) hsl.S /= (hsl.L <= 0.5) ? (v + m ) : (2.0 - v - m); else return hsl; double r2 = (v - r) / vm; double g2 = (v - g) / vm; double b2 = (v - b) / vm; if (Math.Abs(r - v) < double.Epsilon) { hsl.H = (Math.Abs(g - m) < double.Epsilon ? 5.0 + b2 : 1.0 - g2); } else if (Math.Abs(g - v) < double.Epsilon) { hsl.H = (Math.Abs(b - m) < double.Epsilon ? 1.0 + r2 : 3.0 - b2); } else { hsl.H = (Math.Abs(r - m) < double.Epsilon ? 3.0 + g2 : 5.0 - r2); } hsl.H /= 6.0; return hsl; }