xref: /imagick/tests/025-get-color.phpt (revision a806b85e)
1--TEST--
2Test getColor and getColorQuantum
3--SKIPIF--
4<?php
5
6require_once(dirname(__FILE__) . '/skipif.inc');
7
8
9?>
10--FILE--
11<?php
12
13define('ORIGINAL', 'ORIGINAL');
14define('NORMALISED', 'NORMALISED');
15define('NORMALISED_INCLUDING_ALPHA', 'NORMALISED_INCLUDING_ALPHA');
16define('QUANTUM', 'QUANTUM');
17
18function checkExpectedValue($expectedValue, $actualValue, $hasVariance) {
19
20	$variance = 0;
21
22	// Behaviour of 50% pixel was changed in
23	// key = version
24	// value = variance expected in result
25	$troubledVersions = array(
26		0x692 => 1
27	);
28	$v = Imagick::getVersion();
29	$versionNumber = $v['versionNumber'];
30
31	if (array_key_exists($versionNumber, $troubledVersions)) {
32		$variance = $troubledVersions[$versionNumber];
33	}
34
35	if (Imagick::getHDRIEnabled()) {
36		return abs($expectedValue - $actualValue) < (0.01 + $variance);
37	}
38
39	if ($hasVariance) {
40		$difference = abs($expectedValue - $actualValue);
41		if ($difference < 1 + $variance) {
42			return true;
43		}
44		echo "difference $difference not < 1 + variance $variance\n";
45		return false;
46	}
47	else if($expectedValue == $actualValue) {
48		return true;
49	}
50
51	return false;
52}
53
54function getExpectedValue($someValue) {
55	if (Imagick::getHDRIEnabled()) {
56		return $someValue;
57	}
58
59	$v = Imagick::getVersion();
60	if ($v['versionNumber'] >= 0x692) {
61		//this is the new correct behaviour
62		return (intval(round($someValue, 0, PHP_ROUND_HALF_UP)));
63	}
64	else {
65		//old behaviour had wrong rounding.
66		return (intval(round($someValue, 0, PHP_ROUND_HALF_DOWN)));
67	}
68}
69
70
71$tests = array(
72	array(
73		'red',
74		ORIGINAL,
75		array(
76			array('r', getExpectedValue(255), 0),
77			array('a', getExpectedValue(1.0), 0)
78		),
79	),
80	array(
81		'red',
82		QUANTUM,
83		array(
84			array('r', getExpectedValue(\Imagick::getQuantum()), 0),
85			array('a', getExpectedValue(\Imagick::getQuantum()), 0)
86		),
87	),
88	array(
89		'rgb(25%, 25%, 25%)',
90		QUANTUM,
91		array(
92			array('r', getExpectedValue(\Imagick::getQuantum() / 4), 0),
93			array('a', getExpectedValue(\Imagick::getQuantum()), 0),
94		)
95	)
96);
97
98$version = Imagick::getVersion();
99// The following don't seem stable in lesser versions.
100if ($version['versionNumber'] >= 0x687) {
101	$tests[] = array(
102		'green',
103		QUANTUM,
104		array(
105			array('g', getExpectedValue(\Imagick::getQuantum() * (128 / 255)), 1),
106			array('a', getExpectedValue(\Imagick::getQuantum()), 1)
107		),
108	);
109
110	$tests[] = array(
111		'rgb(0, 50%, 0)',
112		QUANTUM,
113		array(
114			array('g', getExpectedValue(\Imagick::getQuantum() / 2), 1),
115			array('a', getExpectedValue(\Imagick::getQuantum()), 0)
116		),
117	);
118}
119
120
121foreach ($tests as $test) {
122
123	list($colorString, $type, $expectations) = $test;
124	$pixel = new ImagickPixel($colorString);
125
126	switch ($type) {
127
128		case(ORIGINAL): {
129			$color = $pixel->getColor();
130			break;
131		}
132
133		case(NORMALISED): {
134			$color = $pixel->getColor(true);
135			break;
136		}
137
138		case(NORMALISED_INCLUDING_ALPHA): {
139			$color = $pixel->getColor(2);
140			break;
141		}
142
143		case(QUANTUM): {
144			$color = $pixel->getColorQuantum();
145			break;
146		}
147
148		default:{
149			echo "Unknown test type $type" . PHP_EOL;
150			break;
151		}
152	}
153
154	foreach ($expectations as $test) {
155		list($key, $expectedValue, $hasVariance) = $test;
156		if (!checkExpectedValue($expectedValue, $color[$key], $hasVariance)) {
157			printf(
158				"%s %s is wrong for colorString '%s': actual %s != expected %s"  . PHP_EOL,
159				$type,
160				$key, $colorString,
161				$color[$key], $expectedValue
162			);
163		}
164	}
165}
166
167echo "OK" . PHP_EOL;
168?>
169--EXPECT--
170OK