/// <summary> /// See <see cref="BaseColorCacheQuantizer.OnGetPaletteToCache"/> for more details. /// </summary> protected override List <Color> OnGetPaletteToCache(Int32 colorCount) { // creates the initial cube covering all the pixels in the image MedianCutCube initalMedianCutCube = new MedianCutCube(UniqueColors.Keys); 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 <Color> result = new List <Color>(); Int32 paletteIndex = 0; // adds all the cubes' colors to the palette, and mark that cube with palette index for later use foreach (MedianCutCube cube in cubeList) { result.Add(cube.Color); cube.SetPaletteIndex(paletteIndex++); } // returns the palette (should contain <= ColorCount colors) return(result); }
/// <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 MedianCutCube firstMedianCutCube, out MedianCutCube secondMedianCutCube) { List <Int32> colors; switch (componentIndex) { // red colors case 0: colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentA(ColorModel, TextureBitmap.Color.FromARGB(argb))).ToList(); break; // green colors case 1: colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentB(ColorModel, TextureBitmap.Color.FromARGB(argb))).ToList(); break; // blue colors case 2: colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentC(ColorModel, TextureBitmap.Color.FromARGB(argb))).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 MedianCutCube(colors.GetRange(0, medianIndex)); secondMedianCutCube = new MedianCutCube(colors.GetRange(medianIndex, colors.Count - medianIndex)); }
/// <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 MedianCutCube firstMedianCutCube, out MedianCutCube secondMedianCutCube) { List<Int32> colors; switch (componentIndex) { // red colors case 0: colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentA(ColorModel, Color.FromArgb(argb))).ToList(); break; // green colors case 1: colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentB(ColorModel, Color.FromArgb(argb))).ToList(); break; // blue colors case 2: colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentC(ColorModel, Color.FromArgb(argb))).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 MedianCutCube(colors.GetRange(0, medianIndex)); secondMedianCutCube = new MedianCutCube(colors.GetRange(medianIndex, colors.Count - medianIndex)); }
/// <summary> /// See <see cref="BaseColorCacheQuantizer.OnGetPaletteToCache"/> for more details. /// </summary> protected override List<Color> OnGetPaletteToCache(Int32 colorCount) { // creates the initial cube covering all the pixels in the image MedianCutCube initalMedianCutCube = new MedianCutCube(UniqueColors.Keys); 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<Color> result = new List<Color>(); Int32 paletteIndex = 0; // adds all the cubes' colors to the palette, and mark that cube with palette index for later use foreach (MedianCutCube cube in cubeList) { result.Add(cube.Color); cube.SetPaletteIndex(paletteIndex++); } // returns the palette (should contain <= ColorCount colors) return result; }