private static double[] GetValuesFromImage_1Pixel(FeatureRecognizer_Image image, SimpleColorScheme scheme) { BitmapSource bitmap = UtilityWPF.ResizeImage(new BitmapImage(new Uri(image.Filename)), 1, 1); byte[][] colors = ((BitmapCustomCachedBytes)UtilityWPF.ConvertToColorArray(bitmap, false, Colors.Black)) .GetColors_Byte(); if (colors.Length != 1) { throw new ApplicationException("Expected exactly one pixel: " + colors.Length.ToString()); } double[] retVal = new double[3]; switch (scheme) { case SimpleColorScheme.RGB: #region RGB retVal[0] = colors[0][1] / 255d; //colors[0][0] is alpha retVal[1] = colors[0][2] / 255d; retVal[2] = colors[0][3] / 255d; #endregion break; case SimpleColorScheme.HSV: #region HSV ColorHSV hsv = UtilityWPF.RGBtoHSV(Color.FromArgb(colors[0][0], colors[0][1], colors[0][2], colors[0][3])); retVal[0] = hsv.H / 360d; retVal[1] = hsv.S / 100d; retVal[2] = hsv.V / 100d; #endregion break; default: throw new ApplicationException("Unknown SimpleColorScheme: " + scheme.ToString()); } return retVal; }
private static Color GetColor(double[] values, SimpleColorScheme scheme) { #region validate #if DEBUG if (values.Length != 3) { throw new ArgumentException("Expected values to be length 3: " + values.Length.ToString()); } #endif #endregion switch (scheme) { case SimpleColorScheme.RGB: double r = values[0] * 255d; double g = values[1] * 255d; double b = values[2] * 255d; if (r < 0) r = 0; if (r > 255) r = 255; if (g < 0) g = 0; if (g > 255) g = 255; if (b < 0) b = 0; if (b > 255) b = 255; return Color.FromRgb(Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)); case SimpleColorScheme.HSV: double h = values[0] * 360d; double s = values[1] * 100d; double v = values[2] * 100d; if (h < 0) h = 0; if (h > 360) h = 360; if (s < 0) s = 0; if (s > 100) s = 100; if (v < 0) v = 0; if (v > 255) v = 255; return new ColorHSV(h, s, v).ToRGB(); default: throw new ApplicationException("Unknown SimpleColorScheme: " + scheme.ToString()); } }