public ColorHsl Add(double h, double s, double l) { double newH = ColorConversionUtils.NormalizeHue(this._h + h); double newS = ColorConversionUtils.NormalizeSaturation(this._s + s); double newL = ColorConversionUtils.NormalizeLightness(this._l + l); return(new ColorHsl(newH, newS, newL)); }
public ColorHsl(ColorRgb rgb) { double maxc = System.Math.Max(rgb.R, System.Math.Max(rgb.G, rgb.B)); double minc = System.Math.Min(rgb.R, System.Math.Min(rgb.G, rgb.B)); double delta = maxc - minc; double l = (maxc + minc) / 2.0; double h = double.NaN; double s = double.NaN; // Handle case for r,g,b all have the same value if (maxc == minc) { // Black, White, or some shade of Gray -> No Chroma this._alpha = rgb.Alpha; this._h = double.NaN; this._s = double.NaN; this._l = l; return; } // At this stage, we know R,G,B are not all set to the same value - i.e. there Chroma if (l < 0.5) { s = delta / (maxc + minc); } else { s = delta / (2.0 - maxc - minc); } double rc = (((maxc - rgb.R) / 6.0) + (delta / 2.0)) / delta; double gc = (((maxc - rgb.G) / 6.0) + (delta / 2.0)) / delta; double bc = (((maxc - rgb.B) / 6.0) + (delta / 2.0)) / delta; h = 0.0; if (rgb.R == maxc) { h = bc - gc; } else if (rgb.G == maxc) { h = (1.0 / 3.0) + rc - bc; } else if (rgb.B == maxc) { h = (2.0 / 3.0) + gc - rc; } h = ColorConversionUtils.NormalizeHue(h); this._alpha = rgb.Alpha; this._h = h; this._s = s; this._l = l; }
public ColorHsv(ColorRgb rgb) { double maxc = System.Math.Max(rgb.R, System.Math.Max(rgb.G, rgb.B)); double minc = System.Math.Min(rgb.R, System.Math.Min(rgb.G, rgb.B)); double h = double.NaN; double s = double.NaN; double v = double.NaN; // Handle case for r,g,b all have the same value if (maxc == minc) { // Black, White, or some shade of Gray -> No Chroma this._alpha = rgb.Alpha; this._h = double.NaN; this._s = double.NaN; this._v = maxc; return; } // At this stage, we know R,G,B are not all set to the same value - i.e. there is Chromatic data double delta = maxc - minc; s = delta / maxc; v = maxc; if (rgb.R == maxc) { h = 0.0 + ((rgb.G - rgb.B) / delta); } else if (rgb.G == maxc) { h = 2.0 + ((rgb.B - rgb.R) / delta); } else { h = 4.0 + ((rgb.R - rgb.G) / delta); } h = ColorConversionUtils.NormalizeHue(h / 6.0); this._alpha = rgb.Alpha; this._h = h; this._s = s; this._v = v; }
private static double hue_2_rgb(double m1, double m2, double h) { h = ColorConversionUtils.NormalizeHue(h); if ((6.0 * h) < 1.0) { return(m1 + (m2 - m1) * 6.0 * h); } if ((2.0 * h) < 1.0) { return(m2); } if ((3.0 * h) < 2.0) { return(m1 + (m2 - m1) * ((2.0 / 3.0) - h) * 6.0); } return(m1); }