Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to change the color of my bitmap

This thread is locked; no one can reply to it. rss feed Print
How to change the color of my bitmap
peelemachine
Member #8,085
December 2006

Hi hello, I have a question.

I drew this white polygon in Adobe Illustrator and i saved it as a bitmap with a pink background.

I can easily load it into my program, but I need to change the white to any other color I want, like green or purple. But the pink background needs to stay transparent. I think it should be easy but I'm out of ideas.

Please help me :'(

You're damned if you do, and you're damned if you don't.

Kris Asick
Member #1,424
July 2001

Yes, it does sound simple, and with 256-colour bitmaps, it is. With anything else however, it's not.

There is a command in Allegro called draw_character() which is similar to draw_sprite() but functions similarly to the textprintf() commands in that you can define a solid colour to draw in. (Or with draw_character_ex(), a background colour too.)

However, even though you can only use this command with 256-colour bitmaps for the source (as far as I'm aware), you can draw onto any other colour-depth bitmap with it.

Remembering too that with 256-colour bitmaps, translucency is defined as colour index 0 by default instead of bright magenta.

If you really must use a source bitmap with more than 256 colours you may need to use lit sprites, using the draw_lit_sprite() command and by setting up the blender functions within Allegro. This will eat away CPU time however and depending on the type of project you're working on, may not be practical to your framerate.

All of the commands I've mentioned can be looked up in the manual, either on your computer in the allegro/docs folder or here.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Steve Terry
Member #1,989
March 2002
avatar

If it is practical you can prerender all of your bitmaps to all the colors you will need from the one base bitmap by creating a new bitmap of the same size and write a function to rip through the base bitmap and plot the pixel on the resulting bitmap by changing white to the desired color and magenta to magenta. Now if it's a polygon you can use allegros polygon routines and have it rendered in real time using allegro. If it's more complex and high color mode then you will have to prerender if you want obtainable speeds in your program. This usually isn't a problem if you have colored team sprites or something, if it's going to change a lot of colors then you will eat up too much memory.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

James Stanley
Member #7,275
May 2006
avatar

void replace_colour(BITMAP *bmp, int curcolour, int newcolour) {
  int x, y;

  for(y = 0; y < bmp->h; y++) {
    for(x = 0; x < bmp->w; x++) {
      if(getpixel(x, y) == curcolour) putpixel(x, y, newcolour);
    }
  }

}

Untested, but it should work.

peelemachine
Member #8,085
December 2006

Sweet, thanks for the help.

I think I can make it work now. I don't have the internet at home yet, so it's hard to come on here sometimes. Thanks again 8-)

You're damned if you do, and you're damned if you don't.

Go to: