// Gets the palette with specified count of the colors.
        public List <Color> SetUpColorPalette(int colorCount)
        {
            // creates the initial cube covering all the pixels in the image
            var initalMedianCutCube = new MedianCutCube(_colorList);

            _cubeList.Add(initalMedianCutCube);

            // finds the minimum iterations needed to achieve the cube count (color count) we need
            int iterationCount = 1;

            while ((1 << iterationCount) < colorCount)
            {
                iterationCount++;
            }

            for (var iteration = 0; iteration < iterationCount; iteration++)
            {
                SplitCubes(colorCount);
            }
            var result = new List <Color>();

            // adds all the cubes' colors to the palette
            foreach (var cube in _cubeList)
            {
                result.Add(cube.Color);
            }

            return(result);
        }
示例#2
0
        /// <summary>
        /// Splits the cube into 2 smaller cubes using the specified color side for splitting.
        /// </summary>
        /// <param name="componentIndex"></param>
        /// <param name="medianCube1"></param>
        /// <param name="medianCube2"></param>
        public void SplitAtMedian(byte componentIndex, out MedianCutCube medianCube1, out MedianCutCube medianCube2)
        {
            switch (componentIndex)
            {
            case 0:
                _colorList.Sort((p, n) => p.R.CompareTo(n.R));
                break;

            case 1:
                _colorList.Sort((p, n) => p.R.CompareTo(n.R));
                break;

            case 2:
                _colorList.Sort((p, n) => p.R.CompareTo(n.R));
                break;
            }

            var medianIndex = _colorList.Count >> 1;

            medianCube1 = new MedianCutCube(_colorList.GetRange(0, medianIndex));
            medianCube2 = new MedianCutCube(_colorList.GetRange(medianIndex, _colorList.Count - medianIndex));
        }