public double GetLABDist(ColorCount other) { return array.GetEDeltaColorDifference(other.array); }
private Color GetMostCommonColor(double tolerance, double bkgTol) { List<ColorCount> colors = new List<ColorCount>(); ColorCount background = new ColorCount() { R = 255, G = 255, B = 255 }; BitmapData bmData = Image.LockBits(new Rectangle(0, 0, Image.Width, Image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride = bmData.Stride; IntPtr Scan0 = bmData.Scan0; bool[,] alreadyFilled = new bool[Image.Width, Image.Height]; unsafe { byte* p = (byte*)(void*)Scan0; int nOffset = stride - Image.Width * 3; for (int y = 0; y < Image.Height; ++y) { for (int x = 0; x < Image.Width; ++x) { ColorCount temp = new ColorCount() { R = p[2], G = p[1], B = p[0] }; if (background.GetLABDist(temp) > bkgTol) { int bestindex = -1; double best = Double.MaxValue; for (int i = 0; i < colors.Count; i++) { double test = colors[i].GetLABDist(temp); if (test < best && test < tolerance) { best = test; bestindex = i; } } if (bestindex != -1 && colors.Count > 0) { colors[bestindex].FoundOne(); } else { colors.Add(temp); } } p += 3; } p += nOffset; } } Image.UnlockBits(bmData); colors.Sort((c, n) => -c.Count.CompareTo(n.Count)); return colors.FirstOrDefault().Color; }