PHP | imagecolorclosesthwb() Function

The imagecolorclosesthwb() function is an inbuilt function in PHP which is used to get the index of the color hue, white and blackness in the given image. This function returns an integer value with the index of the color which contains the hue, white and blackness nearest the given color.

Syntax:

int imagecolorclosesthwb ( $image, $red, $green, $blue )

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



  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $red: This parameter is used to set value of red color component.
  • $green: This parameter is used to set value of green color component.
  • $blue: This parameter is used to set value of blue color component.

Return Value: This function returns an integer value with the index of the color hue, white and blackness nearest the given color in the image.

Below programs illustrate the imagecolorclosesthwb() function in PHP.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new image from given URL
$image = imagecreatefromgif(
  
// Display the index of color
echo 'Hue White and Blackness closest color index: ' 
    . imagecolorclosesthwb($image, 5, 165, 10);
  
imagedestroy($image);
?>

chevron_right


Output:

Hue White and Blackness closest color index: 42

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new image from given URL
$image = imagecreatefromgif(
  
// Array contains rgb color value
$color = array(
    array(7, 150, 0),
    array(53, 190, 255),
    array(255, 165, 54)
);
  
// Loop to find closest HWB color
foreach($color as $id => $rgb)
{
    echo 'Hue White and Blackness closest color index: '
    . imagecolorclosesthwb($image, $rgb[0], $rgb[1], $rgb[2]) . "<br>";
}
  
imagedestroy($image);
?>

chevron_right


Output:

Hue White and Blackness closest color index: 42
Hue White and Blackness closest color index: 101
Hue White and Blackness closest color index: 186

Related Articles:

Reference: http://php.net/manual/en/function.imagecolorclosesthwb.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.