private static function quantize($pixels, $maxColors)
{
// short-circuit
if (!count($pixels) || $maxColors < 2 || $maxColors > 256) {
// echo 'wrong number of maxcolors'."\n";
return false;
}
$histo = static::getHisto($pixels);
// check that we aren't below maxcolors already
//if (count($histo) <= $maxcolors) {
// XXX: generate the new colors from the histo and return
//}
$vBox = static::vboxFromHistogram($histo);
$priorityQueue = new PQueue(function ($a, $b) {
return ColorThief::naturalOrder($a->count(), $b->count());
});
$priorityQueue->push($vBox);
// first set of colors, sorted by population
static::quantizeIter($priorityQueue, static::FRACT_BY_POPULATIONS * $maxColors, $histo);
// Re-sort by the product of pixel occupancy times the size in color space.
$priorityQueue->setComparator(function ($a, $b) {
return ColorThief::naturalOrder($a->count() * $a->volume(), $b->count() * $b->volume());
});
// next set - generate the median cuts using the (npix * vol) sorting.
static::quantizeIter($priorityQueue, $maxColors - $priorityQueue->size(), $histo);
// calculate the actual colors
$cmap = new CMap();
for ($i = $priorityQueue->size(); $i > 0; $i--) {
$cmap->push($priorityQueue->pop());
}
return $cmap;
}