# Setcolorkey

**URL:** <https://discourse.libsdl.org/t/setcolorkey/6248>\
**Category:** SDL Development\
**Created:** [March 21, 2002, 5:55am UTC](https://discourse.libsdl.org/t/setcolorkey/6248 "2002-03-21T05:55:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Perrine](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@Richard\_Perrine](https://discourse.libsdl.org/u/Richard_Perrine)\
**Post date:** [March 21, 2002, 5:55am UTC](https://discourse.libsdl.org/t/setcolorkey/6248/1 "2002-03-21T05:55:01Z")

</div>

Hi,

The SetColorKey function seems pretty straight forward, but my blitted bitmaps still show the black area around the actual image of the bmp. I set the colorkey to black, ie. SDL\_SetColorKey(bmp,SDL\_SRCCOLORKEY|SDL\_RLEACCEL,RGB(0,0,0));

I also have the alpha active, ie. SDL\_SetAlpha(bmp, SDL\_SRCALPHA|SDL\_RLEACCEL, 255);

What am i doing wrong? It obviously looks great when the bmp goes over black areas, but when it goes over colored areas, the black area of the bmp boundary is revealed. Are there other issues that I need to pay close attention to in order for the bmp to show only the actual image w/o the boundary? Eg. updating the screen, updating scene, etc.

THanks again. I truly am grateful for all your help. I hope to be as knowledgeable about SDL with your help.

richard---------------------------------  
Do You Yahoo!?  
Yahoo! Movies - coverage of the 74th Academy Awards?

---

<div class="post-metadata">

**Author:** ![Karl\_Bartel](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/f05b48/32.png) [@Karl\_Bartel](https://discourse.libsdl.org/u/Karl_Bartel)\
**Post date:** [March 21, 2002, 6:02am UTC](https://discourse.libsdl.org/t/setcolorkey/6248/2 "2002-03-21T06:02:01Z")

</div>

> The SetColorKey function seems pretty straight forward, but my blitted  
> bitmaps still show the black area around the actual image of the bmp. I  
> set the colorkey to black, ie.  
> SDL\_SetColorKey(bmp,SDL\_SRCCOLORKEY|SDL\_RLEACCEL,RGB(0,0,0));
> 
> I also have the alpha active, ie. SDL\_SetAlpha(bmp,  
> SDL\_SRCALPHA|SDL\_RLEACCEL, 255);

This could be the problem. AFAIK, you can only use EITHER alpha values  
OR a color key, not both at the same time. If this doesn help, send me  
an example file and I’ll have a look at it.

Bye,

– Karl

---

<div class="post-metadata">

**Author:** ![Jason\_Hoffoss](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@Jason\_Hoffoss](https://discourse.libsdl.org/u/Jason_Hoffoss)\
**Post date:** [March 21, 2002, 6:17am UTC](https://discourse.libsdl.org/t/setcolorkey/6248/3 "2002-03-21T06:17:00Z")

</div>

Hi,

The SetColorKey function seems pretty straight forward, but my blitted bitmaps still show the black area around the actual image of the bmp. I set the colorkey to black, ie. SDL\_SetColorKey(bmp,SDL\_SRCCOLORKEY|SDL\_RLEACCEL,RGB(0,0,0));

Not sure what this RGB macro or function is, but you want to make sure you map a color in the bmp’s colorspace, rather than the screen’s colorspace. They each have a seperate set of colors, so using the screen’s black color may not match what is black in the bmp.

I just had the same sort of problem earlier today, and this was what I was doing wrong.

-Jason----- Original Message -----  
From: Richard Perrine  
To: SDLgroup  
Sent: Thursday, March 21, 2002 8:54 AM  
Subject: [SDL] setcolorkey

---

<div class="post-metadata">

**Author:** ![Richard\_Perrine](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@Richard\_Perrine](https://discourse.libsdl.org/u/Richard_Perrine)\
**Post date:** [March 21, 2002, 9:13am UTC](https://discourse.libsdl.org/t/setcolorkey/6248/4 "2002-03-21T09:13:00Z")

</div>

Thanks jason!

That makes perfect sense to me. I’ll give a try.

richard

— Jason Hoffoss wrote:\> ----- Original Message -----

> From: Richard Perrine  
> To: SDLgroup  
> Sent: Thursday, March 21, 2002 8:54 AM  
> Subject: [SDL] setcolorkey
> 
> Hi,
> 
> The SetColorKey function seems pretty straight  
> forward, but my blitted bitmaps still show the black  
> area around the actual image of the bmp. I set the  
> colorkey to black, ie.

SDL\_SetColorKey(bmp,SDL\_SRCCOLORKEY|SDL\_RLEACCEL,RGB(0,0,0));

> Not sure what this RGB macro or function is, but you  
> want to make sure you map a color in the bmp’s  
> colorspace, rather than the screen’s colorspace.  
> They each have a seperate set of colors, so using  
> the screen’s black color may not match what is black  
> in the bmp.
> 
> I just had the same sort of problem earlier today,  
> and this was what I was doing wrong.
> 
> -Jason

* * *

Do You Yahoo!?  
Yahoo! Movies - coverage of the 74th Academy Awards?

> **[Latest Movie News and Updates, plus Interviews, Reviews, and More - Yahoo...](https://www.yahoo.com/entertainment/movies/)**
>
> The latest and breaking movie news from Yahoo Entertainment including new releases, trailers, reactions, interviews, and more.

---

<div class="post-metadata">

**Author:** ![Richard\_Perrine](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@Richard\_Perrine](https://discourse.libsdl.org/u/Richard_Perrine)\
**Post date:** [March 21, 2002, 12:00pm UTC](https://discourse.libsdl.org/t/setcolorkey/6248/5 "2002-03-21T12:00:03Z")

</div>

Jason,  
Still not getting it. What did you do to solve the problem? By the way, what did you mean that you were not sure what the RGB macro or function was? Should the colorkey value be a single integer? Could you give an example of the SetColorKey function in a program that successfully blits a bmp with transparency?  
Thanks again!  
Richard  
Jason Hoffoss wrote: ----- Original Message ----- From: Richard Perrine To: SDLgroup Sent: Thursday, March 21, 2002 8:54 AMSubject: [SDL] setcolorkey

Hi,

The SetColorKey function seems pretty straight forward, but my blitted bitmaps still show the black area around the actual image of the bmp. I set the colorkey to black, ie. SDL\_SetColorKey(bmp,SDL\_SRCCOLORKEY|SDL\_RLEACCEL,RGB(0,0,0));

Not sure what this RGB macro or function is, but you want to make sure you map a color in the bmp’s colorspace, rather than the screen’s colorspace. They each have a seperate set of colors, so using the screen’s black color may not match what is black in the bmp.

I just had the same sort of problem earlier today, and this was what I was doing wrong.

-Jason---------------------------------  
Do You Yahoo!?  
Yahoo! Movies - coverage of the 74th Academy Awards?

---

<div class="post-metadata">

**Author:** ![Jason\_Hoffoss](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@Jason\_Hoffoss](https://discourse.libsdl.org/u/Jason_Hoffoss)\
**Post date:** [March 21, 2002, 12:35pm UTC](https://discourse.libsdl.org/t/setcolorkey/6248/6 "2002-03-21T12:35:01Z")

</div>

Here’s the code I had that had the problem, but had fixed:

SDL\_Surface \*load\_image(const char \*pathname)  
{  
char buf[1024];  
Uint32 blue;  
SDL\_Surface \*surface, \*temp;

sprintf(buf, “images/%s”, pathname);  
temp = IMG\_Load(buf);  
if (!temp)  
fatal\_error(“Could not load %s!”, buf);

blue = SDL\_MapRGB(temp-\>format, 0, 0, 255);  
SDL\_SetColorKey(temp, SDL\_SRCCOLORKEY | SDL\_RLEACCEL, blue);  
surface = SDL\_DisplayFormat(temp);  
SDL\_FreeSurface(temp);

return surface;  
}

Previously, I was using the global blue variable, which was generated by this line elsewhere (in my init() function):

blue = SDL\_MapRGB(screen-\>format, 0x00, 0x00, 0xff);

Problem was that the screen colors apparently didn’t match my image colors. They would in ‘surface’ after the SDL\_DisplayFormat(temp) call, of course. So my global blue above mapped to the value 255, but the blue that mapped to “temp” was 4 million and something. A very different number.

Anyway, use the above function, or a variation on it, and it should work. The transparent color here is full blue, but you said you use black, so make that 255 a 0 instead.

-Jason----- Original Message -----  
From: Richard Perrine  
To: sdl at [libsdl.org](http://libsdl.org)  
Sent: Thursday, March 21, 2002 2:59 PM  
Subject: Re: [SDL] setcolorkey

Jason,

Still not getting it. What did you do to solve the problem? By the way, what did you mean that you were not sure what the RGB macro or function was? Should the colorkey value be a single integer? Could you give an example of the SetColorKey function in a program that successfully blits a bmp with transparency?

Thanks again!

Richard

```
Jason Hoffoss <jason at hoffoss.com> wrote: 

  ----- Original Message ----- 
  From: Richard Perrine 
  To: SDLgroup 
  Sent: Thursday, March 21, 2002 8:54 AM
  Subject: [SDL] setcolorkey

  Hi,

  The SetColorKey function seems pretty straight forward, but my blitted bitmaps still show the black area around the actual image of the bmp. I set the colorkey to black, ie. SDL_SetColorKey(bmp,SDL_SRCCOLORKEY|SDL_RLEACCEL,RGB(0,0,0));

Not sure what this RGB macro or function is, but you want to make sure you map a color in the bmp's colorspace, rather than the screen's colorspace. They each have a seperate set of colors, so using the screen's black color may not match what is black in the bmp.

I just had the same sort of problem earlier today, and this was what I was doing wrong.

   -Jason

```

* * *

Do You Yahoo!?  
Yahoo! Movies - coverage of the 74th Academy Awards?
