Public Group

# Untitled

This topic is 4401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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!

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 12
• 15
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632655
• Total Posts
3007675
• ### Who's Online (See full list)

There are no registered users currently online

×