GD2 palette question

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

GD2 palette question

Post by Shendemiar »

(With 8-bit png, from database.)

How come this says the colors are not in the palette.

Looping thour all the pixels:
The only colour it says it is in the palette, is the colour that is in the palette with id=0, but this says it has id=9, which is black in the palette.

Code: Select all

function get_terrain($x,$y, $style='id') {
    $rgb = imagecolorat($this->{'terrain_image'}, $x, $y);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >>  & 0xFF;
    $b = $rgb & 0xFF;
    $tmp_id = "$r.$g.$b";

    switch ($style){
    [cut]
    case 'palette_id':
      return imagecolorexact($this->{'terrain_image'},$r,$g,$b);
      break;
    }
    [cut]
Outputted it looks just like it should be. And if I save it and open in graphics editor, the palette is just like it should be.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

imagecolorat() returns the palette index number for 8 bit images. To break it into RGB values you'll have to use imagecolorsforindex().
Post Reply