public Color[,] Transform(Color[,] image, INearestColorStrategy nearestColorStrategy) { var result = new Color[image.GetLength(0), image.GetLength(1)]; for (int y = 0; y < image.GetLength(0); y++) { for (int x = 0; x < image.GetLength(1); x++) { result[y, x] = FindMostSuitableAvailibleColor(image[y, x], nearestColorStrategy); } } return(result); }
private Color FindMostSuitableAvailibleColor(Color color, INearestColorStrategy nearestColorStrategy) { if (_cachedColors.TryGetValue(color, out var neighbour)) { return(neighbour); } var distances = _pallet.Select(c => new { Neighbour = c, Distance = nearestColorStrategy.DistanceToNeighbour(color, c) }); var minDistance = distances.Min(x => x.Distance); neighbour = distances.First(x => x.Distance == minDistance).Neighbour; _cachedColors.Add(color, neighbour); return(neighbour); }