Skip to content

ImagickPixel::isSimilar broken. #10

Closed

Description

@Danack

Hi,

It looks like ImagickPixel::isSimilar is not working as described.

$red250 = new ImagickPixel('rgb(250, 0, 0)');
$pureRed = new ImagickPixel('rgb(255, 0, 0)');

$isSimilar = $pureRed->isSimilar($red250, 1);

var_dump($red250->getcolor(true));
var_dump($pureRed->getcolor(true));

echo "pure red  and red250 ";
if ($isSimilar) {
    echo "are similar";
}
else{
    echo "are not similar.";
}

outputs

array (size=4)
  'r' => float 0.98039215686275
  'g' => float 0
  'b' => float 0
  'a' => float 1
array (size=4)
  'r' => float 1
  'g' => float 0
  'b' => float 0
  'a' => float 1
pure red and red250 are not similar.

Changing the fuzz factor to 1000 makes the colours be similar, but that is a fuzz factor that is much larger than ever should be used - according to the manual.

It looks like this may be failing in the tests, FAIL Test filling thumbnail with color [tests/007_thumbnail_fill.phpt]

Additionally, the function accepts things that are not of type ImagickPixel as the first parameter. I'm not sure if thats bug or a feature.

Activity

  1. mkoppanen commented on Oct 16, 2013

    @mkoppanen
    Collaborator

    I think the 007 test fails on some imagemagick versions because setImageBackgroundColor behaves differently. I added a skip for known faulty versions.

    Other than that the parameters are passed directly to the MagickWand API. Which manual are you referring to with the fuzz parameter?

    As for the function accepting something else than imagickpixel, this is true for all methods that accept imagickpixel (they also accept string of valid color). Such as setBackgroundColor ('pink') and setBackgroundColor (new ImagickPixel ('pink'));

  2. Danack commented on Oct 16, 2013

    @Danack
    CollaboratorAuthor

    Sorry, I was referring to the PHP manual http://php.net/manual/en/imagickpixel.issimilar.php

    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.
    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).

    The distance between the two colours should be (5 / 256) which should be found to be less than one.

  3. mkoppanen commented on Oct 17, 2013

    @mkoppanen
    Collaborator

    Is the parameter fuzz given in the quantum range?

    <?php
    $red250 = new ImagickPixel('rgb(250, 0, 0)');
    $pureRed = new ImagickPixel('rgb(255, 0, 0)');
    
    $range = Imagick::getQuantumRange ();
    
    $isSimilar = $pureRed->isSimilar($red250, 0.012 * $range ['quantumRangeLong']);
    
    var_dump($red250->getcolor(true));
    var_dump($pureRed->getcolor(true));
    
    echo "pure red  and red250 ";
    if ($isSimilar) {
        echo "are similar";
    }
    else{
        echo "are not similar.";
    }
    echo PHP_EOL;
    
  4. Danack commented on Oct 17, 2013

    @Danack
    CollaboratorAuthor

    I think the underlying function may just be broken.

    It's giving wildly different results for when an Alpha is set, and when it isn't

    $redTest = new ImagickPixel('rgb(245, 0, 0)');
    $pureRed = new ImagickPixel('rgb(255, 0, 0)');
    
    //$redTest = new ImagickPixel('rgba(245, 0, 0, 0)');
    //$pureRed = new ImagickPixel('rgba(255, 0, 0, 0)');
    
    
    $tests = [
        1 => false,
        4 => false,
        '4.9' => false,
        5 => false,
        '5.1' => false,
        6 => false,
        9 => false,
        11 => true,
        25 => true,
    ];
    
    
    foreach ($tests as $distance => $expectation) {
    
        $testDistance = $distance / 255.0;
    
        $isSimilar = $pureRed->isSimilar($redTest, $testDistance);
    
        if ($isSimilar == $expectation) {
            echo "Test distance $distance aka $testDistance passed.".PHP_EOL;
        }
        else {
            echo "Test distance $distance aka $testDistance FAILED.".PHP_EOL;
        }
    }
    
    echo PHP_EOL;
    
    

    Changing between the red with and without alpha gives:

    
    Test distance 1 aka 0.003921568627451 passed.
    Test distance 4 aka 0.015686274509804 passed.
    Test distance 4.9 aka 0.01921568627451 passed.
    Test distance 5 aka 0.019607843137255 passed.
    Test distance 5.1 aka 0.02 passed.
    Test distance 6 aka 0.023529411764706 passed.
    Test distance 9 aka 0.035294117647059 passed.
    Test distance 11 aka 0.043137254901961 FAILED.
    Test distance 25 aka 0.098039215686275 FAILED.
    
    
    Test distance 1 aka 0.003921568627451 FAILED.
    Test distance 4 aka 0.015686274509804 FAILED.
    Test distance 4.9 aka 0.01921568627451 FAILED.
    Test distance 5 aka 0.019607843137255 FAILED.
    Test distance 5.1 aka 0.02 FAILED.
    Test distance 6 aka 0.023529411764706 FAILED.
    Test distance 9 aka 0.035294117647059 FAILED.
    Test distance 11 aka 0.043137254901961 passed.
    Test distance 25 aka 0.098039215686275 passed.
    
    

    i.e. the behaviour of the function just completely changes.

    I'll setup some tests to see if I can figure it out.

  5. mkoppanen commented on Oct 17, 2013

    @mkoppanen
    Collaborator
  6. Danack commented on Oct 17, 2013

    @Danack
    CollaboratorAuthor

    You were right that the fuzz value needs to be scaled by the QuantumRange.

    But I'm also getting more convinced the function IsMagickColorSimilar is also just broken. In it the fuzz factor is being multiplied by 3 (because of 'reasons'), but the distance comparison isn't being multiple by 3.

    e.g. the calculation is being done as

       fuzz*=3.0;
       pixel = p->red-q->red;
       distance+=pixel*pixel*scale;
    

    I'm testing rgb(245, 0, 0) against rgb(255, 0, 0) with a distance of 6

    Distance being tested = (6 / 255) * 65535 = 1542 quantum distance
    quantum distance^2 = 2,377,764
    
    Actual distance between colours = (10 / 255) * 65535 = 2,570 quantum distance
    quantum distance^2 = 6,604,900
    
    

    With some debugging added the final compare that it's doing is:

       distance is 6604900.000000, fuzz is 7133292.000000 
       Within distance, returning true
    

    So it's just getting the wrong value, because the fuzz has been multiplied by 3.

    I'll see if I can make a test in the Image Magick test suite and submit it as a bug.

  7. Danack commented on Oct 18, 2013

    @Danack
    CollaboratorAuthor
  8. Danack commented on Oct 19, 2013

    @Danack
    CollaboratorAuthor

    So, it's not a bug in ImageMagick but:

    • The value should be passed in to Imagick in the range zero to one, where one represents the distance from rgb(0, 0, 0) to rgb(255, 255, 255).
    • Imagick needs to scale the value by the Quantum range.

    Pull request has been sent.

  9. mkoppanen commented on Oct 30, 2013

    @mkoppanen
    Collaborator

    This has been resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions