1--TEST--
2Imagick::setImageAlpha
3--SKIPIF--
4<?php
5$imageMagickRequiredVersion=0x700;
6require_once(dirname(__FILE__) . '/skipif.inc');
7?>
8--FILE--
9<?php
10
11require_once __DIR__ . "/../util/functions.php";
12
13
14
15$imagick = new Imagick();
16$imagick->newPseudoImage(256, 256, 'xc:purple');
17$imagick->setImageAlpha(0.5);
18
19$imagick->setImageFormat('png');
20$imagick->writeImage("./setAlphaTest.png");
21
22$pixelTypes = array(
23	Imagick::PIXEL_CHAR => array(128, 0, 128, 128),
24	Imagick::PIXEL_FLOAT => array(0.50196081399918, 0, 0.50196081399918, 0.5),
25    Imagick::PIXEL_DOUBLE => array(0.50196078431373, 0, 0.50196078431373, 0.5),
26	Imagick::PIXEL_SHORT => array(32896, 0, 32896, 32768),
27);
28
29function getColorError($type, $expected, $actual) {
30
31    if ($type == Imagick::PIXEL_CHAR ||
32        $type == Imagick::PIXEL_SHORT) {
33        $string  = "Expected: " . $actual . "\n";
34        $string .= "Actual  : " . $actual . "\n";
35
36        return $string;
37    }
38
39    if ($type == Imagick::PIXEL_FLOAT) {
40        return float_compare_32($expected, $actual);
41    }
42
43    if ($type == Imagick::PIXEL_DOUBLE) {
44        return float_compare($expected, $actual);
45    }
46
47    echo "Unknown type: $type \n";
48    exit(-1);
49
50}
51
52
53foreach ($pixelTypes as $pixelType => $expectedValues) {
54	$pixels = $imagick->exportImagePixels(0, 0, 1, 1, "RGBA", $pixelType);
55	$channelNames = ['R', 'G', 'B', 'A'];
56
57	// Loop over the colours
58	for ($channel=0; $channel<4; $channel++) {
59		$actual = $pixels[$channel];
60		$expected = $expectedValues[$channel];
61		if (abs($actual - $expected) > 0.0000001) {
62		    $channelName = $channelNames[$channel];
63
64			echo "Pixel values appear incorrect for pixelType $pixelType channel:$channelName\n";
65            echo getColorError($pixelType, $expected, $actual);
66			break;
67		}
68	}
69}
70
71echo "Ok";
72
73?>
74--EXPECTF--
75Ok