Paint Colors
You can actually paint colors with
color handling methods.
Color Handling
For images with an 8-bit indexed palette it can be tricky to manage
colors (paint colors).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
$im = ImageCreate(300,256);
for($r=0; $r<256; $r++) {
$col = ImageColorAllocate($im,$r,0,0);
ImageLine($im, 0,$r, 100, $r, $col);
}
for($g=0; $g<256; $g++) {
$col = ImageColorAllocate($im,0,$g,0);
ImageLine($im, 100,255-$g, 200, 255-$g, $col);
}
for($b=0; $b<256; $b++) {
$col = ImageColorAllocate($im,0,0,$b);
ImageLine($im, 200,$b, 300, $b, $col);
}
Header('Content-Type: image/png');
ImagePNG($im);
?>
|
Output
For paletted images the following functions can be useful:
- ImageColorClosest
- ImageColorExact
- ImageColorDeallocate
For
Truecolor images we have no such issues.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
$im = ImageCreateTruecolor(300,256);
for($r=0; $r<256; $r++) {
$col = ImageColorAllocate($im,$r,0,0);
ImageLine($im, 0,$r, 100, $r, $col);
}
for($g=0; $g<256; $g++) {
$col = ImageColorAllocate($im,0,$g,0);
ImageLine($im, 100,255-$g, 200, 255-$g, $col);
}
for($b=0; $b<256; $b++) {
$col = ImageColorAllocate($im,0,0,$b);
ImageLine($im, 200,$b, 300, $b, $col);
}
Header('Content-Type: image/png');
ImagePNG($im);
?>
|
Output:
Truecolor
For
Truecolor images the colors are actually simple 31-bit longs. Or, think of them as being composed of 4 bytes arranged like this:
The highest or leftmost bit in the alpha channel is not used which
means the alpha channel can only have values from 0 to 127. You can use
the ImageColorAllocate
() as with
paletted images, but you can also
specify the color directly.
Example
|
<?php
$im = ImageCreateTruecolor(400,300);
ImageFilledRectangle($im,0,0,399,299,0x00ffffff);
ImageFilledEllipse($im,200,150,300,300,0x00000000);
ImageAlphaBlending($im,true);
ImageFilledRectangle($im,100,0,400,100,0x60ff1111);
ImageFilledRectangle($im,100,100,400,200,0x30ff1111);
ImageFilledRectangle($im,100,200,400,300,0x10ff1111);
Header('Content-Type: image/png');
ImagePNG($im);
?>
|
Output:
This example could also be written like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
$im = ImageCreateTruecolor(400,300);
$white = ImageColorAllocate($im,255,255,255);
ImageFilledRectangle($im,0,0,399,299,$white);
$black = ImageColorAllocate($im,0,0,0);
ImageFilledEllipse($im,200,150,300,300,$black);
ImageAlphaBlending($im,true);
$col = ImageColorResolveAlpha($im,0xff,0x11,0x11,0x60);
ImageFilledRectangle($im,100,0,400,100,$col);
$col = ImageColorResolveAlpha($im,0xff,0x11,0x11,0x30);
ImageFilledRectangle($im,100,100,400,200,$col);
$col = ImageColorResolveAlpha($im,0xff,0x11,0x11,0x10);
ImageFilledRectangle($im,100,200,400,300,$col);
Header('Content-Type: image/png');
ImagePNG($im);
?>
|
Given the nature of the way
truecolor colors are constructed, we can
rewrite our color testing strip using PHP’s
bitshift operator:
|
<?php
$im = ImageCreateTrueColor(256,60);
for($x=0; $x<256; $x++) {
ImageLine($im, $x, 0, $x, 19, $x);
ImageLine($im, 255-$x, 20, 255-$x, 39, $x<<8);
ImageLine($im, $x, 40, $x, 59, $x<<16);
}
Header('Content-Type: image/png');
ImagePNG($im);
?>
|
Output:
0 comments:
Post a Comment