xref: /imagick/tests/024-ispixelsimilar.phpt (revision e40ec28c)
1--TEST--
2Test ImagickPixel::isPixelSimilar
3--SKIPIF--
4<?php require_once(dirname(__FILE__) . '/skipif.inc');
5
6--FILE--
7<?php
8
9$root3 = 1.732050807568877;
10
11$tests = array(
12	array ('rgb(245, 0, 0)',     'rgb(255, 0, 0)',        9 / $root3, false,),
13	array ('rgb(245, 0, 0)',     'rgb(255, 0, 0)',       10 / $root3, true,),
14	array ('rgb(0, 0, 0)',       'rgb(7, 7, 0)',          9 / $root3, false,),
15	array ('rgb(0, 0, 0)',       'rgb(7, 7, 0)',         10 / $root3, true,),
16	array ('rgba(0, 0, 0, 1)',   'rgba(7, 7, 0, 1)',      9 / $root3, false,),
17	array ('rgba(0, 0, 0, 1)',   'rgba(7, 7, 0, 1)',     10 / $root3, true,),
18	array ('rgb(128, 128, 128)', 'rgb(128, 128, 120)',    7 / $root3, false,),
19	array ('rgb(128, 128, 128)', 'rgb(128, 128, 120)',    8 / $root3, true,),
20
21	array ('rgb(0, 0, 0)',       'rgb(255, 255, 255)',  254.9,        false,),
22	array ('rgb(0, 0, 0)',       'rgb(255, 255, 255)',    255,        true,),
23	array ('rgb(255, 0, 0)',     'rgb(0, 255, 255)',    254.9,        false,),
24	array ('rgb(255, 0, 0)',     'rgb(0, 255, 255)',      255,        true,),
25	array ('black',              'rgba(0, 0, 0)',         0.0,        true),
26	array ('black',              'rgba(10, 0, 0, 1.0)',  10.0 / $root3, true),
27);
28
29try {
30	foreach ($tests as $testInfo) {
31		$color1 = $testInfo[0];
32		$color2 = $testInfo[1];
33		$distance = $testInfo[2];
34		$expectation = $testInfo[3];
35		$testDistance = ($distance / 255.0);
36
37		$color1Pixel = new ImagickPixel($color1);
38		$color2Pixel = new ImagickPixel($color2);
39
40		$isSimilar = $color1Pixel->isPixelSimilarQuantum($color2Pixel, $testDistance * \Imagick::getquantum());
41		if ($isSimilar !== $expectation) {
42			echo "isSimilar failed. Color [$color1] compared to color [$color2] distance $testDistance doesn't meet expected result [$expectation].". PHP_EOL;
43		}
44
45		$isPixelSimilar = $color1Pixel->isPixelSimilar($color2Pixel, $testDistance);
46		if ($isPixelSimilar !== $expectation) {
47			echo "isPixelSimilar failed. Color [$color1] compared to color [$color2] distance $testDistance doesn't meet expected result [$expectation].". PHP_EOL;
48		}
49	}
50	echo "success";
51} catch (\Exception $e) {
52	echo "Exception caught in ImagickPixel::isPixelSimilar test: ".$e->getMessage() . PHP_EOL;
53}
54
55?>
56--EXPECT--
57success