- Online Community Forums » Programming Questions » how to find and replace colours in a bitmap?

This thread is locked; no one can reply to it. rss feed Print
how to find and replace colours in a bitmap?
Member #7,581
August 2006

I'm making a snake game for multiplayer. I have one bitmap with all the snake body sprites. Each player will have a different coloured snake.

Is it possible to colour the inside of the snake sprites white (purple = transparent is used on the outsides of the snakes to let the background show) and use some function to replace the white with the colour of the snake, such as red?

Also would I need to make copies of the bitmap in memory for each player, or could I do the find and replace during painting the sprite pieces?

Sounds more efficent then creating separate bitmaps for each player, and will allow players to choose which colour they wish to be.

Thanks for your help

Member #565
August 2000

You can make multiple copies of the snake bitmap(s), and recolor them manually at run time. Takes more memory, but it works.

You can have only one copy of the bitmap, but keep that copy in 8 bits per pixel, keep the screen in 15/16/24/32 bits per pixel, use only drawing functions that support 8 bit sources and truecolor destinations, and switch palettes for each different snake color. You'll have to look up in the docs which drawing functions support 8 -> 15/16/24/32 drawing, I don't remember, but I think several do. And you'd need one palette per snake color.

Probably other ways to do it too.

Member #7,581
August 2006

I've solved the problem by creating my own blit function
It handles transparent pixels by ignoring them, and more importantly it replaces all white pixels with the colour of the snake. Apart from the replace white feature, this is probably how the blit function works, and so will probably run at nearly the same speed. I haven't put the size params because all the sprites are 32*32, however if you have different size you will need to modify the method

void objSnake::paintSeg (BITMAP *imgBuffer, BITMAP *imgSnakes, int sourceX, int sourceY, int desX, int desY)

int x, y, pixel;
int white = makecol(255, 255, 255);
int transparent = makecol(255, 0, 255);

for (x = 0; x < 32; x++)

for (y = 0; y < 32; y++)

pixel = getpixel(imgSnakes, sourceX + x, sourceY + y);

if (pixel == transparent)
else if (pixel == white)
pixel = this->myColour;

putpixel(imgBuffer, desX + x, desY + y, pixel);




Member #7,167
April 2006

If the bodyparts are just a few small bitmaps you could loop through the pixels replacing white with your color of choice. Maybe something like

1BITMAP* MakeBitmapCopy(BITMAP *bmp)
3 BITMAP *tmp = create_bitmap(bmp->w,bmp->h);
4 blit(tmp,0,0,0,0,bmp->w,bmp-h);
5 return tmp;
8void SwapBitmapColor(BITMAP *bmp, int ColorNow, int ColorThen)
10 for(int y=0;y<bmp->h;y++) {
11 for(int x=0;x<b->w;x++) {
12 if(getpixel(bmp,x,y)==ColorNow)
13 putpixel(bmp,x,y,ColorThen);
14 }
15 }
17// Somewhere in your code
18BITMAP *bmpSnakeHeadRed = MakeBitmapCopy(bmpSnakeHeadOriginal);

Note that MakeBitmapCopy() will return a new BITMAP, dont forget to run destroy_bitmap() on it.

Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

Member #794
November 2000


It handles transparent pixels by ignoring them, and more importantly it replaces all white pixels with the colour of the snake.

A better idea would be use an 8-bit image and use a custom palette for each player, or using a custom blender.

Member #2,407
June 2002

What I like to do is this:

1. Draw the sprites as normal, probably in one colour version or in black and white.
2. Make copies of the sprites and colour different areas with different solid easily identifiable colours. For example if I had a football player sprite for a football game, I would make shoes solid yellow, socks green, skin orange, shorts blue, shirt red, hair black, transparent parts magenta and parts that aren't supposed to be coloured white.
3. In-game at load time, for each colour scheme combine a pair of sprites from steps 1 and 2 to make the final coloured sprite. Take the sprite from step 1 and for all pixels that are yellow in the sprite from step 2, colour them with the shoe colour, all green ones with the socks colour and so on for all colourable areas. The colour of a pixel can be easily changed by converting the RGB values to HSV, changing H to the target colour and converting back to RGB.

sig used to be here

Member #2,981
December 2002

Your custom blit is going to be bloody slow.

Paul Pridham
Member #250
April 2000

Go to: