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