xref: /PHP-8.0/ext/gd/tests/colorclosest.phpt (revision 14a26db3)
1--TEST--
2imageclosest
3--SKIPIF--
4<?php
5        if (!function_exists('imagecolorclosest')) die("skip gd extension not available\n");
6?>
7--FILE--
8<?php
9
10$im = imagecreatetruecolor(5,5);
11$c = imagecolorclosest($im, 255,0,255);
12printf("%X\n", $c);
13imagedestroy($im);
14
15$im = imagecreate(5,5);
16$c = imagecolorclosest($im, 255,0,255);
17try {
18  imagecolorsforindex($im, $c);
19} catch (ValueError $exception) {
20    echo $exception->getMessage() . "\n";
21}
22imagedestroy($im);
23
24$im = imagecreate(5,5);
25imagecolorallocate($im, 255, 0, 255);
26$c = imagecolorclosest($im, 255,0,255);
27print_r(imagecolorsforindex($im, $c));
28imagedestroy($im);
29
30$im = imagecreate(5,5);
31for ($i=0; $i<255; $i++) imagecolorresolve($im, $i,0,0);
32$c = imagecolorclosest($im, 255,0,0);
33print_r(imagecolorsforindex($im, $c));
34
35
36$im = imagecreate(5,5);
37for ($i=0; $i<256; $i++) {
38    if ($i == 246) {
39        imagecolorallocate($im, $i,10,10);
40    } else {
41        imagecolorallocate($im, $i,0,0);
42    }
43}
44$c = imagecolorclosest($im, 255,10,10);
45print_r(imagecolorsforindex($im, $c));
46
47// with alpha
48$im = imagecreatetruecolor(5,5);
49$c = imagecolorclosestalpha($im, 255,0,255,100);
50printf("%X\n", $c);
51imagedestroy($im);
52
53$im = imagecreate(5,5);
54$c = imagecolorclosestalpha($im, 255,0,255,100);
55try {
56  imagecolorsforindex($im, $c);
57} catch (ValueError $exception) {
58    echo $exception->getMessage() . "\n";
59}
60imagedestroy($im);
61
62$im = imagecreate(5,5);
63imagecolorallocatealpha($im, 255, 0, 255, 1);
64$c = imagecolorclosestalpha($im, 255,0,255,1);
65print_r(imagecolorsforindex($im, $c));
66imagedestroy($im);
67
68$im = imagecreate(5,5);
69for ($i=0; $i<255; $i++) imagecolorresolvealpha($im, $i,0,0,1);
70$c = imagecolorclosestalpha($im, 255,0,0,1);
71print_r(imagecolorsforindex($im, $c));
72
73$im = imagecreate(5,5);
74for ($i=0; $i<256; $i++) {
75    if ($i == 246) {
76        imagecolorallocatealpha($im, $i,10,10,1);
77    } else {
78        imagecolorallocatealpha($im, $i,0,0,100);
79    }
80}
81$c = imagecolorclosestalpha($im, 255,10,10,1);
82print_r(imagecolorsforindex($im, $c));
83
84?>
85--EXPECT--
86FF00FF
87imagecolorsforindex(): Argument #2 ($color) is out of range
88Array
89(
90    [red] => 255
91    [green] => 0
92    [blue] => 255
93    [alpha] => 0
94)
95Array
96(
97    [red] => 254
98    [green] => 0
99    [blue] => 0
100    [alpha] => 0
101)
102Array
103(
104    [red] => 246
105    [green] => 10
106    [blue] => 10
107    [alpha] => 0
108)
10964FF00FF
110imagecolorsforindex(): Argument #2 ($color) is out of range
111Array
112(
113    [red] => 255
114    [green] => 0
115    [blue] => 255
116    [alpha] => 1
117)
118Array
119(
120    [red] => 254
121    [green] => 0
122    [blue] => 0
123    [alpha] => 1
124)
125Array
126(
127    [red] => 246
128    [green] => 10
129    [blue] => 10
130    [alpha] => 1
131)
132