views:

131

answers:

1

Hi all,

I need to find a colour within an image and replace it - if it is present.

All I can find is information about replacing within an indexed image.

Is there an easy way to do it with a true color image?

Is it more efficient to convert a true color image to an indexed one and then replace the colour?

Thanks,

Mark

A: 

Use imagetruecolortopalette to create a palette image, then you can use imagecolorexact to find the index of the color you're looking for and then call imagecolorset to change it.

Chris Newman
The problem I have with this is I am creating an image mask... by that I mean a square filled with a 'chroma-key' colour with a shape cut out of it with transparent pixels then overlaying that onto a picture. Finally it replaces the 'chroma-key' colour with transparent pixels. The reason I want to replace the colour is I don't want the picture to contain the 'chroma key' colour. The problem with your solution is for it to work I would have to make it a pallet image, replace the colour and then make it a true colour image again.
Mark
So how about this? :Create a copy of your image and convert to a palette image.Use imagecolorexact to find the index of the matching colorUse imagecolordeallocate to remove all other colors from the paletteYou can then change the remaining (chromakey) color and try some jiggery pokery using the new image as a mask to overlay on the original truecolor version.I have not tested this and I am not sure what the result is when you deallocate all colours except one, you'll have to do some sucking and seeing, but I think this approach may have legs if you want to avoid converting the image.
Chris Newman
Sounds like a good solution, I'll try it and get back to you
Mark