/// <summary> /// Creates from a given color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static HslColor FromHslColor( HslColor color ) { return new HslColor( color.PreciseHue, color.PreciseSaturation, color.PreciseLight ); }
/// <summary> /// Creates from a given color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static RgbColor FromHslColor( HslColor color ) { return color.ToRgbColor(); }
/// <summary> /// HSL to RGB. /// </summary> /// <param name="hsl">The HSL.</param> public static RgbColor HslToRgb( HslColor hsl ) { double v; double r, g, b; double H = hsl.PreciseHue / 360.0; double S = hsl.PreciseSaturation / 100.0; double L = hsl.PreciseLight / 100.0; r = L; // default to gray g = L; b = L; v = (L <= 0.5) ? (L * (1.0 + S)) : (L + S - L * S); if (v > 0) { double m; double sv; int sextant; double fract, vsf, mid1, mid2; m = L + L - v; sv = (v - m) / v; H *= 6.0; sextant = (int)H; fract = H - sextant; vsf = v * sv * fract; mid1 = m + vsf; mid2 = v - vsf; switch (sextant) { case 0: r = v; g = mid1; b = m; break; case 1: r = mid2; g = v; b = m; break; case 2: r = m; g = v; b = mid1; break; case 3: r = m; g = mid2; b = v; break; case 4: r = mid1; g = m; b = v; break; case 5: r = v; g = m; b = mid2; break; } } var nRed = Convert.ToByte(r * 255.0f); var nGreen = Convert.ToByte(g * 255.0f); var nBlue = Convert.ToByte(b * 255.0f); //Debug.WriteLine(string.Format("r={0}, b={1}, g={2}", nRed, nBlue, nGreen)); return new RgbColor(nRed, nGreen, nBlue); /* double h = hsl.PreciseHue / 360.0; double s = hsl.PreciseSaturation / 100.0; double l = hsl.PreciseLight / 100.0; double r, g, b; if (s == 0) { r = l; g = l; b = l; } else { double t1, t2; double th = h / 6.0d; if (l < 0.5d) { t2 = l * (1d + s); } else { t2 = (l + s) - (l * s); } t1 = 2d * l - t2; double tr, tg, tb; tr = th + (1.0d / 3.0d); tg = th; tb = th - (1.0d / 3.0d); r = ColorCalc(tr, t1, t2); g = ColorCalc(tg, t1, t2); b = ColorCalc(tb, t1, t2); } var nRed = (int)Math.Round(r * 255.0); var nGreen = (int)Math.Round(g * 255.0); var nBlue = (int)Math.Round(b * 255.0); return new RgbColor(nRed, nGreen, nBlue); */ /* double red, green, blue; double h = hsl.PreciseHue / 360.0; double s = hsl.PreciseSaturation / 100.0; double l = hsl.PreciseLight / 100.0; if ( s == 0.0 ) { red = l; green = l; blue = l; } else { double var2; if ( l < 0.5 ) { var2 = l * (1.0 + s); } else { var2 = (l + s) - (s * l); } double var1 = 2.0 * l - var2; red = hue_2_RGB( var1, var2, h + (1.0 / 3.0) ); green = hue_2_RGB( var1, var2, h ); blue = hue_2_RGB( var1, var2, h - (1.0 / 3.0) ); } // -- var nRed = (int)Math.Round( red * 255.0 ); var nGreen = (int)Math.Round( green * 255.0 ); var nBlue = (int)Math.Round( blue * 255.0 ); return new RgbColor( nRed, nGreen, nBlue ); */ }
/// <summary> /// Creates from a given color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static HsbColor FromHslColor( HslColor color ) { return FromRgbColor( color.ToRgbColor() ); }