Jump to content
  • Advertisement
Sign in to follow this  

SDL color blending?

This topic is 5114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I blit an image so that, say, it appears more green/red/blue than it really is? For instance, I want to blit an image and have it blended with rgb (255,0,0). Is there any built in support for this type of thing?

Share this post

Link to post
Share on other sites
Just create a new surface, and make the surface red/green/blue and then set the alpha, then blit this surface over the surface you want more red/green/blue. . .

#include <SDL/SDL.h>

int main(int argc, char * argv[])
SDL_Surface * MainScreen = SDL_SetVideoMode(800, 600, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_Surface * Image = SDL_LoadBMP("smile.bmp");
SDL_Surface * Cover = SDL_CreateRGBSurface(Image->flags, Image->w, Image->h, Image->format->BitsPerPixel, Image->format->Rmask, Image->format->Bmask, Image->format->Gmask, Image->format->Amask);
int Red = 0;
int Green = 0;
int Blue = 100;
SDL_FillRect(Cover, NULL, SDL_MapRGB(Cover->format, Red, Green, Blue));
int Opacity = 127; // 0 being totally transparent and 255 being totally solid
SDL_SetAlpha(Cover, SDL_SRCALPHA, Opacity);

bool Done(false);
SDL_Event Event;

case SDL_QUIT:
Done = true;

SDL_BlitSurface(Image, NULL, MainScreen, NULL);
SDL_BlitSurface(Cover, NULL, MainScreen, NULL);

SDL_FillRect(MainScreen, NULL, SDL_MapRGB(MainScreen->format, 0, 0, 0));
return 0;

[Edited by - ImperfectFreak on December 15, 2004 11:09:45 PM]

Share this post

Link to post
Share on other sites
If you're not using OpenGL, you can do it using palettes. Make sure the bitmap you load is 8-bit. (You can use irfanview to convert images)

After you load the surface, do NOT convert it to the display format.

Create an array - SDL_Color oldcolors[255]
Copy the palette from surface->format->palette->colors to the 'oldcolors' array.

Now you need to make a function like so:

void SetColors(SDL_Surface* surf,Uint8 red, Uint8 green, Uint8 blue, SDLColor* oldcolors)

Here you do this for every color:

surface->format->palette->colors.r = oldcolors.r + red;
surface->format->palette->colors.g = oldcolors.r + green;
surface->format->palette->colors.b = oldcolors.r + blue;

You have to add checks to make sure the value doesn't exceed 255.

Finally you call:


Now, whenever you want to change colors, call

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!