private static Difference[,] CalculateDistances(Color[][] palettes) { // Combination of each palette with each palette except with itself (diagonal) Difference[,] distances = new Difference[palettes.Length, palettes.Length]; // Convert palettes to labcolor space to compute difference LabColor[][] labPalettes = new LabColor[palettes.Length][]; for (int i = 0; i < palettes.Length; i++) { labPalettes[i] = ColorConversion.ToLabPalette <Color>(palettes[i]); } // Compute every possible difference for (int i = 0; i < palettes.Length; i++) { for (int j = 0; j < palettes.Length; j++) { if (i == j) { continue; } distances[i, j] = new Difference(); distances[i, j].SrcPalette = i; distances[i, j].DstPalette = j; distances[i, j].Distance = PaletteDistance.CalculateDistance( labPalettes[i], labPalettes[j]); } } return(distances); }
protected override Pixel QuantizatePixel(int x, int y) { // Get nearest color from palette int colorIndex = nearestNeighbour.Search(labImg[y, x]); // Apply dithering algorithm for each channel LabColor oldPixel = labImg[y, x]; LabColor newPixel = labPalette[colorIndex]; this.Dithering.ApplyDithering(labImg.Data, x, y, 0, oldPixel.X - newPixel.X); this.Dithering.ApplyDithering(labImg.Data, x, y, 1, oldPixel.Y - newPixel.Y); this.Dithering.ApplyDithering(labImg.Data, x, y, 2, oldPixel.Z - newPixel.Z); // If it's a transparent color, set the first palette color Color rgbColor = this.rgbImg[y, x]; if (rgbColor.Alpha == 0) { return(new Pixel(0, 0, true)); } else { return(new Pixel((uint)colorIndex, (uint)rgbColor.Alpha, true)); } }
protected override Pixel QuantizatePixel(int x, int y) { // Get the color and add to the list Color color = image[y, x]; int colorIndex; if (listColor.Count < this.MaxColors) { if (!listColor.Contains(color)) { listColor.Add(color); } colorIndex = listColor.IndexOf(color); } else { // Create the labpalette if so if (nearestNeighbour == null) { LabColor[] labPalette = ColorConversion.ToLabPalette <Color>(listColor.ToArray()); nearestNeighbour = new ExhaustivePaletteSearch(); nearestNeighbour.Initialize(labPalette); } LabColor labNoTrans = ColorConversion.ToLabPalette <Color>(new Color[] { color })[0]; colorIndex = nearestNeighbour.Search(labNoTrans); } return(new Pixel((uint)colorIndex, (uint)color.Alpha, true)); }
public static double CalculateDistance(LabColor[] palette1, LabColor[] palette2) { double totalDistance = 0; for (int i = 0; i < palette1.Length; i++) { double minColorDistance = -1; for (int j = 0; j < palette2.Length; j++) { double distance = palette1[i].GetDistanceSquared(palette2[j]); if (minColorDistance == -1 || distance < minColorDistance) minColorDistance = distance; } totalDistance += minColorDistance; } return totalDistance; }
public static double GetDistanceSquared(this LabColor c1, LabColor c2) { return (c2.X - c1.X) * (c2.X - c1.X) + (c2.Y - c1.Y) * (c2.Y - c1.Y) + (c2.Z - c1.Z) * (c2.Z - c1.Z); }
/// <summary> /// Gets the color difference using Delta E CIE76 (http://en.wikipedia.org/wiki/Color_difference) /// </summary> /// <returns>The distance.</returns> /// <param name="c1">C1.</param> /// <param name="c2">C2.</param> public static double GetDistance(this LabColor c1, LabColor c2) { return Math.Sqrt(c1.GetDistanceSquared(c2)); }
public static double GetDistanceSquared(this LabColor c1, LabColor c2) { return((c2.X - c1.X) * (c2.X - c1.X) + (c2.Y - c1.Y) * (c2.Y - c1.Y) + (c2.Z - c1.Z) * (c2.Z - c1.Z)); }
/// <summary> /// Gets the color difference using Delta E CIE76 (http://en.wikipedia.org/wiki/Color_difference) /// </summary> /// <returns>The distance.</returns> /// <param name="c1">C1.</param> /// <param name="c2">C2.</param> public static double GetDistance(this LabColor c1, LabColor c2) { return(Math.Sqrt(c1.GetDistanceSquared(c2))); }
private static Difference[,] CalculateDistances(Color[][] palettes) { // Combination of each palette with each palette except with itself (diagonal) Difference[,] distances = new Difference[palettes.Length, palettes.Length]; // Convert palettes to labcolor space to compute difference LabColor[][] labPalettes = new LabColor[palettes.Length][]; for (int i = 0; i < palettes.Length; i++) labPalettes[i] = ColorConversion.ToLabPalette<Color>(palettes[i]); // Compute every possible difference for (int i = 0; i < palettes.Length; i++) { for (int j = 0; j < palettes.Length; j++) { if (i == j) continue; distances[i, j] = new Difference(); distances[i, j].SrcPalette = i; distances[i, j].DstPalette = j; distances[i, j].Distance = PaletteDistance.CalculateDistance( labPalettes[i], labPalettes[j]); } } return distances; }