//Das hier alles testen und weitermachen 26.10.2017 /// <summary> /// Calculate the distance to palette 0..4 /// </summary> /// <param name="item">color of the original palette element of sortedList</param> /// <param name="palBitmap">Bitmap from Palette 0..4</param> public static void CalcDistances(int calcMode, ref OriginalColor item, params Bitmap[] palBitmap) { // distance to palette 0..4 double[] distance = new double[palBitmap.Length]; // Minium distance to palette 0..4 double[] minDistance = new double[palBitmap.Length]; // Initialize Minimum with max. from Double for (int i = 0; i < minDistance.Length; i++) { minDistance[i] = Double.MaxValue; } //Lab-color from item double[] lab0 = new double[3]; lab0 = ColorSpace.RGB2Lab(item.Pix); //Lab-color from palette double[] lab1 = new double[3]; //HSV-color from item double[] hsv0 = new double[3]; hsv0 = ColorSpace.RGB2HSV(item.Pix); //Lab-color from palette double[] hsv1 = new double[3]; // j = Palette Bitmap 0 ..4 for (int j = 0; j < palBitmap.Length; j++) { //Calculate the min distance to palette 0..4 if (palBitmap[j] != null) { for (int x = 0; x < palBitmap[j].Width; x++) { for (int y = 0; y < palBitmap[j].Height; y++) { lab1 = ColorSpace.RGB2Lab(palBitmap[j].GetPixel(x, y)); hsv1 = ColorSpace.RGB2HSV(palBitmap[j].GetPixel(x, y)); if (calcMode == 0) // Color-distance { distance[j] = ColorSpace.ColorDistance2(lab0, lab1); } if (calcMode == 1) // HSV-Color H (Farbwert) { distance[j] = Math.Abs(hsv0[0] - hsv1[0]); } if (calcMode == 2) // HSV-Color S (Sätigung) { distance[j] = Math.Abs(hsv0[1] - hsv1[1]); } if (calcMode == 3) // HSV-Color V (Hellwert) { distance[j] = Math.Abs(hsv0[2] - hsv1[2]); } if (distance[j] < minDistance[j]) { minDistance[j] = distance[j]; } } } switch (j) { case 0: { item.DistanceTo0 = minDistance[j]; // Min Distance to palette 0 break; } case 1: { item.DistanceTo1 = minDistance[j]; // Min Distance to palette 1 break; } case 2: { item.DistanceTo2 = minDistance[j]; // Min Distance to palette 2 break; } case 3: { item.DistanceTo3 = minDistance[j]; // Min Distance to palette 3 break; } case 4: { item.DistanceTo4 = minDistance[j]; // Min Distance to palette 4 break; } default: // Error no more palettes break; } } } }
public OriginalColor(int calcMode, Color pix, Bitmap[] bitmapPalette) { this.calcMode = calcMode; Pix = pix; this.bitmapPalette = bitmapPalette; int jMin = -1; // distance to palette 0..4 double[] distance = new double[4]; // Minium distance to palette 0..4 double[] minDistance = new double[4]; //Lab-color from item double[] lab0 = new double[3]; lab0 = ColorSpace.RGB2Lab(pix); //Lab-color from palette double[] lab1 = new double[3]; //HSV-color from item double[] hsv0 = new double[3]; hsv0 = ColorSpace.RGB2HSV(pix); //Lab-color from palette double[] hsv1 = new double[3]; // j = Palette Bitmap 0 ..4 for (int j = 0; j < bitmapPalette.Length; j++) { //Calculate the min distance to palette 0..4 this.distanceMinIndex = -1; this.DistanceMin = Double.MaxValue; this.DistanceTo0 = Double.MaxValue; this.DistanceTo1 = Double.MaxValue; this.DistanceTo2 = Double.MaxValue; this.DistanceTo3 = Double.MaxValue; this.DistanceTo4 = Double.MaxValue; if (bitmapPalette[j] != null) { minDistance[j] = Double.MaxValue; for (int x = 0; x < bitmapPalette[j].Width; x++) { for (int y = 0; y < bitmapPalette[j].Height; y++) { lab1 = ColorSpace.RGB2Lab(bitmapPalette[j].GetPixel(x, y)); hsv1 = ColorSpace.RGB2HSV(bitmapPalette[j].GetPixel(x, y)); if (calcMode == 0) // Color-distance { distance[j] = ColorSpace.ColorDistance2(lab0, lab1); } if (calcMode == 1) // HSV-Color H (Farbwert) { distance[j] = Math.Abs(hsv0[0] - hsv1[0]); } if (calcMode == 2) // HSV-Color S (Sätigung) { distance[j] = Math.Abs(hsv0[1] - hsv1[1]); } if (calcMode == 3) // HSV-Color V (Hellwert) { distance[j] = Math.Abs(hsv0[2] - hsv1[2]); } if (distance[j] < minDistance[j]) { minDistance[j] = distance[j]; jMin = j; if (j == 0) { this.DistanceTo0 = minDistance[j]; } if (j == 1) { this.DistanceTo1 = minDistance[j]; } if (j == 2) { this.DistanceTo2 = minDistance[j]; } if (j == 3) { this.DistanceTo3 = minDistance[j]; } if (j == 4) { this.DistanceTo4 = minDistance[j]; } } } } } } }
private void PreviewLinear() { bool saveTemporaryBmp; bool saveTemporaryJpeg; bool error = PaletteWindowToPaletteGradient(); if (!error) { //string for temporary file string result = ""; // temporary path string result_bmp = ""; // full name temporary bmp string result_jpeg = ""; // full name temporary jpeg // Label text start and End Color string start; string end; start = "Startfarbe: " + PalGra.StartColor.ToString(); end = "Endfarbe: " + PalGra.EndColor.ToString(); LblStartColor.Content = start; LblEndColor.Content = end; // Write the pixels in the temp Bitmap BitmapPalette = new System.Drawing.Bitmap(PalGra.PixelCounter, 1); // first pixel PalGra.RGB_StartColor = new System.Drawing.Color(); PalGra.RGB_StartColor = System.Drawing.Color.FromArgb( PalGra.StartColor.A, PalGra.StartColor.R, PalGra.StartColor.G, PalGra.StartColor.B); BitmapPalette.SetPixel(0, 0, PalGra.RGB_StartColor); // last pixel PalGra.RGB_EndColor = new System.Drawing.Color(); PalGra.RGB_EndColor = System.Drawing.Color.FromArgb( PalGra.EndColor.A, PalGra.EndColor.R, PalGra.EndColor.G, PalGra.EndColor.B); BitmapPalette.SetPixel(PalGra.PixelCounter - 1, 0, PalGra.RGB_EndColor); //gradient pixels if (PalGra.PixelCounter > 2) { PalGra.LabStartColor = ColorSpace.RGB2Lab(PalGra.RGB_StartColor); PalGra.LabEndColor = ColorSpace.RGB2Lab(PalGra.RGB_EndColor); for (int i = 0; i < 3; i++) { PalGra.LabStepColor[i] = (PalGra.LabEndColor[i] - PalGra.LabStartColor[i]) / (Convert.ToDouble(PalGra.PixelCounter) - 1.0); PalGra.LabNewColor[i] = PalGra.LabStartColor[i] + PalGra.LabStepColor[i]; } for (int i = 1; i < PalGra.PixelCounter - 1; i++) { PalGra.RGB_NewColor = ColorSpace.Lab2RGB(PalGra.LabNewColor); BitmapPalette.SetPixel(i, 0, PalGra.RGB_NewColor); for (int j = 0; j < 3; j++) { PalGra.LabNewColor[j] = PalGra.LabNewColor[j] + PalGra.LabStepColor[j]; } } } BitmapPaletteCopy = new Bitmap(BitmapPalette); //Save Bitmap in TempPath result = System.IO.Path.GetTempPath(); saveTemporaryBmp = false; for (int i = 1; !saveTemporaryBmp && i < 10; i++) { result_bmp = result + i.ToString() + "Palette.bmp"; try { BitmapPalette.Save(result_bmp, System.Drawing.Imaging.ImageFormat.Bmp); saveTemporaryBmp = true; } catch (Exception ex) { if (i > 8) { System.Windows.MessageBox.Show(i.ToString() + " Save *.Bmp in temporary path: " + ex.Message); } } } PalGra.PathTempBitmap = result_bmp; //Save Bitmap in TempPath as jpeg saveTemporaryJpeg = false; for (int i = 1; !saveTemporaryJpeg && i < 10; i++) { result_jpeg = result + i.ToString() + "Palette.jpg"; try { BitmapPaletteCopy.Save(result_jpeg, System.Drawing.Imaging.ImageFormat.Jpeg); saveTemporaryJpeg = true; } catch (Exception ex) { if (i > 8) { System.Windows.MessageBox.Show(i.ToString() + " Save *.jpg in temporary path: " + ex.Message); } } } PalGra.PathTempBitmapImage = result_jpeg; // Show Bitmap if (File.Exists(PalGra.PathTempBitmapImage)) { ImageColor.BeginInit(); ImageColor.Source = new BitmapImage(new Uri(PalGra.PathTempBitmapImage)); ImageColor.EndInit(); this.Title = "Palette: " + PalGra.PathTempBitmapImage; } } }
public ImitationColor(int calcMode, double[] correction, Color pix, Bitmap[] palBitmap) { //default settings PaletteNewColor = Color.FromArgb(0, 222, 222, 222); TargetIndex = -1; PaletteListIndex = -1; OriginalIndex = -1; OriginalListIndex = -1; int jMin = -1; Pix = pix; // distance to palette 0..4 double distance = Double.MaxValue; // Minium distance to palette 0..4 double minDistance = Double.MaxValue; //Lab-color from item double[] lab0 = new double[3]; lab0 = ColorSpace.RGB2Lab(pix); //Lab-color from palette double[] lab1 = new double[3]; //HSV-color from item double[] hsv0 = new double[3]; hsv0 = ColorSpace.RGB2HSV(pix); //Lab-color from palette double[] hsv1 = new double[3]; // j = Palette Bitmap 0 ..4 for (int j = 0; j < palBitmap.Length; j++) { //Calculate the min distance to palette 0..4 if (palBitmap[j] != null) { for (int x = 0; x < palBitmap[j].Width; x++) { for (int y = 0; y < palBitmap[j].Height; y++) { lab1 = ColorSpace.RGB2Lab(palBitmap[j].GetPixel(x, y)); hsv1 = ColorSpace.RGB2HSV(palBitmap[j].GetPixel(x, y)); if (calcMode == 0) // Color-distance { distance = ColorSpace.ColorDistance2(lab0, lab1) - correction[j]; } if (calcMode == 1) // HSV-Color H (Farbwert) { distance = Math.Abs(hsv0[0] - hsv1[0]) - correction[j]; } if (calcMode == 2) // HSV-Color S (Sätigung) { distance = Math.Abs(hsv0[1] - hsv1[1]) - correction[j]; } if (calcMode == 3) // HSV-Color V (Hellwert) { distance = Math.Abs(hsv0[2] - hsv1[2]) - correction[j]; } if (distance < minDistance) { minDistance = distance; jMin = j; } } } } } this.PaletteIndex = jMin; }
private void Preview3D() { bool saveTemporaryBmp; bool saveTemporaryJpeg; // corditates from bitmap for setpixel int x = 0; int y = 0; // Color Palette sorted by distance to 0..4 this.hsvColors = new Dictionary <string, HSV_Color>(); //the new calculate hsv-Color HSV_Color newHsvColor = new HSV_Color(); //the gradients (3d) hsv-Color double[] partHsvColor = new double[3]; // step hsv bool error = PaletteWindowToPaletteGradient(); ; if (!error) { //string for temporary file string result = ""; // temporary path string result_bmp = ""; // full name temporary bmp string result_jpeg = ""; // full name temporary jpeg // Label text start and End Color string start; string end; start = "Startfarbe: " + PalGra.StartColor.ToString(); end = "Endfarbe: " + PalGra.EndColor.ToString(); LblStartColor.Content = start; LblEndColor.Content = end; // Calculate the HSV start color Color drawingcolorStart = System.Drawing.Color.FromArgb( PalGra.StartColor.A, PalGra.StartColor.R, PalGra.StartColor.G, PalGra.StartColor.B); PalGra.HsvStartColor = ColorSpace.RGB2HSV(drawingcolorStart); // Calculate the HSV end color Color drawingcolorEnd = System.Drawing.Color.FromArgb( PalGra.EndColor.A, PalGra.EndColor.R, PalGra.EndColor.G, PalGra.EndColor.B); PalGra.HsvEndColor = ColorSpace.RGB2HSV(drawingcolorEnd); // Label HSV Start colors LblStartFarbwert.Content = "Farbwert HSV " + PalGra.HsvStartColor[0].ToString(); LblStartHellwert.Content = "Hellwert HSV " + PalGra.HsvStartColor[1].ToString(); LblStartSaetigung.Content = "Saetigung HSV " + PalGra.HsvStartColor[2].ToString(); // Label HSV End colors LblEndFarbwert.Content = "Farbwert HSV " + PalGra.HsvEndColor[0].ToString(); LblEndHellwert.Content = "Hellwert HSV " + PalGra.HsvEndColor[1].ToString(); LblEndSaetigung.Content = "Saetigung HSV " + PalGra.HsvEndColor[2].ToString(); // Define the width and height from the bitmap if (PalGra.HsvCounter1 >= PalGra.HsvCounter0 * PalGra.HsvCounter2) { BitmapPalette = new System.Drawing.Bitmap(PalGra.HsvCounter0 * PalGra.HsvCounter2, PalGra.HsvCounter1); } else if (PalGra.HsvCounter2 >= PalGra.HsvCounter0 * PalGra.HsvCounter1) { BitmapPalette = new System.Drawing.Bitmap(PalGra.HsvCounter0 * PalGra.HsvCounter1, PalGra.HsvCounter2); } else { BitmapPalette = new System.Drawing.Bitmap(PalGra.HsvCounter1 * PalGra.HsvCounter2, PalGra.HsvCounter0); } PalGra.RGB_StartColor = new Color(); PalGra.RGB_StartColor = System.Drawing.Color.FromArgb( PalGra.StartColor.A, PalGra.StartColor.R, PalGra.StartColor.G, PalGra.StartColor.B); BitmapPalette.SetPixel(0, 0, PalGra.RGB_StartColor); // last pixel PalGra.RGB_EndColor = new System.Drawing.Color(); PalGra.RGB_EndColor = System.Drawing.Color.FromArgb( PalGra.EndColor.A, PalGra.EndColor.R, PalGra.EndColor.G, PalGra.EndColor.B); BitmapPalette.SetPixel(BitmapPalette.Width - 1, BitmapPalette.Height - 1, PalGra.RGB_EndColor); partHsvColor[0] = (PalGra.HsvEndColor[0] - PalGra.HsvStartColor[0]) / Convert.ToDouble(PalGra.HsvCounter0); partHsvColor[1] = (PalGra.HsvEndColor[1] - PalGra.HsvStartColor[1]) / Convert.ToDouble(PalGra.HsvCounter1); partHsvColor[2] = (PalGra.HsvEndColor[2] - PalGra.HsvStartColor[2]) / Convert.ToDouble(PalGra.HsvCounter2); ; //gradient pixels newHsvColor.HsvColor[0] = PalGra.HsvStartColor[0]; ; do { newHsvColor.HsvColor[1] = PalGra.HsvStartColor[1]; ; do { newHsvColor.HsvColor[2] = PalGra.HsvStartColor[2]; ; do { // the new color from hsv newHsvColor.Pix = ColorSpace.HSV2RGB(newHsvColor.HsvColor); // setpixel in the bitmap BitmapPalette.SetPixel(x, y, newHsvColor.Pix); ; // define x and y for the next pixel if (y < BitmapPalette.Height - 1) { y = Math.Min(++y, BitmapPalette.Height - 1); } else if (x < BitmapPalette.Width - 1) { y = 0; x = Math.Min(++x, BitmapPalette.Width - 1); } newHsvColor.HsvColor[2] += partHsvColor[2]; }while ((Math.Abs(newHsvColor.HsvColor[2] - PalGra.HsvEndColor[2]) >= 0.0001 && (PalGra.HsvEndColor[2] != PalGra.HsvStartColor[2]))); newHsvColor.HsvColor[1] += partHsvColor[1]; }while ((Math.Abs(newHsvColor.HsvColor[1] - PalGra.HsvEndColor[1]) >= 0.0001 && (PalGra.HsvEndColor[1] != PalGra.HsvStartColor[1]))); newHsvColor.HsvColor[0] += partHsvColor[0]; }while ((Math.Abs(newHsvColor.HsvColor[0] - PalGra.HsvEndColor[0]) >= 0.0001 && (PalGra.HsvEndColor[0] != PalGra.HsvStartColor[0]))); BitmapPaletteCopy = new Bitmap(BitmapPalette); //Save Bitmap in TempPath result = System.IO.Path.GetTempPath(); saveTemporaryBmp = false; for (int i = 1; !saveTemporaryBmp && i < 10; i++) { result_bmp = result + i.ToString() + "Palette.bmp"; try { BitmapPalette.Save(result_bmp, System.Drawing.Imaging.ImageFormat.Bmp); saveTemporaryBmp = true; } catch (Exception ex) { if (i > 8) { System.Windows.MessageBox.Show(i.ToString() + " Save *.Bmp in temporary path: " + ex.Message); } } } PalGra.PathTempBitmap = result_bmp; //Save Bitmap in TempPath as jpeg saveTemporaryJpeg = false; for (int i = 1; !saveTemporaryJpeg && i < 10; i++) { result_jpeg = result + i.ToString() + "Palette.jpg"; try { BitmapPaletteCopy.Save(result_jpeg, System.Drawing.Imaging.ImageFormat.Jpeg); saveTemporaryJpeg = true; } catch (Exception ex) { if (i > 8) { System.Windows.MessageBox.Show(i.ToString() + " Save *.jpg in temporary path: " + ex.Message); } } } PalGra.PathTempBitmapImage = result_jpeg; // Show Bitmap if (File.Exists(PalGra.PathTempBitmapImage)) { ImageColor.BeginInit(); ImageColor.Source = new BitmapImage(new Uri(PalGra.PathTempBitmapImage)); ImageColor.EndInit(); this.Title = "Palette: " + PalGra.PathTempBitmapImage; } } }