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::vboxFromPixels($pixels, $histo);
$pq = new PQueue(function ($a, $b) {
return ColorThief::naturalOrder($a->count(), $b->count());
});
$pq->push($vbox);
// first set of colors, sorted by population
static::quantizeIter($pq, self::FRACT_BY_POPULATIONS * $maxcolors, $histo);
// Re-sort by the product of pixel occupancy times the size in color space.
$pq2 = new PQueue(function ($a, $b) {
return ColorThief::naturalOrder($a->count() * $a->volume(), $b->count() * $b->volume());
});
for ($i = $pq->size(); $i > 0; $i--) {
$pq2->push($pq->pop());
}
// next set - generate the median cuts using the (npix * vol) sorting.
static::quantizeIter($pq2, $maxcolors - $pq2->size(), $histo);
// calculate the actual colors
$cmap = new CMap();
for ($i = $pq2->size(); $i > 0; $i--) {
$cmap->push($pq2->pop());
}
return $cmap;
}