HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelTutorial Source codeReport an issue
Category
Example

Get color

← destroy   getColorQuantum →
Returns the color described by the ImagickPixel object, as an array. If the color has an opacity channel set, this is provided as a fourth value in the list.
Create an ImagickPixel with the predefined color 'brown' and set the color to have an alpha of 25%

Standard values

r : 165
g : 42
b : 42
a : 0

Normalized values

r : 0.6470588235
g : 0.1647058824
b : 0.1647058824
a : 0.25
// Example 1
$color = new \ImagickPixel('brown');
$color->setColorValue(\Imagick::COLOR_ALPHA, 0.25);
$output .= "<h4>Standard values</h4>";
foreach ($color->getColor() as $key => $value) {
    $output .= "$key : $value <br/>";
}
$output .= "<br/>";
$output .= "<h4>Normalized values</h4>";
foreach ($color->getColor(true) as $key => $value) {
    $output .= "$key : $value <br/>";
}
// Example 2 - ValueQuantum
$color = new \ImagickPixel('rgb(128, 5, 255)');
$colorRed = $color->getColorValueQuantum(\Imagick::COLOR_RED);
$colorGreen = $color->getColorValueQuantum(\Imagick::COLOR_GREEN);
$colorBlue = $color->getColorValueQuantum(\Imagick::COLOR_BLUE);
$colorAlpha = $color->getColorValueQuantum(\Imagick::COLOR_ALPHA);
return sprintf(
    "Red: %s Green: %s  Blue %s Alpha: %s",
    $colorRed,
    $colorGreen,
    $colorBlue,
    $colorAlpha
);
// Example 3 - Quantum
$color = new \ImagickPixel('brown');
$color->setColorValue(\Imagick::COLOR_ALPHA, 0.25);
$output .= "<h4>Values</h4>";
foreach ($color->getColorQuantum() as $key => $value) {
    $output .= "$key : " . var_export($value, true) . " <br/>";
}
// Example 4 - Value
$color = new \ImagickPixel('rgba(90%, 20%, 20%, 0.75)');
$output =  "Alpha value is " . $color->getColorValue(\Imagick::COLOR_ALPHA) . "<br/>";
$output .=  "" . "<br/>";
$output .=  "Red value is " . $color->getColorValue(\Imagick::COLOR_RED) . "<br/>";
$output .=  "Green value is " . $color->getColorValue(\Imagick::COLOR_GREEN) . "<br/>";
$output .=  "Blue value is " . $color->getColorValue(\Imagick::COLOR_BLUE) . "<br/>";
$output .=  "" . "<br/>";
$output .=  "Cyan value is " . $color->getColorValue(\Imagick::COLOR_CYAN) . "<br/>";
$output .=  "Magenta value is " . $color->getColorValue(\Imagick::COLOR_MAGENTA) . "<br/>";
$output .=  "Yellow value is " . $color->getColorValue(\Imagick::COLOR_YELLOW) . "<br/>";
$output .=  "Black value is " . $color->getColorValue(\Imagick::COLOR_BLACK) . "<br/>";
// Example 5 - AsString
$output = "Create an ImagickPixel with the predefined color 'brown' and output the result of `getColorAsString`. <br/>";
$color = new \ImagickPixel('brown');
$color->setColorValue(\Imagick::COLOR_ALPHA, 64 / 256.0);
$output .= $color->getColorAsString();
return $output;
// Example 6 - Count
$imagick = new \Imagick();
$imagick->newPseudoImage(640, 480, "magick:logo");
$histogramElements = $imagick->getImageHistogram();
$lastColor = array_pop($histogramElements);
$color = $lastColor->getColor();
$output = sprintf(
    "ColorCount last pixel = %d\nColor is R %d G %d B %d",
    $lastColor->getColorCount(),
    $color['r'],
    $color['g'],
    $color['b']
);

return nl2br($output);