# Textures and color keys

**URL:** <https://discourse.libsdl.org/t/textures-and-color-keys/16440>\
**Category:** SDL Development\
**Created:** [July 4, 2009, 7:41pm UTC](https://discourse.libsdl.org/t/textures-and-color-keys/16440 "2009-07-04T19:41:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mason\_Wheeler](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@Mason\_Wheeler](https://discourse.libsdl.org/u/Mason_Wheeler)\
**Post date:** [July 4, 2009, 7:41pm UTC](https://discourse.libsdl.org/t/textures-and-color-keys/16440/1 "2009-07-04T19:41:34Z")

</div>

Can a texture have a color key? If so, how do you set it and retrieve its value?

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [August 7, 2009, 9:56am UTC](https://discourse.libsdl.org/t/textures-and-color-keys/16440/2 "2009-08-07T09:56:19Z")

</div>

Textures don’t have colorkey, instead you should create a texture with an  
alpha channel.  
You can take a look at SDL\_ConvertColorkeyToAlpha() in SDL\_surface.c for an  
example.On Sat, Jul 4, 2009 at 12:41 PM, Mason Wheeler wrote:

> Can a texture have a color key? If so, how do you set it and retrieve its  
> value?
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

–  
-Sam Lantinga, Founder and President, Galaxy Gameworks LLC

---

<div class="post-metadata">

**Author:** ![Donny\_Viszneki](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/donny_viszneki/32/360_2.png) [@Donny\_Viszneki](https://discourse.libsdl.org/u/Donny_Viszneki)\
**Post date:** [August 7, 2009, 4:06pm UTC](https://discourse.libsdl.org/t/textures-and-color-keys/16440/3 "2009-08-07T16:06:51Z")

</div>

You could use a fragment shader to achieve the desired effect as well.On Fri, Aug 7, 2009 at 5:56 AM, Sam Lantinga wrote:

> Textures don’t have colorkey, instead you should create a texture with an  
> alpha channel.  
> You can take a look at SDL\_ConvertColorkeyToAlpha() in SDL\_surface.c for an  
> example.

–  
[http://codebad.com/](http://codebad.com/)

---

<div class="post-metadata">

**Author:** ![Mason\_Wheeler](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@Mason\_Wheeler](https://discourse.libsdl.org/u/Mason_Wheeler)\
**Post date:** [August 7, 2009, 4:56pm UTC](https://discourse.libsdl.org/t/textures-and-color-keys/16440/4 "2009-08-07T16:56:07Z")

</div>

Three problems with that.

One, the abstraction inversions implied by such a course of action make me  
shudder just thinking about it.

Two, this would require me to keep track of the colorkey value of every single  
image file I load (they’re not always the same, and this is a factor beyond my  
control), find a way of associating it with the textures I create, and passing it  
to the shader every time.

Three, I don’t think SDL has any shader APIs. That would be a definite value  
add, though…\> ----- Original Message -----

> From: [donny.viszneki@gmail.com](mailto:donny.viszneki@gmail.com) (Donny Viszneki)  
> Subject: Re: [SDL] Textures and color keys
> 
> On Fri, Aug 7, 2009 at 5:56 AM, Sam Lantinga wrote:
> 
> > Textures don’t have colorkey, instead you should create a texture with an  
> > alpha channel.  
> > You can take a look at SDL\_ConvertColorkeyToAlpha() in SDL\_surface.c for an  
> > example.
> 
> You could use a fragment shader to achieve the desired effect as well.

---

<div class="post-metadata">

**Author:** ![Donny\_Viszneki](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/donny_viszneki/32/360_2.png) [@Donny\_Viszneki](https://discourse.libsdl.org/u/Donny_Viszneki)\
**Post date:** [August 7, 2009, 5:26pm UTC](https://discourse.libsdl.org/t/textures-and-color-keys/16440/5 "2009-08-07T17:26:24Z")

</div>

While IMHO these are all whiny complaints that shouldn’t stop anyone  
from cutting their texture memory allocation by 25% (eliminating the  
alpha channel) I didn’t say this couldn’t be implemented in SDL, as  
opposed to on top of the existing SDL API ;)On Fri, Aug 7, 2009 at 12:56 PM, Mason Wheeler wrote:

> Three problems with that.
> 
> One, the abstraction inversions implied by such a course of action make me  
> shudder just thinking about it.
> 
> Two, this would require me to keep track of the colorkey value of every single  
> image file I load (they’re not always the same, and this is a factor beyond my  
> control), find a way of associating it with the textures I create, and passing it  
> to the shader every time.
> 
> Three, I don’t think SDL has any shader APIs. That would be a definite value  
> add, though…

–  
[http://codebad.com/](http://codebad.com/)

---

<div class="post-metadata">

**Author:** ![Mason\_Wheeler](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@Mason\_Wheeler](https://discourse.libsdl.org/u/Mason_Wheeler)\
**Post date:** [August 7, 2009, 5:40pm UTC](https://discourse.libsdl.org/t/textures-and-color-keys/16440/6 "2009-08-07T17:40:15Z")

</div>

> > Three problems with that.
> > 
> > One, the abstraction inversions implied by such a course of action make me  
> > shudder just thinking about it.
> > 
> > Two, this would require me to keep track of the colorkey value of every single  
> > image file I load (they’re not always the same, and this is a factor beyond my  
> > control), find a way of associating it with the textures I create, and passing it  
> > to the shader every time.
> > 
> > Three, I don’t think SDL has any shader APIs. That would be a definite value  
> > add, though…
> 
> While IMHO these are all whiny complaints that shouldn’t stop anyone  
> from cutting their texture memory allocation by 25% (eliminating the  
> alpha channel) I didn’t say this couldn’t be implemented in SDL, as  
> opposed to on top of the existing SDL API 😉

That would require a bit of reworking. Right now, as far as I can tell, there’s no  
way to create an RGB-only texture.\>----- Original Message ----

> From: Donny Viszneki \<donny.viszneki at [gmail.com](http://gmail.com)\>  
> Subject: Re: [SDL] Textures and color keys  
> On Fri, Aug 7, 2009 at 12:56 PM, Mason Wheeler\<@Mason\_Wheeler\> wrote:
