xref: /PHP-7.2/ext/gd/tests/colorclosest.phpt (revision cb41a19f)
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);
17print_r(imagecolorsforindex($im, $c));
18imagedestroy($im);
19
20$im = imagecreate(5,5);
21imagecolorallocate($im, 255, 0, 255);
22$c = imagecolorclosest($im, 255,0,255);
23print_r(imagecolorsforindex($im, $c));
24imagedestroy($im);
25
26$im = imagecreate(5,5);
27for ($i=0; $i<255; $i++) imagecolorresolve($im, $i,0,0);
28$c = imagecolorclosest($im, 255,0,0);
29print_r(imagecolorsforindex($im, $c));
30
31
32$im = imagecreate(5,5);
33for ($i=0; $i<256; $i++) {
34	if ($i == 246) {
35		imagecolorallocate($im, $i,10,10);
36	} else {
37		imagecolorallocate($im, $i,0,0);
38	}
39}
40$c = imagecolorclosest($im, 255,10,10);
41print_r(imagecolorsforindex($im, $c));
42
43// with alpha
44$im = imagecreatetruecolor(5,5);
45$c = imagecolorclosestalpha($im, 255,0,255,100);
46printf("%X\n", $c);
47imagedestroy($im);
48
49$im = imagecreate(5,5);
50$c = imagecolorclosestalpha($im, 255,0,255,100);
51print_r(imagecolorsforindex($im, $c));
52imagedestroy($im);
53
54$im = imagecreate(5,5);
55imagecolorallocatealpha($im, 255, 0, 255, 1);
56$c = imagecolorclosestalpha($im, 255,0,255,1);
57print_r(imagecolorsforindex($im, $c));
58imagedestroy($im);
59
60$im = imagecreate(5,5);
61for ($i=0; $i<255; $i++) imagecolorresolvealpha($im, $i,0,0,1);
62$c = imagecolorclosestalpha($im, 255,0,0,1);
63print_r(imagecolorsforindex($im, $c));
64
65
66$im = imagecreate(5,5);
67for ($i=0; $i<256; $i++) {
68	if ($i == 246) {
69		imagecolorallocatealpha($im, $i,10,10,1);
70	} else {
71		imagecolorallocatealpha($im, $i,0,0,100);
72	}
73}
74$c = imagecolorclosestalpha($im, 255,10,10,1);
75print_r(imagecolorsforindex($im, $c));
76
77
78?>
79--EXPECTF--
80FF00FF
81
82Warning: imagecolorsforindex(): Color index -1 out of range in %s on line %d
83Array
84(
85    [red] => 255
86    [green] => 0
87    [blue] => 255
88    [alpha] => 0
89)
90Array
91(
92    [red] => 254
93    [green] => 0
94    [blue] => 0
95    [alpha] => 0
96)
97Array
98(
99    [red] => 246
100    [green] => 10
101    [blue] => 10
102    [alpha] => 0
103)
10464FF00FF
105
106Warning: imagecolorsforindex(): Color index -1 out of range in %s on line %d
107Array
108(
109    [red] => 255
110    [green] => 0
111    [blue] => 255
112    [alpha] => 1
113)
114Array
115(
116    [red] => 254
117    [green] => 0
118    [blue] => 0
119    [alpha] => 1
120)
121Array
122(
123    [red] => 246
124    [green] => 10
125    [blue] => 10
126    [alpha] => 1
127)
128