public RGBColor apply(RGBColor color) { for (int i = buckets.Length - 1; i >= 0; i--) { var bucket = buckets[i]; if (bucket.startEucDistance <= color.eucDistance) { return(bucket.ClothestColor(color)); } } throw new Exception("WTF exception: you've performed some bullshit don't do so in the future"); }
public RGBColor ClothestColor(RGBColor col) { var result = this.bucketColors[0]; var minDist = result.distanceBetween(col); foreach (var color in bucketColors) { var dst = color.distanceBetween(col); if (dst < minDist) { result = color; minDist = dst; } } return(result); }
public ClothestColorFinder(IEnumerable <RGBColor> colors, int bucketsCount) { buckets = new ColorBucket[bucketsCount]; var sorted = colors.OrderBy(c => c.eucDistance).ToArray(); var colCount = colors.Count(); var bucketSize = colCount / bucketsCount; var lastBucketSize = bucketSize + colCount % bucketsCount; for (int i = 0; i < bucketsCount; i++) { var bucketColors = new RGBColor[i == bucketsCount - 1 ? lastBucketSize : bucketSize]; var startColorIndex = i * bucketSize; var endColorIndex = startColorIndex + bucketColors.Length; int counter = 0; for (int j = startColorIndex; j < endColorIndex; j++) { bucketColors[counter++] = sorted[j]; } buckets[i] = new ColorBucket(bucketColors, i == 0 ? 0 : buckets[i - 1].endEucDistance); } }
public double distanceBetween(RGBColor other) { return(Math.Sqrt(Math.Pow(r - other.r, 2) + Math.Pow(g - other.g, 2) + Math.Pow(b - other.b, 2))); }