PHP | ImagickPixel isPixelSimilar() function

The ImagickPixel::isPixelSimilar() function is an inbuilt function in PHP which is used to check the distance between the color described by this ImagickPixel object and that of the provided object, by plotting their RGB values on the color cube. If the distance between the two points is less than the fuzz value given, the colors are similar.

Syntax:

bool ImagickPixel::isPixelSimilar( ImagickPixel $color, float $fuzz )

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



  • $color: It specifies the pixel containing the color to be compared with.
  • $fuzz: It specifies the fuzz value which tells the maximum distance within which to consider these colors as similar.

Return Value: This function returns a bool value which tells whether colors are similar (true) or not (false).

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixel::isPixelSimilar() function in PHP:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create a new imagickPixel object
$imagickPixelwhite = new ImagickPixel('white');
  
// Create another new imagickPixel object
$imagickPixelblue = new ImagickPixel('blue');
  
// Check if similar or not
$isSimilar = $imagickPixelwhite->isPixelSimilar($imagickPixelblue, 0.1);
  
if($isSimilar) {
    echo 'Similar';
} else {
    echo 'Not Similar';
}
?>

chevron_right


Output:

Not Similar

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create two new imagickPixel objects with same color
$imagickPixel1 = new ImagickPixel('green');
$imagickPixel2 = new ImagickPixel('green');
  
// Check if similar
$isSimilar = $imagickPixel1->isPixelSimilar($imagickPixel2, 0.01);
  
if($isSimilar) {
    echo 'Similar';
} else {
    echo 'Not Similar';
}
?>

chevron_right


Output:

Similar

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the image histogram
$histogramElements = $imagick->getImageHistogram();
  
// Get the 501th pixel
$imagickPixel1 = $histogramElements[500];
  
// Get the 601th pixel
$imagickPixel2 = $histogramElements[600];
  
// Check if similar
$isSimilar = $imagickPixel1->isPixelSimilar($imagickPixel2, 0.01);
  
if ($isSimilar) {
    echo 'Similar';
} else {
    echo 'Not Similar';
}
?>

chevron_right


Output:

Not Similar

Reference: https://www.php.net/manual/en/imagickpixel.ispixelsimilar.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.