Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » al_draw_text with alpha

Credits go to Dennis, Edgar Reynaldo, Elias, Evert, J-Gamer, and jmasterx for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
al_draw_text with alpha
Mark Oates
Member #1,146
March 2001
avatar

For some reason the concept hasn't quite sunk in with all the allegroids, yet. When this thread comes up again, nobody should mention the al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA) and/or the ALLEGRO_NO_PREMULTIPLIED_ALPHA flag as a solution. It does not "return things to how they were before." Specifically, half-opacity is gray, you get text outlines, etc.

Those two are not a working solution. (In fact, I think ALLEGRO_NO_PREMULTIPLIED_ALPHA should be removed. :-X)

The correct solution is "you'll need to multiply all your color components by the alpha value. For example:

float alpha = 0.3f;
ALLEGRO_COLOR color = al_map_rgba_f(0.7*alpha, 0.3*alpha, 0.6*alpha, alpha);

" or, the similar answer using 0-255 values.

Thank you.
:P

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

For some reason the concept hasn't quite sunk in with all the allegroids, yet.

I haven't used A5 yet, but well personally I just don't get it either.

Mark Oates said:

The correct solution is "you'll need to multiply all your color components by the alpha value. For example:

The problem is, it should work the same way whether you multiply the alpha yourself or whether Allegro multiplies it for you.

Using 'filtering' on a bitmap with alpha makes no sense to me, because then the color values in the pixels with zero alpha are used to blend the edges. So you better pick the right color values for your zero alpha pixels, or you're screwed and you get funky outlines.

So the real problem is that you can't pick sensible values for zero alpha pixels with filtering because it should be mixed with the background color instead.

I'm reading Shawn Hargreaves blogs linked from Elias's response to anomalous blending, and maybe it makes sense and maybe it doesn't. I'm not sure yet.

jmasterx
Member #11,410
October 2009

I really think a nice flag which would state weather allegro's color mapping functions will be multiplying the colors for you would be very useful.

By this I mean, if the flag is set, a call to al_map_rgba would do:

al_map_rgba(r * a, g * a, b * a, a);

and would return a pre multiplied ALLEGRO_COLOR.

Mark Oates
Member #1,146
March 2001
avatar

jmasterx said:

By this I mean, if the flag is set, a call to al_map_rgba would do:

al_map_rgba(r * a, g * a, b * a, a);

I think that's a good idea.

Right now, I use these two functions quite a bit:

#SelectExpand
1static inline ALLEGRO_COLOR color_hex(const char *hex, float a=1.0f) 2{ 3 ALLEGRO_COLOR color = al_color_html(hex); 4 color.a = a; 5 color.r *= a; 6 color.g *= a; 7 color.b *= a; 8 return color; 9} 10 11static inline ALLEGRO_COLOR color_name(const char *name, float a=1.0f) 12{ 13 ALLEGRO_COLOR color = al_color_name(name); 14 color.a = a; 15 color.r *= a; 16 color.g *= a; 17 color.b *= a; 18 return color; 19}

Some time ago, I had a convenience function:
static inline ALLEGRO_COLOR color(const char *identifier, float alpha=1.0);
that would identify identifier as a hex or name string, and would handle them accordingly. I should bring that bad boy back. 8-)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Peter Wang
Member #23
April 2000

There would be no problem adding convenience versions of al_map_rgb* which multiply in the alpha component, if only someone could suggest decent names.

For now, I create my own makecol.

jmasterx
Member #11,410
October 2009

al_map_premultiplied_rgba ? or a flag and use the same functions

Mark Oates
Member #1,146
March 2001
avatar

if only someone could suggest decent names.

I think the al_map_rgb* functions should, by default, multiply the components by the alpha.

But, if you ALLEGRO_NO_PREMULTIPLIED_ALPHA then they will not.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

jmasterx
Member #11,410
October 2009

I think the al_map_rgb* functions should, by default, multiply the components by the alpha.

But, if you ALLEGRO_NO_PREMULTIPLIED_ALPHA then they will not.

I was hoping Allegro 5.0.0 was going to work like that.

Mark Oates
Member #1,146
March 2001
avatar

I haven't completely thought through what else it could affect, but it seems to make sense. Since it's essentially what's happening to a bitmap's colors when you're loading them, it should also happen with the mapped colors.

[edit]however, I seem to recall that xma makes you multiply the components explicitly... hmm...

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Matthew Leverton
Supreme Loser
January 1999
avatar

The flag used for bitmap loading is a necessary evil. For colors, it would be another annoying thing I'd have to check for and disable in library code.

Peter Wang
Member #23
April 2000

ALLEGRO_NO_PREMULTIPLIED_ALPHA is only a bitmap flag that affects how allegro_image loads images. It has no other bearing. The rest of Allegro really doesn't care if you use the convention or not, which is all it is.

Mark Oates
Member #1,146
March 2001
avatar

ALLEGRO_NO_PREMULTIPLIED_ALPHA is only a bitmap flag that affects how allegro_image loads images. It has no other bearing. The rest of Allegro really doesn't care if you use the convention or not, which is all it is.

Hmm... I see...

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Evert
Member #794
November 2000
avatar

jmasterx said:

I really think a nice flag which would state weather allegro's color mapping functions will be multiplying the colors for you would be very useful.

It could potentially slow you down quite a bit as well, if you're doing an extra comparison in a tight loop.

There would be no problem adding convenience versions of al_map_rgb* which multiply in the alpha component, if only someone could suggest decent names.

Agreed.
How about al_map_alpha_rgba()?
By the way, having functions that converts from non-pre-multiplied alpha to pre-multiplied alpha and back would be useful too.

Elias
Member #358
May 2000

Evert said:

How about al_map_alpha_rgba()?

Or maybe al_map_premultipled_rgba(). In both cases you likely want to keep your custom color functions/macros though so not sure it's necessary.

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

 1   2 


Go to: