private Color CalculateLeastImportantColor(Color[,] image, HashSet<Color> palette)
{
Dictionary<Color, double> colorErrorSquaredDelta = new Dictionary<Color, double>();
foreach (Color c in palette)
colorErrorSquaredDelta.Add(c, 0);
for (int i = 0; i < image.GetLength(0); i++)
for (int j = 0; j < image.GetLength(1); j++)
{
Color nearestColor = GetNearestColor(palette, image[i, j]);
double currentError = Color.DistanceSquared(image[i, j], nearestColor);
double potentialError = Color.DistanceSquared(image[i, j], GetSecondNearestColor(palette, image[i, j]));
colorErrorSquaredDelta[nearestColor] += (potentialError - currentError);
}
return colorErrorSquaredDelta.Aggregate((a, b) => (a.Value < b.Value) ? a : b).Key;
}