Pop\Image\Imagick::getColors PHP Метод

getColors() публичный Метод

Return all of the colors in the palette in an array format
public getColors ( integer | string $format = Pop\Image\Imagick::HEX ) : array
$format integer | string
Результат array
    public function getColors($format = \Pop\Image\Imagick::HEX)
    {
        // Initialize the colors array and the image resource.
        $colors = array();
        // Loop through each pixel of the image, recording the color result
        // in the color array.
        for ($h = 0; $h < $this->height; $h++) {
            for ($w = 0; $w < $this->width; $w++) {
                $point = $this->resource->getImagePixelColor($w, $h);
                $color = $point->getColor();
                // Convert to the proper HEX or RGB format.
                if ($format) {
                    $rgb = sprintf('%02s', dechex($color['r'])) . sprintf('%02s', dechex($color['g'])) . sprintf('%02s', dechex($color['b']));
                } else {
                    $rgb = $color['r'] . "," . $color['g'] . "," . $color['b'];
                }
                // If the color is not already in the array, add to it.
                if (!in_array($rgb, $colors)) {
                    $colors[] = $rgb;
                }
            }
        }
        // Return the colors array.
        return $colors;
    }

Usage Example

Пример #1
0
 public function testGetColors()
 {
     $i = new Imagick(__DIR__ . '/../tmp/test.gif');
     $hex = $i->getColors();
     $rgb = $i->getColors(false);
     $this->assertEquals(16, count($hex));
     $this->assertEquals(16, count($rgb));
     $this->assertTrue(in_array('113405', $hex));
     $this->assertTrue(in_array('17,52,5', $rgb));
 }