/// <summary> /// Method to find a color that is most /// </summary> /// <param name="colors">An array of color values</param> /// <param name="color">A color</param> /// <returns>The color that matches <paramref name="color"/> most.</returns> public static System.Drawing.Color FindNearestColor( this IList <System.Drawing.Color> colors, System.Drawing.Color color) { var hlsColor = (HlsColor)color; var distance = double.MaxValue; var resColor = new HlsColor(); if (colors == null || colors.Count == 0) { throw new ArgumentException("colors"); } foreach (var c in colors) { var testHlsColor = (HlsColor)c; var d = hlsColor.Distance(testHlsColor); if (d < distance) { distance = d; if (distance == 0d) { return(testHlsColor); } resColor = testHlsColor; } } return(resColor); }
static ColorExtensions() { var p = new List <string>(); for (int i = 0; i <= 255; i += 15) { string sc = "#" + string.Format("{0:X2}{1:X2}{2:X2}", i, i, i); p.Add(sc); } p.Add("#FF0000"); p.Add("#00FF00"); p.Add("#0000FF"); p.Add("#FFFF00"); p.Add("#00FFFF"); p.Add("#FF00FF"); p.Add("#C0C0C0"); for (var h = 0f; h < 360f; h += 10f) { for (var l = 0.25f; l <= 0.75; l += 1f / 16f) { System.Drawing.Color c = new HlsColor { H = h, L = l, S = 1f }; var sc = "#" + string.Format("{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B); p.Add(sc); } } TomPaletteBase = p.ToArray(); }
/// <summary> /// Computes the weighted euclidian distance to <paramref name="other"/> color /// </summary> /// <param name="other">The other color</param> /// <returns>The distance</returns> public double Distance(HlsColor other) { var h = (H - other.H) * 1d; var l = (L - other.L) * 100d; var s = (S - other.S) * 100d; h = 0.5d * h * h; l *= l; s = 0.5d * s * s; return(Math.Sqrt(h + l + s)); }
private void tomChannel_ValueChange(object sender, EventArgs e) { System.Drawing.Color newColor = System.Drawing.Color.Empty; switch (_colorModel) { case ColorModel.RGB: newColor = System.Drawing.Color.FromArgb( Convert.ToByte(_channel4.Value), Convert.ToByte(_channel1.Value), Convert.ToByte(_channel2.Value), Convert.ToByte(_channel3.Value)); break; case ColorModel.CMYK: var cmykColor = new CmykColor { C = (byte)_channel1.Value, M = (byte)_channel2.Value, Y = (byte)_channel3.Value, K = (byte)_channel4.Value }; newColor = System.Drawing.Color.FromArgb(Convert.ToByte(_channel5.Value), cmykColor); break; case ColorModel.HSL: var hlsColor = new HlsColor { H = _channel1.Value, L = _channel2.Value, S = _channel3.Value }; var tmpColor = new HlsColor { H = hlsColor.H, L = 0.5f, S = 1f }; _channel2.Color1 = tmpColor; _channel3.Color1 = tmpColor; newColor = System.Drawing.Color.FromArgb(Convert.ToByte(_channel4.Value), hlsColor); break; } SelectedColor = newColor; }