Exemplo n.º 1
0
        private ColorCutQuantizer(ColorHistogram colorHistogram, int maxColors)
        {
            if (colorHistogram == null)
            {
                throw new ArgumentNullException("colorHistogram can not be null");
            }
            if (maxColors < 1)
            {
                throw new ArgumentException("maxColors must be 1 or greater");
            }

            int rawColorCount = colorHistogram.getNumberOfColors();

            Color[] rawColors      = colorHistogram.getColors();
            int[]   rawColorCounts = colorHistogram.getColorCounts();

            // First, lets pack the populations into a SparseIntArray so that they can be easily
            // retrieved without knowing a color's index
            mColorPopulations = new List <KeyValuePair <Color, Int32> >(rawColorCount);
            for (int i = 0; i < rawColors.Length; i++)
            {
                mColorPopulations.Add(new KeyValuePair <Color, Int32>(rawColors[i], rawColorCounts[i]));
            }

            // Now go through all of the colors and keep those which we do not want to ignore
            mColors = new Color[rawColorCount];
            int validColorCount = 0;

            foreach (Color color in rawColors)
            {
                if (!shouldIgnoreColor(color))
                {
                    mColors[validColorCount++] = color;
                }
            }
            if (validColorCount <= maxColors)
            {
                // The image has fewer colors than the maximum requested, so just return the colors
                mQuantizedColors = new List <Swatch>();
                foreach (Color color in mColors)
                {
                    try
                    {
                        mQuantizedColors.Add(new Swatch(color, mColorPopulations.First(kvp => kvp.Key == color).Value));
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else
            {
                // We need use quantization to reduce the number of colors
                mQuantizedColors = quantizePixels(validColorCount - 1, maxColors);
            }
        }
Exemplo n.º 2
0
        private ColorCutQuantizer(ColorHistogram colorHistogram, int maxColors)
        {
            if (colorHistogram == null)
            {
                throw new ArgumentNullException("colorHistogram can not be null");
            }
            if (maxColors < 1)
            {
                throw new ArgumentException("maxColors must be 1 or greater");
            }

            int rawColorCount = colorHistogram.getNumberOfColors();
            Color[] rawColors = colorHistogram.getColors();
            int[] rawColorCounts = colorHistogram.getColorCounts();

            // First, lets pack the populations into a SparseIntArray so that they can be easily
            // retrieved without knowing a color's index
            mColorPopulations = new List<KeyValuePair<Color, Int32>>(rawColorCount);
            for (int i = 0; i < rawColors.Length; i++)
            {
                mColorPopulations.Add(new KeyValuePair<Color, Int32>(rawColors[i], rawColorCounts[i]));
            }

            // Now go through all of the colors and keep those which we do not want to ignore
            mColors = new Color[rawColorCount];
            int validColorCount = 0;

            foreach (Color color in rawColors)
            {
                if (!shouldIgnoreColor(color))
                {
                    mColors[validColorCount++] = color;
                }
            }
            if (validColorCount <= maxColors)
            {
                // The image has fewer colors than the maximum requested, so just return the colors
                mQuantizedColors = new List<Swatch>();
                foreach (Color color in mColors)
                {
                    try
                    {
                        mQuantizedColors.Add(new Swatch(color, mColorPopulations.First(kvp => kvp.Key == color).Value));
                    }
                    catch (Exception)
                    {
                    }

                }
            }
            else
            {
                // We need use quantization to reduce the number of colors
                mQuantizedColors = quantizePixels(validColorCount - 1, maxColors);
            }
        }