Sign in to follow this  

Ignoring a color when drawing and image

Recommended Posts

Conqueran    139
I've been using targa to draw images to the screen using openGL. However, I'd like to ignore the color white. I don't want to draw white pixels to the screen, otherwise I get an ugly white box surrounding my sprites. How can I do this?

Share this post

Link to post
Share on other sites
RaptorZero    152
That's called color keying.

I don't know why you would want to do that since targa supports transparency but:
to make one color transparent you just have to take your image before it is sent to the drawing API (OpenGL), loop through all it's pixels and if it's color is white then you set this pixel's alpha value (transparency) to zero (fully transparent).

How exactely you should do that depends on how you are loading, storing and passing this image to the rendering API, so you could post some code to help us help you :)

Googling for 'color keying' should help a bit too.


Here's a bit of code:

void SetAlphaFromColor(unsigned char *pImage, int Size)
int i;
for(i = 0; i < Size; i += 4)
if(pImage[i] == 255 && pImage[i + 1] == 255 && pImage[i + 2] == 255)
pImage[i + 3] = 0;


That function should accept a image as an array of 'unsigned char' in the RGBA format (red-green-blue-alpha -> 4 chars per pixel) and the size of the array and it should make the white color transparent.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this