public Color[] GetPalette(int paletteSize) { Color[] colors = new Color[paletteSize]; ColorBox[] boxes = new ColorBox[paletteSize]; int boxIndex = 0; ColorBox box; int startIndex = 0; if (_transparentColors.Count > 0) { box = new ColorBox(0); foreach (Color color in _transparentColors) box.Colors.Add(color); box.ShrinkToFit(); boxes[boxIndex] = box; boxIndex++; startIndex++; } //Handle the rest of the boxes box = new ColorBox(0); foreach (Color color in _opaqueColors) box.Colors.Add(color); box.ShrinkToFit(); boxes[boxIndex] = box; boxIndex++; while (boxIndex < paletteSize) { int longestEdge = -1; int longestIndex = startIndex; for (int i = startIndex; i < boxIndex; i++) { if (boxes[i].LongestSide > longestEdge) { longestEdge = boxes[i].LongestSide; longestIndex = i; } } Tuple<ColorBox, ColorBox> newBoxes = boxes[longestIndex].SplitBox(); boxes[longestIndex] = newBoxes.Item1; boxes[boxIndex] = newBoxes.Item2; boxIndex++; } for (int i = 0; i < boxes.Length; i++) colors[i] = boxes[i].GetCentroidColor(); return colors; }