Bitmaps/Textures/Colours

francisg2002
francisg2002 used Ask the Experts™
on
Hi!

I am writing a game which is basde around blocks. At the moment I load in a 60x60 bitmap which contains a block which is in black and white.

What I want to do is change the white bits of the bitmap to a different colour. I don't really want to have to loop through the bitmap and change the colours manually.

Is there a function which will allow me to do this. Bear in mind the bitmap texture may be 16 colours, 256 colours or 24-bit colour.

If there is, can you please supply some sample code. Thanks.

Gary
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Maybe you can do this by changing the Palette? I'll be back... soon... hope...

Commented:
You could create a new bitmap and fill it fully with the new color. Then you could BitBlt it on your bitmap. I'm not sure which "dwRop" parameter you have to choose. Try them one by one, one of them will work exactly like you want it.

This will work for sure with the 24-bit bitmap. I'm not sure about the 16 and 256 color bitmap. You might want to try that. Alternatively changing the palette might also be an option for those color depths (as Luka already suggested).

Regards, Madshi.
Commented:
Comment by Madshi leads to better solution I think:

with Bitmap1.Canvas do begin
  CopyMode := cmMergeCopy;
  { Create 2nd Bitmap filled with desired color }
  Bitmap2.Height := Height;
  Bitmap2.Width := Width;
  Bitmap2.Canvas.Brush.Color := YOUR_COLOR;
  Bitmap2.Canvas.FillRect(ClipRect);
  { Copy it to the original black/white Bitmap }
  CopyRect(ClipRect, Bitmap2, ClipRect);
end;

For Palette handling see standard Delphi Help/Examples/Bitmap project, which demonstrates how to change bitmap colors changing palette.

Author

Commented:
That code didnt seem to work for me! I am not sure why though, as everythign you say seems to be right. I have since foudn a way to this within directx (what I was actualyl using the code for anyway).

As your answer shodul ebright in theory, I will give you the points mate.

GHary

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial