Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Is there any way to tint a bitmap white?

This thread is locked; no one can reply to it. rss feed Print
Is there any way to tint a bitmap white?
AleaInfinitus
Member #18,873
December 2020

I know that you can darken a bitmap with al_draw_tinted_bitmap if you turn down all color values equally, but is there any way to do the opposite? Or is my only option to draw a slightly transparent white bitmap on top of the main one?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

William Labbett
Member #4,486
March 2004
avatar

Presumably you decrease each r, g, b value by some fixed percentage (ratio) to
make an image darker. You could take

255 - r, 255 - g, and 255 - g and add a percentage of each of these to r, g, and b respectively to do the opposite.

dthompson
Member #5,749
April 2005
avatar

The most scalable way to do this is with a pixel shader. The setup is a bit long-winded though.

Here's an example with GLSL:

#SelectExpand
1uniform sampler2D al_tex; 2uniform bool al_use_tex; 3uniform vec4 tint; 4varying vec4 varying_color; 5varying vec2 varying_texcoord; 6 7void main() 8{ 9 vec4 t; 10 11 if(al_use_tex) 12 t = texture2D(al_tex, varying_texcoord); 13 else 14 t = varying_color; 15 16 gl_FragColor = t + tint; 17}

To use this with Allegro 5:

#SelectExpand
1// firstly, before you create your display, you'll need to add this line: 2al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_PROGRAMMABLE_PIPELINE); 3 4// you'll then need to load the above GLSL code somehow, maybe from a text file. 5// here, I'm assuming you've loaded it into a string called 'source'. 6 7ALLEGRO_SHADER* shader; 8shader = al_create_shader(ALLEGRO_SHADER_AUTO); 9 10al_attach_shader_source(shader, ALLEGRO_VERTEX_SHADER, al_get_default_shader_source(ALLEGRO_SHADER_AUTO, ALLEGRO_VERTEX_SHADER)); 11al_attach_shader_source(shader, ALLEGRO_PIXEL_SHADER, source); 12al_build_shader(shader); 13 14al_use_shader(shader); 15 16// set the tint to { 1, 1, 1, 0 }. 17// this will make every pixel of your bitmap white. 18al_set_shader_float_vector("tint", 4, (float[]){ 1, 1, 1, 0 }, 1); 19 20// now draw your bitmap(s).

Ideally, you'll also want to check the return values of al_create_shader, al_attach_shader_source and al_build_shader (as they can fail).

______________________________________________________
Website. It was freakdesign.bafsoft.net.
This isn't a game!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Additive blending is NOT what you want here.

You can use the formula William gave to tint white, or any other color. Just add a percentage of the difference between colors to the original pixel.

Go to: