Пример #1
0
        private void GetPalette(WriteableBitmap bmp)
        {
            if (bmp.Pixels.Length == 0)
            {
                return;
            }

            // limit white and black
            // fixed by myself
            const double limit = 0.265;


            Dictionary <string, int> occulot = new Dictionary <string, int>();

            var s2 = Stopwatch.StartNew();

            foreach (int t in bmp.Pixels)
            {
                Color             clr = ColorConverter.FromInt(t);
                HSVHelper.HSVData hsv = HSVHelper.ConvertColorToHSV(clr);

                if (hsv.v > limit && hsv.s > limit)
                {
                    var accentColor = GetAccentColor(clr).ToString();
                    if (occulot.ContainsKey(accentColor))
                    {
                        occulot[accentColor]++;
                    }
                    else
                    {
                        occulot.Add(accentColor, 1);
                    }
                }
            }
            s2.Stop();

            Debug.WriteLine(((double)(s2.Elapsed.TotalSeconds)).ToString("0.00 s"));

            var items = from pair in occulot orderby pair.Value descending select pair;

            List <ColorItem> l = items.Select(item => new ColorItem {
                Background = item.Key.ToString()
            }).ToList();

            //ColorsList.ItemsSource = l;
        }
Пример #2
0
        // http://stackoverflow.com/questions/1068373/how-to-calculate-the-average-rgb-color-values-of-a-bitmap
        #region fast code cant use

        /*private Color CalculateAverageColor(WriteableBitmap bm)
         * {
         *  int width = bm.PixelWidth;
         *  int height = bm.PixelHeight;
         *  int red = 0;
         *  int green = 0;
         *  int blue = 0;
         *  int minDiversion = 15; // drop pixels that do not differ by at least minDiversion between color values (white, gray or black)
         *  int dropped = 0; // keep track of dropped pixels
         *  long[] totals = new long[] { 0, 0, 0 };
         *  int bppModifier = bm.PixelFormat == System.Windows.Media.Imaging.PixelFormat.Format24bppRgb ? 3 : 4; // cutting corners, will fail on anything else but 32 and 24 bit images
         *
         *  WriteableBitmap srcData = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat);
         *  int stride = srcData.Stride;
         *  IntPtr Scan0 = srcData.Scan0;
         *
         *  unsafe
         *  {
         *      byte* p = (byte*)(void*)Scan0;
         *
         *      for (int y = 0; y < height; y++)
         *      {
         *          for (int x = 0; x < width; x++)
         *          {
         *              int idx = (y * stride) + x * bppModifier;
         *              red = p[idx + 2];
         *              green = p[idx + 1];
         *              blue = p[idx];
         *              if (Math.Abs(red - green) > minDiversion || Math.Abs(red - blue) > minDiversion || Math.Abs(green - blue) > minDiversion)
         *              {
         *                  totals[2] += red;
         *                  totals[1] += green;
         *                  totals[0] += blue;
         *              }
         *              else
         *              {
         *                  dropped++;
         *              }
         *          }
         *      }
         *  }
         *
         *  int count = width * height - dropped;
         *  int avgR = (int)(totals[2] / count);
         *  int avgG = (int)(totals[1] / count);
         *  int avgB = (int)(totals[0] / count);
         *
         *  return Color.FromArgb((byte)255, (byte)avgR, (byte)avgG, (byte)avgB);
         * }*/
        #endregion

        #region algos
        private static Color RunOldAlgo(WriteableBitmap bmp)
        {
            if (bmp.Pixels.Length == 0)
            {
                return(Colors.White);
            }

            // limit white and black
            // fixed by myself
            const double limit = 0.265;

            //Used for tally
            int r = 0;
            int g = 0;
            int b = 0;

            int tot = 0;

            var s1 = Stopwatch.StartNew();

            for (int x = 0; x < bmp.PixelWidth; x++)
            {
                for (int y = 0; y < bmp.PixelHeight; y++)
                {
                    Color             clr = bmp.GetPixel(x, y);
                    HSVHelper.HSVData hsv = HSVHelper.ConvertColorToHSV(clr);

                    if (hsv.v > limit && hsv.s > limit)
                    {
                        r += clr.R;
                        g += clr.G;
                        b += clr.B;
                        tot++;
                    }
                }
            }
            s1.Stop();
            Debug.WriteLine((s1.Elapsed.TotalSeconds).ToString("0.00 s"));

            if (tot == 0)
            {
                tot = 1;
            }

            return(Color.FromArgb((byte)255, (byte)(r / tot), (byte)(g / tot), (byte)(b / tot)));
        }
Пример #3
0
        private static Color RunNewAlgo(WriteableBitmap bmp)
        {
            if (bmp.Pixels.Length == 0)
            {
                return(Colors.White);
            }

            // limit white and black
            // fixed by myself
            const double limit = 0.265;

            //Used for tally
            int r = 0;
            int g = 0;
            int b = 0;

            int tot = 0;

            var s2 = Stopwatch.StartNew();

            for (int i = 0; i < bmp.Pixels.Length; i++)
            {
                Color             clr = ColorConverter.FromInt(bmp.Pixels[i]);
                HSVHelper.HSVData hsv = HSVHelper.ConvertColorToHSV(clr);

                if (hsv.v > limit && hsv.s > limit)
                {
                    r += clr.R;
                    g += clr.G;
                    b += clr.B;
                    tot++;
                }
            }
            s2.Stop();

            Debug.WriteLine(((double)(s2.Elapsed.TotalSeconds)).ToString("0.00 s"));

            // be sure to not divide by 0
            if (tot == 0)
            {
                tot = 1;
            }

            return(Color.FromArgb((byte)255, (byte)(r / tot), (byte)(g / tot), (byte)(b / tot)));
        }
Пример #4
0
        private static Color FindNearestColor(Color current, List <Color> colors)
        {
            HSVHelper.HSVData hsv  = HSVHelper.ConvertColorToHSV(current);
            double            diff = 360;

            Color nearestColor = new Color();

            foreach (var color in colors)
            {
                var    tmp = color;
                double abs = Math.Abs(HSVHelper.ConvertColorToHSV(tmp).h - hsv.h);
                if (diff > abs)
                {
                    nearestColor = tmp;
                    diff         = abs;
                }
            }

            return(nearestColor);
        }