imagecolortransparent: color, but not index dependant

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zhade
Forum Newbie
Posts: 3
Joined: Sun Oct 10, 2010 2:13 pm

imagecolortransparent: color, but not index dependant

Post by zhade »

Alright, the problem is as follows. I am trying to create an image via imagecreatetruecolor. Basicly I convert sprite files (sprites as in you see in those 2d sidescrolling beat-em up games) to gif. Even though everything is completely and fully working there is just one thing I just can't seem to fix.
Since those sprites use color palettes they have no transparency, yet the transparency is defined through the first index of the color palette.
Means the parser knows which parts of the image have to be transparent, when rendering.

Now the problem is that imagecolortransparent can only make COLORS transparent, but not specific index colors in the palette. So if there are multiple colors in the palette but with different indexes, the color, since they're the same are both transparent in the end.

So my question is: is it somehow possible to only make specific indexed colors transparent instead of a color itself?
Because if it doesn't I see no other option than to create a complete gif encoder just for this little feature. And I honestly dont want to do that >__>
I could also make it an alpha based image, but i'd like to keep the images filesize small in the end.

Thanks in advance!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: imagecolortransparent: color, but not index dependant

Post by requinix »

As long as the sprites are palette images, you should be able to do exactly what you would first think:
If the sprites really have the first index as transparent (and not the index of the top-left pixel) then pass 0 to imagetransparent.

The reason being that GD, when it comes to palette images, deals with palette indexes - not colors. For example, if you do imagecolorat(X,Y) you'll get the color's index, not the RGB(A) integer.
zhade
Forum Newbie
Posts: 3
Joined: Sun Oct 10, 2010 2:13 pm

Re: imagecolortransparent: color, but not index dependant

Post by zhade »

Thanks for the answer, but I still don't really understand what you mean...
So in order to make GD treat the image like a palette image I have to use the function imagetruecolortopalette() ?
But I still don't know how I would be able to pass a index number to the imagecolortransparent() function as it requires a color through the imagecolorallocate() function. And again to this function I can also only pass the color values but no indexes.

Heres a picture of the color palette that is being used. The first color with index 00 is white. However the last color with a different index (ff) is also white.
Now imagecolortransparent makes both transparent. And I still don't know how I can pass any sort of index color to any function.
I can set the whole palette with imagecolorset() but then again, there is no way I know of which lets me write index colors, means where I have to pass an index and not a color to.
Image

I'm sorry if you answered it, but if so could you elaborate it a bit more? Thanks.
zhade
Forum Newbie
Posts: 3
Joined: Sun Oct 10, 2010 2:13 pm

Re: imagecolortransparent: color, but not index dependant

Post by zhade »

I am still in need of a solution to this problem. Note: I won't bump this anymore.
Post Reply