Sign in to follow this  
Hachaso

Untitled

Recommended Posts

Hi! I need some help with alpha blending. What I have done, is to write 2 different functions that are supposed to return the same value. The problem is that something is different and I can't seem to figure out what. I have done it by hand to see that the calculation is correct and still nothing. Please tell me if I'm on the right track. Is that the way to do alpha blending? And also what is the difference between the two functions? Here's the code: Alpha is mapped to a value between 0 - 32. If I understand correct, alpha originally is a float containing a value between 0.0 - 1.0. So now I have mapped alpha to an unsigned char value between 0 - 32. unsigned short rgb565Blender1(unsigned short ColorA, unsigned short ColorB, unsigned char Alpha) { unsigned short res = 0; unsigned short ColorAred = 0; unsigned short ColorAgreen = 0; unsigned short ColorAblue = 0; unsigned short ColorBred = 0; unsigned short ColorBgreen = 0; unsigned short ColorBblue = 0; unsigned short resR = 0; unsigned short resG = 0; unsigned short resB = 0; ColorAred = ColorA & (0x1f << 11); ColorBred = ColorB & (0x1f << 11); ColorAgreen = ColorA & (0x3f << 5); ColorBgreen = ColorB & (0x3f << 5); ColorAblue = ColorA & 0x1f; ColorBblue = ColorB & 0x1f; resR = ((Alpha*(ColorAred - ColorBred)) >> 5) + ColorBred; resG = ((Alpha*(ColorAgreen - ColorBgreen)) >> 5) + ColorBgreen; resB = ((Alpha*(ColorAblue - ColorBblue)) >> 5) + ColorBblue; res = resR | resG | resB; return res; } Next function here with some minor differences. unsigned short rgb565Blender2(unsigned short ColorA , unsigned short ColorB , unsigned char Alpha) { unsigned short res = 0; unsigned short ColorAred = 0; unsigned short ColorAgreen = 0; unsigned short ColorAblue = 0; unsigned short ColorBred = 0; unsigned short ColorBgreen = 0; unsigned short ColorBblue = 0; unsigned short resR = 0; unsigned short resG = 0; unsigned short resB = 0; ColorAred = (ColorA >> 11) & 0x1f; ColorBred = (ColorB >> 11) & 0x1f; ColorAgreen = (ColorA >> 5) & 0x3f; ColorBgreen = (ColorB >> 5) & 0x3f; ColorAblue = ColorA & 0x1f; ColorBblue = ColorB & 0x1f; resR = (Alpha*(ColorAred - ColorBred) >> 5) + ColorBred; resG = (Alpha*(ColorAgreen - ColorBgreen) >> 5) + ColorBgreen; resB = (Alpha*(ColorAblue - ColorBblue) >> 5) + ColorBblue; resG = resG << 5; resR = resR << 11; res = resB | resG | resR; return res; } Thanks for the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this