Пример #1
0
        /// ============================================================
        /// <summary>生成指定颜色数目的调色板。</summary>
        /// <param name="count">颜色数目</param>
        /// <returns>颜色列表</returns>
        /// ============================================================
        public List <Color> MakePalette(Int32 count)
        {
            FMedianCutCube initalMedianCutCube = new FMedianCutCube(_colorList);

            _cubeList.Add(initalMedianCutCube);
            Int32 iterationCount = 1;

            while ((1 << iterationCount) < count)
            {
                iterationCount++;
            }
            for (Int32 iteration = 0; iteration < iterationCount; iteration++)
            {
                SplitCubes(count);
            }
            List <Color> result       = new List <Color>();
            Int32        paletteIndex = 0;

            foreach (FMedianCutCube cube in _cubeList)
            {
                result.Add(cube.Color);
                cube.SetPaletteIndex(paletteIndex++);
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// Splits this cube's color list at median index, and returns two newly created cubes.
        /// </summary>
        /// <param name="componentIndex">Index of the component (red = 0, green = 1, blue = 2).</param>
        /// <param name="firstMedianCutCube">The first created cube.</param>
        /// <param name="secondMedianCutCube">The second created cube.</param>
        public void SplitAtMedian(Byte componentIndex, out FMedianCutCube firstMedianCutCube, out FMedianCutCube secondMedianCutCube)
        {
            List <Color> colors;

            switch (componentIndex)
            {
            // red colors
            case 0:
                colors = colorList.OrderBy(color => color.R).ToList();
                break;

            // green colors
            case 1:
                colors = colorList.OrderBy(color => color.G).ToList();
                break;

            // blue colors
            case 2:
                colors = colorList.OrderBy(color => color.B).ToList();
                break;

            default:
                throw new NotSupportedException("Only three color components are supported (R, G and B).");
            }

            // retrieves the median index (a half point)
            Int32 medianIndex = colorList.Count >> 1;

            // creates the two half-cubes
            firstMedianCutCube  = new FMedianCutCube(colors.GetRange(0, medianIndex));
            secondMedianCutCube = new FMedianCutCube(colors.GetRange(medianIndex, colors.Count - medianIndex));
        }