Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Alpha Transparency

This thread is locked; no one can reply to it. rss feed Print
Alpha Transparency
InfiniteLoop
Member #8,434
March 2007

Hello again everyone,

I am noticing that alpha is not working the way I figured it would. It seems I cannot notice my alpha changes at all when my background is black. Doing a little searching, I discovered that it is probably due to the blending mode. Here is what I am doing:

at some point, the user will set the RGBA for a sprites color

//arbitrary example
this->color = al_map_rgba(255, 0, 0, 100);

later, in the draw method

al_draw_tinted_bitmap_region(this->image, this->color, etc...);

What do I need to do to get this to render with alpha in a straight forward manner?

Thank you

Elias
Member #358
May 2000

It seems I cannot notice my alpha changes at all when my background is black.

Which background? Do you mean black pixels in your bitmap? Your example should make them get drawn transparently already. Maybe you can post a screenshot of the bitmap and how it looks and how you want it to look instead.

Also in general you probably want to multiply the RGB components in your color with the alpha component when using the default blending mode: this->color = al_map_rgba(255 * 100 / 255, 0, 0, 100); But it depends on what you want to do.

--
"Either help out or stop whining" - Evert

InfiniteLoop
Member #8,434
March 2007

That background is black (as in clear_to_color(0, 0, 0)). My code would make my images appear transparent if there was anything behind them. One would imagine that a red block drawn at half opacity over a black background would appear much darker, but in my program, it appears bright red until something is drawn behind it.

Also, given the way I read in color and handle my function call, I do not have direct access to the alpha component (or any individual component). Instead, I just have an ALLEGRO_COLOR object. Not sure how to do what you are suggesting.

I cannot get you screenshots until tonight when I am home again. I am hoping someone can share some insight before then though.

Elias
Member #358
May 2000

Try something like this:

ALLEGRO_COLOR premul_alpha(ALLEGRO_COLOR color) {
    float r, g, b, a;
    al_unmap_rgba_f(color, &r, &g, &b, &a);
    return al_map_rgba_f(r * a, g * a, b * a, a);
}

and then draw like this:

al_draw_tinted_bitmap(bitmap, premul_alpha(color), ...);

--
"Either help out or stop whining" - Evert

InfiniteLoop
Member #8,434
March 2007

Thank you, I will give this a whirl when I get home. While I wait, why is it I have to do this? Is this a feature?

Elias
Member #358
May 2000

The default blending mode is al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA) - so basically the source RGB is added to the RGB already on the target. Therefore to get transparency you need to multiply it first.

ex_premul_alpha should explain why this is the default blending mode.

And al_map_rgba does not do the multiplication because it probably would be confusing.

--
"Either help out or stop whining" - Evert

InfiniteLoop
Member #8,434
March 2007

Excellent. That answer is exactly what I was looking for.

Thank you

EDIT:
This worked. Thank you.

Go to: