PHP | GmagickPixel getcolor() Function

The GmagickPixel::getcolor() function is an inbuilt function in PHP which is used to get the color described by the GmagickPixel object, as a string or an array. If the color has an opacity channel set, this is provided as a fourth value in the list.

Syntax:

mixed GmagickPixel::getcolor( bool $as_array,
                  bool $normalized_array )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $as_array (Optional): It specifies whether to get value as array. Its default value is FALSE.
  • $normalized_array (Optional): It specifies whether to get normalized array. Its default value is FALSE.

Return Value: This function returns a string or an array of color values.

Exceptions: This function throws GmagickPixelException on error.



Below given programs illustrate the GmagickPixel::getcolor() function in PHP:

Program 1 (Getting color as string):

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new GmagickPixel object
// using __construct
$gmagickPixel = new GmagickPixel('#ccb062');
  
// Get the color
$color = $gmagickPixel->getcolor();
print("<pre>".print_r($color, true)."</pre>");
?>

chevron_right


Output:

rgb(52428, 45232, 25186)

Program 2 (Getting color as array with normalization):

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new GmagickPixel object
// using __construct
$gmagickPixel = new GmagickPixel('#ccb062');
  
// Get the color
$color = $gmagickPixel->getcolor(true, true);
print("<pre>".print_r($color, true)."</pre>");
?>

chevron_right


Output:

Array
(
    [r] => 0.8
    [g] => 0.69019607843137
    [b] => 0.3843137254902
)

Program 3 (Getting color as array without normalization):

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new GmagickPixel object
// using __construct
$gmagickPixel = new GmagickPixel('#ccb062');
  
// Get the color
$color = $gmagickPixel->getcolor(true, false);
print("<pre>".print_r($color, true)."</pre>");
?>

chevron_right


Output:

Array
(
    [r] => 204
    [g] => 176
    [b] => 98
)

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




My Personal Notes arrow_drop_up

I love Open Source Culture have contributed to phpMyAdmin, LibreOffice, Ant-Design and Web is my passion

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.