Thursday, December 18, 2025
HomeLanguagesPHP | ImagickPixel getColor() Function

PHP | ImagickPixel getColor() Function

The ImagickPixel::getColor() function is an inbuilt function in PHP which is used to get 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. Keys of the array are r is (red), b is (blue), g is (green) and a is (alpha/opacity).

Syntax:

array ImagickPixel::getColor( int $normalized )

Parameters: This function accepts a single parameter $normalized which tells whether to normalize the values or not.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixel::getColor() function in PHP:

Program 1:




<?php
  
// Create a new imagickPixel object
$imagickPixel = new ImagickPixel(); 
  
// Get the color
$color = $imagickPixel->getColor();
  
// Print the color
print("<pre>".print_r($color, true)."</pre>");
?>


Output:

Array       // which is the default value
(
    [r] => 0
    [g] => 0
    [b] => 0
    [a] => 1
)

Program 2:




<?php
  
// Create a new imagickPixel object
// with a color
$imagickPixel = new ImagickPixel('#3539bd'); 
  
// Get the color
$color = $imagickPixel->getColor();
  
// Print the color
print("<pre>".print_r($color, true)."</pre>");
?>


Output:

Array
(
    [r] => 53
    [g] => 57
    [b] => 189
    [a] => 1
)

Program 3:




<?php
  
// Create a new imagickPixel object
$imagickPixel = new ImagickPixel(); 
  
// Set the color
$imagickPixel->setColor('#38d9d3');
  
// Get the color
$color = $imagickPixel->getColor();
  
// Print the color
print("<pre>".print_r($color, true)."</pre>");
?>


Output:

Array
(
    [r] => 56
    [g] => 217
    [b] => 211
    [a] => 1
)

Reference: https://www.php.net/manual/en/imagickpixel.getcolor.php

RELATED ARTICLES

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
108 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12036 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6910 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS