Re: [GD-DEVEL] Replacing one color with another

[email protected] (Pierre)
Newsgroups php.gd.devel
Message-ID <[email protected]>
Hi Andrie,

On 5/4/07, Andrei Stebakov <[email protected]> wrote:
> I was wondering if there is a way to replace one color for another in an
> image? Let's say I have an image with a white background and a number of
> solid color figures (circles) in say red color. I need to replace those red
> circles into black ones. If not gdlib, could you suggest a way I can go
> here?

Something like this should work for both truecolor and palette image:
black = gdImageColorAllocate(im, 0,0,0);
red = gdImageColorAllocate(im, 255,0,0); //red

for (y=0; y < gdImageSY(im); y++) {
 for (x=0; x < gdImageSx(im); x++) {
  int c =  gdImageGetPixel(im, x,y);
  if (c == red) {
    gdImageSetPixel(im, x, y, black);
  }
 }
}

For palette images only, you can replace the red color index values
directly, it should do it as well (a bit hackish though with the
current API).


--Pierre
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.