ImagickPixel::isSimilar

(PECL imagick 2, PECL imagick 3)

ImagickPixel::isSimilarCheck the distance between this color and another

Description

public ImagickPixel::isSimilar(ImagickPixel $color, float $fuzz): bool
Warning

This function is currently not documented; only its argument list is available.

Checks 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. Deprecated in favour of ImagickPixel::isPixelSimilar().

Parameters

color

The ImagickPixel object to compare this object against.

fuzz

The maximum distance within which to consider these colors as similar. The theoretical maximum for this value is the square root of three (1.732).

Return Values

Returns true on success.

Examples

Example #1 ImagickPixel::isSimilar()

<?php
        
// The tests below are written with the maximum distance expressed as 255
        // so we need to scale them by the square root of 3 - the diagonal length
        // of a unit cube.
        
$root3 1.732050807568877;

        
$tests = array(
            [
'rgb(245, 0, 0)',      'rgb(255, 0, 0)',   $root3,         false,],
            [
'rgb(245, 0, 0)',      'rgb(255, 0, 0)',  10 $root3,         true,],
            [
'rgb(0, 0, 0)',        'rgb(7, 7, 0)',     $root3,         false,],
            [
'rgb(0, 0, 0)',        'rgb(7, 7, 0)',    10 $root3,         true,],
            [
'rgba(0, 0, 0, 1)',    'rgba(7, 7, 0, 1)'$root3,         false,],
            [
'rgba(0, 0, 0, 1)',    'rgba(7, 7, 0, 1)',    10 $root3,     true,],
            [
'rgb(128, 128, 128)',  'rgb(128, 128, 120)',   $root3,     false,],
            [
'rgb(128, 128, 128)',  'rgb(128, 128, 120)',   $root3,     true,],
            [
'rgb(0, 0, 0)',        'rgb(255, 255, 255)',   254.9,          false,],
            [
'rgb(0, 0, 0)',        'rgb(255, 255, 255)',   255,            true,],
            [
'rgb(255, 0, 0)',      'rgb(0, 255, 255)',     254.9,          false,],
            [
'rgb(255, 0, 0)',      'rgb(0, 255, 255)',     255,            true,],
            [
'black',               'rgba(0, 0, 0)',        0.0,            true],
            [
'black',               'rgba(10, 0, 0, 1.0)',  10.0 $root3,  true],);

        
$output "<table width='100%' class='infoTable'><thead>
                <tr>
                <th>
                Color 1
                </th>
                <th>
                Color 2
                </th>
                <th>
                    Test distance * 255
                </th>
                <th>
                    Is within distance
                </th>
                </tr>
        </thead>"
;

        
$output .= "<tbody>";

        foreach (
$tests as $testInfo) {
            
$color1 $testInfo[0];
            
$color2 $testInfo[1];
            
$distance $testInfo[2];
            
$expectation $testInfo[3];
            
$testDistance = ($distance 255.0);

            
$color1Pixel = new \ImagickPixel($color1);
            
$color2Pixel = new \ImagickPixel($color2);

            
$isSimilar $color1Pixel->isPixelSimilar($color2Pixel$testDistance);


            if (
$isSimilar !== $expectation) {
                echo 
"Test distance failed. Color [$color1] compared to color [$color2] is not within distance $testDistance FAILED.".NL;
            }

            
$layout "<tr>
                <td>%s</td>
                <td>%s</td>
                <td>%s</td>
                <td style='text-align: center;'>%s</td>
            </tr>"
;
            
            
$output .= sprintf(
                
$layout,
                
$color1,
                
$color2,
                
$distance,
                
$isSimilar 'yes' 'no'
            
);
        }

        
$output .= "</tbody></table>";
        
        return 
$output;

?>

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial