Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Setting Transparent Color

This thread is locked; no one can reply to it. rss feed Print
Setting Transparent Color
emoremor
Member #2,667
August 2002
avatar

Hi,

I have a question -

I noticed that the default transparent color for bitmap blitting is 255,0,255 (some sort of pink) but I have always used 0,0,0 - black.

Is there a way to instruct the library to use black as transparent? I have a lot of bitmaps that I am reusing in rewrites of my games porting them to allegro and I am kind of tired of doing transparent color replacement???

Thanks for the info

kdevil
Member #1,075
March 2001
avatar

I assume you're using some color depth other than 8-bit.

I don't think there's a way to change that transparent color. You're probably better off just writing a small program that does the color replacement for you.

-----
"I am the Black Mage! I casts the spells that makes the peoples fall down!"

gnolam
Member #2,030
March 2002
avatar

Short answer: no, not unless you're using 8-bit mode (in which case palette entry 0 is always used, regardless of its color).

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Kitty Cat
Member #2,815
October 2002
avatar

No, you can't change the transparent color. However, if you're not using 8bpp, you can do a little post-loading picture editing so you won't have to change your bitmaps, but they'll behave the same:

for(y = 0;y < bmp->h;++y)
{
  for(x = 0;x < bmp->w;++x)
  {
    c = getpixel(bmp, x, y);
    if(c == makecol(255, 0, 255))
      putpixel(bmp, x, y, makecol(255, 0, 255)-1);
    else if(c == 0)
      putpixel(bmp, x, y, makecol(255, 0, 255));
  }
}

This will make any magenta pixels in the bitmap one bit darker(hardly noticeable) so they won't get masked, and make black the magic pink color Allegro wants.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

emoremor
Member #2,667
August 2002
avatar

Thanks everyone for your answers. This clarifies it completely.

Yes, I am using 16-bit. I tried 32-bit but the whole game slowed down to a halt. It seems that for 32-bit depth I should be using VRAM. I just did not want to use it in case some potential users did not have it. But I guess, nowadays, it may be rare for people not to have it.

Anyway, I can switch the colors quickly with PSP. It's just that I thought it would have been easy to do with Allegro. It's not a hard thing to do. I am not sure I understand why the library does not support selecting the transparent color...

Ceniza
Member #2,027
March 2002
avatar

Because it is possible:

1#include <allegro.h>
2 
3int main()
4{
5 BITMAP * bmp, * bmp2;
6 allegro_init();
7 set_color_depth(16);
8 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
9 install_keyboard();
10 clear_to_color(screen, makecol(255, 255, 255)); //white
11 bmp = load_bitmap("bitmap.bmp", 0);
12 bmp -> vtable -> mask_color = makecol(0, 0, 0); // <-- (here is the trick) black
13 masked_blit(bmp, screen, 0, 0, 0, 0, bmp -> w, bmp -> h);
14 destroy_bitmap(bmp);
15 readkey();
16 clear_to_color(screen, makecol(0, 255, 255)); //cyan
17 bmp2 = load_bitmap("bitmap.bmp", 0);
18 masked_blit(bmp2, screen, 0, 0, 0, 0, bmp -> w, bmp -> h);
19 destroy_bitmap(bmp2);
20 readkey();
21 return 0;
22}
23END_OF_MAIN();

I only needed to change it for bmp and it also worked fine for bmp2.

I tested it with an image in which I drew with the wrongly called magic pink color (it's light magenta) and also with black and it works as you want it to: black becomes the mask color and light magenta is just another color.

Possible problem: compatibility.

I tested it in an XP machine.

Chris Katko
Member #1,881
January 2002
avatar

Quote:

Anyway, I can switch the colors quickly with PSP. It's just that I thought it would have been easy to do with Allegro. It's not a hard thing to do. I am not sure I understand why the library does not support selecting the transparent color...

It's not "a hard thing to do" to convert it to whatever is nessecary yourself. Which is the same end result as having it set-able. They even gave you the code to do so. Cutting the work you have to do virtually down to a "cut and paste."

So could Allegro support changing the transparency color? Yes. Would it be worth the effort of recoding every single function that takes that into account? Probably not. Also, you now have a speed loss, because your using a variable in RAM, instead of a hard-coded constant.

And PSP? I assume you mean paintshop pro. Which is a professional graphics editor. Not a game programming library. So certain features are expected in graphics editors.

Lastly, your need falls under the "utility" category and therefor not nessecary the "game programming library" category. Hence, why I really doubt you'll find that in OpenGL, SDL, DirectX, etc as well.

I really hope I'm not coming off too harse.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Korval
Member #1,538
September 2001
avatar

Quote:

So could Allegro support changing the transparency color? Yes.

Maybe for memory bitmaps, but hardware accelerated video blits may be another matter. If FF00FF is hardcoded as the mask color in the hardware, then there is nothing you can do to change it.

Granted, most modern color keying hardware does allow you to select it, but it is not impossible to find hardware that doesn't allow for selecting the color key.

spellcaster
Member #1,493
September 2001
avatar

I know that this is Bob's normal explanation, but what cards actually do this? Most APIs let you set a mask color or a mask color range.

Maybe we should allow to change the color, but have a checking function that tells you if it's save or not. That checking function could create a vid bitmap set it to a non pink trans colro, do a blit and read back from the vid bitmap.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

emoremor
Member #2,667
August 2002
avatar

Etwinox:

That's fine. It is not that big of a deal. The issue was to reuse my existing artwork without doing color replacements.

In the future, I don't mind one or another color key. I use PSP as a graphic package, of course, to replace color because I can do it pretty fast and I don't need to modify my code (although I can see from the previous example that I could do that too...) Having said that, if you use DirectX you can easily specify the color key in code by setting one simple flag. That's what lead me to ask whether Allegro provided for a simple flag to set the key.

Go to: