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
AMCerasoli
Member #11,955
May 2010
avatar

There is a way to draw text with "al_draw_text" with Alpha? I tried:

al_draw_text(font, al_map_rgba(255, 255 ,255 ,120), 635, 645, 0, "aloha");

But didn't work, there is a way? or should I use bitmap fonts?

Conclusion:

Allegro Uses pre-multiplied alpha by default al_map_rgba works great with bitmaps but doesn't seems to work with text.

al_map_rgba_f works fine with text, but instead of use a parameter number from 0-255 for RBG colors uses a ranging from 0.0f-1.0f.

So if you wan to convert a color from 0-255 to 0.0f-1.0f you need to use division.

Ex: normal RGB (180,120,100,127.5)
180/255=0.705
120/255=0.470
100/255=0.392
127/255=0.5

Would be:
Allegro RGB al_map_rgba_f(0.705,0.470,0.392.0.5)

And if you need convert from 0.0f-1.0f to 0-255 you need to use uses multiplication.

Ex: normal RGB (0.705,0.470,0.392,0.5)
0.705*255=180
0.470*255=120
0.392*255=100
0.5*255=127.5

jmasterx
Member #11,410
October 2009

It should work its just pre multiplied alpha is on by default so at the top of your render loop try:

al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);

http://docs.liballeg.org/graphics.html#al_set_blender

AMCerasoli
Member #11,955
May 2010
avatar

Thanks man, now is working!

But, when it says: "Sets the function to use for blending for the current thread" what is saying?

The current tread is the whole tread right? because I saw something about "threading interface" which allows me to do multi threading? but if I'm not using it there is just one thread, right?

jmasterx
Member #11,410
October 2009

What they mean I think is that it affects all of your displays. If you have many displays, you must set blender for each one.

void render()
{
  //set display
  //set blender
  //draw

  //set display2
  //set blender
  //draw
}

But if you only have 1 display (1 window) you don't have to worry about it.

AMCerasoli
Member #11,955
May 2010
avatar

Oh I see.

But have just notice this problem:

I put al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
before all the drawing calls.

{"name":"603492","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/e\/2e1ea87d9d52d4ffa11dfc586179bb35.jpg","w":505,"h":718,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/e\/2e1ea87d9d52d4ffa11dfc586179bb35"}603492

Should be like the PhotoShop version.
If I remove al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA); it works but again can't draw transparent text. :o

I'm using: al_draw_tinted_bitmap(PNG, al_map_rgba(255, 255, 255, 255), 0, 0, 0 );

it's a PNG which already have transparency.

jmasterx
Member #11,410
October 2009

This is because Photoshop premultiplies Alpha for Bitmaps so try to set it back to premultiplied before drawing bitmaps and before loading bitmaps, but use the other blender to draw text.

Elias
Member #358
May 2000

Or premultiply the color you draw the text with then you never need to touch the blender.

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

AMCerasoli
Member #11,955
May 2010
avatar

But by default Allegro is also premultiplaying alpha right?

Because if I don't modify the blending, Allegro draw it the same as Photoshop does.

"premultiply the color you draw the text" sounds better, that doesn't mean that if I premultiply the white color, all the bitmaps that are using the white color are going to be affected?

1) How can I premultiply just one color? (if that is what you're suggesting)
2) Isn't changing the blending, CPU expensive?
3) By default, Allegro premultiply alpha?

Edgar Reynaldo
Member #8,592
May 2007
avatar

1) How can I premultiply just one color? (if that is what you're suggesting)
2) Isn't changing the blending, CPU expensive?
3) By default, Allegro premultiply alpha?

1) Use al_map_rgba_f(255.0f*0.5f , 127.0f*0.5f , 64.0f*0.5f , 0.5f). Premultiplying the alpha means multiplying it yourself, which makes no sense to me.
2) Probably not. I'm guessing it just sets some flags.
3) Yes

Dennis
Member #1,090
July 2003
avatar

But by default Allegro is also premultiplaying alpha right?

Last time I checked, Allegro had no multiplayer functions at all. :P

Premultiplied alpha means this:
The individual RGB components of any pixel source data are interpreted in the alpha blending step as having already been multiplied by some alpha value, so the alpha blending step does not multiply the source RGB components with the source A component anymore to determine what gets blended with the RGB components of the target pixel data.

Additional reading:
http://en.wikipedia.org/wiki/Alpha_compositing
A5 specific: http://docs.liballeg.org/graphics.html#al_set_blender
Look at those formulas and the description below. Do the math and feel enlightened afterwards (Note how the A component of the source color is still important for calculating the amount of each RGB value from the target color in the alpha blending step.).

AMCerasoli
Member #11,955
May 2010
avatar

;D;D;D hahahahaha

That would be good a Multiplayer function!!.

You just have to call al_set_multiplayer(ALLEGRO_PC * pc); and that's it.

I would like to do it the Elias way. But I don't even know what means the 'f' letter after the numbers. Can someone drop a link? I don't know how to search (google:"the "f" letter after a number c++"?), it's something related with Hexadecimals right?

The al_map_rgba_f function that Edgar gave me, seems to instead of get transparent, become black (when I modify it), because if I use it as he gave it to me, the text is drawn blue. This surely is because I don't know how to use it. I never saw anything about that in my C++ Book. :P

Edgar Reynaldo
Member #8,592
May 2007
avatar

Sorry, I used al_map_rgba_f wrong. The values go from 0.0 to 1.0, not 0 to 255.

al_map_rgba_f(1.0f*0.5f , 0.5f*0.5f , 0.25f*0.5f , 0.5f);

Would give you a slightly desaturated orange at 50% opacity.

The _f at the end of al_map_rgba_f just means it takes floating point values. The f at the end of the number values means they are floating point values.

AMCerasoli
Member #11,955
May 2010
avatar

But why are you multiplying all that? isn't the same: al_map_rgba_f(0.5f , 0.25f , 0.125 , 0.5f) well actually it is, I tried it.

I like this way since I don't have to use blending. But if I do al_map_rgba_f(0.5f , 0.25f , 0.125 , 0.0f) It gets white instead of transparent. I have been reading and that is one of the difference between premultiply and the other way.

But then what I have to do?, changing the whole blending mode it seems unnecessary I don't know.

I want to make an "appearing effect". from completely invisible to visible.

PS: Oh boy, jmasterx already have told me that premultiplied is on by default, and I asked again, I didn't see it :o:o. I didn't process correctly the info :-X.

Evert
Member #794
November 2000
avatar

But why are you multiplying all that? isn't the same: al_map_rgba_f(0.5f , 0.25f , 0.125 , 0.5f) well actually it is, I tried it.

Of course it's the same.
But first, it's an example, and second, doing the multiplication explicitly can be clearer. It's evaluated at compile-time anyway.

AMCerasoli
Member #11,955
May 2010
avatar

I did change the values with al_set_blender and then set it back, but I'm getting this effect, and I really don't like it.

{"name":"603498","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/e\/0e0e895fbf9444353e9b5164277536a4.jpg","w":243,"h":159,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/e\/0e0e895fbf9444353e9b5164277536a4"}603498

That black border.

How does work al_map_rgb_f?

Can someone show me an example drawing text completely transparent with al_map_rgb_f? no matter what color it's.

Isn't as simple as used to... the unique way I can make it complytly transparent is doing this: al_map_rgba_f(0.0f , 0.0f , 0.0f , 0.0f), the thing is now I don't know how to control those variables... RGB are in most applications, but know what can I use as reference?

Dennis
Member #1,090
July 2003
avatar

..but know what can I use as reference?

Yes, the manual: http://docs.liballeg.org/graphics.html#al_set_blender
and http://docs.liballeg.org/graphics.html#al_map_rgb_f
It explains what's happening in detail. Also, read my previous post (and the links I provided).

Be patient. Read those sections of the manual slowly and make sure you understand each sentence before reading the next one. If there are any words you don't understand, look them up.

Elias
Member #358
May 2000

It's very simple:

al_map_rgba_f(0, 0, 0, 0) // 0% opaque (fully transparent)
al_map_rgba_f(0.25, 0.25, 0.25, 0.25) // 25% opaque
al_map_rgba_f(0.50, 0.50, 0.50, 0.50) // 50% opaque (half transparent)
al_map_rgba_f(1, 1, 1, 1) // 100% opaque (default, no transparency)

This is when using the default blending mode only of course.

[edit: The black border will appear if you change to non-pre-multiplied alpha, so don't do that. Leave everything at the defaults unless you know what you are doing.]

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

Elias said:

The black border will appear if you change to non-pre-multiplied alpha, so don't do that.

Why would there be a black border if you're not using pre multiplied alpha? Shouldn't the outcome be the same as if you were using premultiplied alpha?

Dennis
Member #1,090
July 2003
avatar

Elias said:

Leave everything at the defaults unless you know what you are doing.

..and to reach a point where you know what you're doing, it helps to read and understand manuals, to look up unknown words and to seek more details about a topic and not just copy & pasting examples.

Elias
Member #358
May 2000

Why would there be a black border if you're not using pre multiplied alpha? Shouldn't the outcome be the same as if you were using premultiplied alpha?

Not if you use any kind of filtering (which I assume he's doing, don't see how there could be dark borders otherwise) - and that's precisely the reason why we switched to using pre-multipled alpha. Linear-interpolation-alpha just doesn't work with the way OpenGL/DirectX do interpolation.

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

AMCerasoli
Member #11,955
May 2010
avatar

Black borders appear when I'm not using pre-multiplied alpha. But anyway thack a lot Elias that examples help me a lot, I know that I gonna have to read, what Dennis gave me, but I already solve my problem.

The thing is now I have no reference, if I see for example a text in HTML/PhotoShop/etc, which use simple RGB, with the maximum amount of color been always 255.

What I have to do to transform that to this type of RGB, when the maximum amount of color is 1.0?. I haven't read the links above, so if I'm saying something that I can find there, just don't even bother...

J-Gamer
Member #12,491
January 2011
avatar

Just multiply the value between 0.0 and 1.0 with 255 and there you go ;D
The other way around: just divide by 255.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

jmasterx
Member #11,410
October 2009

N/A

J-Gamer
Member #12,491
January 2011
avatar

Err... jmaster, it's what I said... With that part of the phrase, I meant when going from photoshop/inkscape/... values to allegro values. So here is something clearer.

When converting from the allegro values to the photoshop/inkscape/... values, multiply with 255.
When going from the photoshop/inkscape/... to allegro values divide by 255.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

AMCerasoli
Member #11,955
May 2010
avatar

Thanks to all problem solved! :D

 1   2 


Go to: