public static HslColor FromRgb(byte R, byte G, byte B) { HslColor c = new HslColor(); c.A = 1; double r = ByteToPct(R); double g = ByteToPct(G); double b = ByteToPct(B); double max = Math.Max(b, Math.Max(r, g)); double min = Math.Min(b, Math.Min(r, g)); if (max == min) { c.H = 0; } else if (max == r && g >= b) { c.H = 60 * ((g - b) / (max - min)); } else if (max == r && g < b) { c.H = 60 * ((g - b) / (max - min)) + 360; } else if (max == g) { c.H = 60 * ((b - r) / (max - min)) + 120; } else if (max == b) { c.H = 60 * ((r - g) / (max - min)) + 240; } c.L = .5 * (max + min); if (max == min) { c.S = 0; } else if (c.L <= .5) { c.S = (max - min) / (2 * c.L); } else if (c.L > .5) { c.S = (max - min) / (2 - 2 * c.L); } return c; }
public HslColor Lighten(double pct) { HslColor c = new HslColor(); c.A = this.A; c.H = this.H; c.S = this.S; c.L = Math.Min(Math.Max(this.L + pct, 0), 1); return c; }