Replacing Colors in Images

BlitzMax Forums/BlitzMax Programming/Replacing Colors in Images

Twinprogrammer(Posted 2014) [#1]
Hey all,

I was wondering if you can change the colors of an image. For example, imagine if I have a person wearing an orange shirt. I know the exact rgb of the color. Is there a way I can use Blitz Max to replace the orange with a certain color?


Matty(Posted 2014) [#2]
I would imagine you would have to use the pixel editing commands for that (readpixel/writepixel? assuming they are similar to the ones in blitz3d/plus).


xlsior(Posted 2014) [#3]
Two different ways of doing it: a per-pixel read/write like Matty suggests, OR you can use a seperate sprite for the colored part, and overlay it over the rest of the image -- e.g. have a person, and a seperate (set of) sprite(s) with just the shirt.

If you create the shirt sprite in greyscale (to get light/dark shading), you can draw it in whatever color you want by using setcolor before drawing. If you want two identical characters, one with a red shirt and one with a green shirt:

setcolor 255,255,255 
drawimage guy1,100,100
drawimage guy2,400,100
setcolor 255,0,0
drawimage shirt,100,100
setcolor 0,255,0
drawimage shirt,400,100


with setcolor, you can specify whatever hue you want your image to be drawn in -- but you'd have to separate the character from his clothing, otherwise his skin/hair/whatever would also all be drawn in the same shade.

Of course, the catch is that the guy himself and the shirt do need to be aligned to the graphics/animations line up with one another, and are positioned appropriately. Probably the easiest to keep the image dimensions identical, so you can use the same x and y drawing coordinates for both images.


Derron(Posted 2014) [#4]
https://github.com/GWRon/Dig/blob/master/base.gfx.imagehelper.bmx

Use the function ColorizePixmapCopy() - feel free to adjust it to your needs. I use it to colorize "gray" elements (all other parts are slightly colored - so 200,196,196 instead of 200,200,200 etc.).

Using the approach of multiple sprites results in multiple draw calls - sometimes you may want to avoid that.



bye
Ron


Twinprogrammer(Posted 2014) [#5]
Ok, thanks for all of your help guys!