private List<RGB> GetPalette(Int32 colorCount) { // creates the initial cube covering all the pixels in the image PaletteCube initalMedianCutCube = new PaletteCube(colorList); cubeList.Add(initalMedianCutCube); // finds the minimum iterations needed to achieve the cube count (color count) we need Int32 iterationCount = 1; while ((1 << iterationCount) < colorCount) { iterationCount++; } for (Int32 iteration = 0; iteration < iterationCount; iteration++) { SplitCubes(colorCount); } // initializes the result palette List<RGB> result = new List<RGB>(); Int32 paletteIndex = 0; // adds all the cubes' colors to the palette, and mark that cube with palette index for later use for (int i = 0, count = cubeList.Count; i < count; i++) { result.Add(cubeList[i].RGB); cubeList[i].SetPaletteIndex(paletteIndex++); } return result; }
public void SplitAtMedian(Byte componentIndex, out PaletteCube firstMedianCutCube, out PaletteCube secondMedianCutCube) { List<Palette.RGB> colors; switch (componentIndex) { // red colors case 0: colors = colorList.OrderBy(color => color.R).ToList(); //colors = colorList.Sort(colorList.); 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 PaletteCube(colors.GetRange(0, medianIndex)); secondMedianCutCube = new PaletteCube(colors.GetRange(medianIndex, colors.Count - medianIndex)); }