My own Alpha blending function, is it correct?
Posted: Sep 4, 2006 @ 1:35pm
Hi!
I'm quite new at this so I was wondering if someone could please explain to me how alpha blending is done, from a programmers point of view.
Here's is what I have done. Don't know if I'm on the right track to begin with. Could you please explain to me what the errors are. I wrote this code without any optimization so that I could better understand how it works.
// Alpha is mapped to an unsigned char value of 0 - 32.
unsigned short alpha_blend(unsigned short ColorA, unsigned short ColorB, unsigned char Alpha)
{
unsigned short res = 0; // destination color
unsigned short colorAred = 0;
unsigned short colorAgreen = 0;
unsigned short colorAblue = 0;
unsigned short colorBred = 0;
unsigned short colorBgreen = 0;
unsigned short colorBblue = 0;
double step;
step = 0,03125; // is the result of 1 divided by 32. Result from the mapping
ucAlpha *= step;
// Select color bits to work with.
colorAred = uiA & 0xF800;
colorBred = uiB & 0xF800;
colorAgreen = uiA & 0x7E0;
colorBgreen = uiB & 0x7E0;
colorAblue = uiA & 0x1F;
colorBblue = uiB & 0x1F;
colorAred = colorAred * ucAlpha + colorBred * (1.0 - ucAlpha); // formula for calculating alpha blending
colorAgreen = colorAgreen * ucAlpha + colorBgreen * (1.0 - ucAlpha);
colorAblue = colorAblue * ucAlpha + colorBblue * (1.0 - ucAlpha);
res = (colorAred | colorAgreen) | colorAblue;
return res;
}
If I have it right one shoule use the alpha blending formula on each RGB color, one for Red, Green and Blue. Is this correct?
I'm quite new at this so I was wondering if someone could please explain to me how alpha blending is done, from a programmers point of view.
Here's is what I have done. Don't know if I'm on the right track to begin with. Could you please explain to me what the errors are. I wrote this code without any optimization so that I could better understand how it works.
// Alpha is mapped to an unsigned char value of 0 - 32.
unsigned short alpha_blend(unsigned short ColorA, unsigned short ColorB, unsigned char Alpha)
{
unsigned short res = 0; // destination color
unsigned short colorAred = 0;
unsigned short colorAgreen = 0;
unsigned short colorAblue = 0;
unsigned short colorBred = 0;
unsigned short colorBgreen = 0;
unsigned short colorBblue = 0;
double step;
step = 0,03125; // is the result of 1 divided by 32. Result from the mapping
ucAlpha *= step;
// Select color bits to work with.
colorAred = uiA & 0xF800;
colorBred = uiB & 0xF800;
colorAgreen = uiA & 0x7E0;
colorBgreen = uiB & 0x7E0;
colorAblue = uiA & 0x1F;
colorBblue = uiB & 0x1F;
colorAred = colorAred * ucAlpha + colorBred * (1.0 - ucAlpha); // formula for calculating alpha blending
colorAgreen = colorAgreen * ucAlpha + colorBgreen * (1.0 - ucAlpha);
colorAblue = colorAblue * ucAlpha + colorBblue * (1.0 - ucAlpha);
res = (colorAred | colorAgreen) | colorAblue;
return res;
}
If I have it right one shoule use the alpha blending formula on each RGB color, one for Red, Green and Blue. Is this correct?