public function count($force = false)
{
if (!$this->count_set || $force) {
$npix = 0;
// Select the fastest way (i.e. with the fewest iterations) to count
// the number of pixels contained in this vbox.
if ($this->volume() > count($this->histo)) {
// Iterate over the histogram if the size of this histogram is lower than the vbox volume
foreach ($this->histo as $rgb => $count) {
$rgb_array = ColorThief::getColorsFromIndex($rgb, 0, ColorThief::SIGBITS);
if ($this->contains($rgb_array, 0)) {
$npix += $count;
}
}
} else {
// Or iterate over points of the vbox if the size of the histogram is greater than the vbox volume
for ($i = $this->r1; $i <= $this->r2; $i++) {
for ($j = $this->g1; $j <= $this->g2; $j++) {
for ($k = $this->b1; $k <= $this->b2; $k++) {
$index = ColorThief::getColorIndex($i, $j, $k);
if (isset($this->histo[$index])) {
$npix += $this->histo[$index];
}
}
}
}
}
$this->count = $npix;
$this->count_set = true;
}
return $this->count;
}
/** * @covers ColorThief\VBox::count */ public function testCount() { $this->vbox->r1 = 225 >> ColorThief::RSHIFT; $this->vbox->r2 = 247 >> ColorThief::RSHIFT; $this->vbox->g1 = 180 >> ColorThief::RSHIFT; $this->vbox->g2 = 189 >> ColorThief::RSHIFT; $this->vbox->b1 = 130 >> ColorThief::RSHIFT; $this->vbox->b2 = 158 >> ColorThief::RSHIFT; //$pixels = array(0xE1BE9E, 0xC8BD9E, 0xFFBD9E, 0xE1329E, 0xE1C89E, 0xE1BD64, 0xE1BDC8); $this->vbox->histo = array(29427 => 1, 26355 => 1, 32499 => 1, 28883 => 1, 29491 => 1, 29420 => 1, 29433 => 1); $this->assertEquals(1, $this->vbox->count()); $this->vbox->histo[29427] = 2; $this->vbox->histo[30449] = 1; // Previous result should be cached. $this->assertEquals(1, $this->vbox->count()); // Forcing refresh should now give the right result $this->assertEquals(3, $this->vbox->count(true)); }