# Color blending algorithm?

• 11-09-2005, 03:31 PM
seventoes
Color blending algorithm?
Ok, i just got pg.c working with transperency, but only fully transperent. I did that by changing a part of pgBitBlt to this:
Code:

```for (xx=0; xx<w; xx++) {         for (mx=0; mx<mag; mx++) {                 if (*dd != 0x3e0) { //added this, so if the color is 0x3e0, then it wont change the color of the vram there, thus making                         *(unsigned short *)vptr=*dd;                 }                 vptr+=PIXELSIZE*2;         }         dd++; }```

That was simple enough, but now i want to know if there is some way to add 2 colors together, for instance: Adding red (0x1f) and blue (0x7c00) to get purple (0x7c1f)

I know that i could just add them together, but that will always give me 50% transperency, and i might want different %'s so is there any way to use 2 colors and a percentage to come up with a new color?

Sorry if you dont understand this... basicly im using pg.c (16 bit colors) and i want to use runtime alpha blending.
• 11-10-2005, 02:25 AM
DeNitro
new_r = r2 + (((r1 - r2) * alpha) / 256);
new_g = g2 + (((g1 - g2) * alpha) / 256);
new_b = b2 + (((b1 - b2) * alpha) / 256);

That will blend color 1 onto color 2 at "alpha" (0-255) transparency.

There may be other alpha blending algorithms, but that should do the trick =)

Hope that helps :icon_smil

--DeNitro
• 11-10-2005, 06:43 AM
seventoes
Thanks! but idk how to get the RGB values of a hex color?

Do you know how i could get the RGB values of something direcly off of the vram? Or just from a hex value, since thats what the vram contains.
• 11-10-2005, 11:28 AM
DeNitro
Code:

```#define R( c )    ( (c & 0x1f) << 3 ) #define G( c )    ( ((c >> 5) & 0x1f ) << 3 ) #define B( c )    ( ((c >> 10) & 0x1f) << 3 )```
Those defines make an easy way to break a hex color value into individual RGB values.

So to split the color of the pixel currently pointed to by *vptr in vram in the pgBitBlt function, You could do

Code:

```r = R(*(unsigned short *)vptr); g = G(*(unsigned short *)vptr); b = B(*(unsigned short *)vptr);```
--DeNitro
• 11-10-2005, 11:32 AM
seventoes
Thanks!!! Illl try it out when i get home and let you know if it works.
• 11-10-2005, 01:05 PM
seventoes
Thanks it works! Only bad thing is that is is very slow.... :(
Can anyone think of a faster way to do this? heres what i have now:
Code:

```for (xx=0; xx<w; xx++) {     for (mx=0; mx<mag; mx++) {         if (*dd != 0x3e0) {             if (t != 255) {                 rB = getR(*(unsigned short *)vptr);                 gB = getG(*(unsigned short *)vptr);                 bB = getB(*(unsigned short *)vptr);                 rS = getR(*dd);                 gS = getG(*dd);                 bS = getB(*dd);                 new_r = rB + (((rS - rB) * t) / 256);                 new_g = gB + (((gS - gB) * t) / 256);                 new_b = bB + (((bS - bB) * t) / 256);                 *(unsigned short *)vptr = rgb2col(new_r,new_g,new_b);             }             else {                 *(unsigned short *)vptr = *dd;             }         }         vptr+=PIXELSIZE*2;     }     dd++; }```
So right now i have the code tranfering both of the color numbers into RGB values, then mixing them with the algorithm DeNitro gave me, then changing them back to a 16 bit number and storing that in the vram.

God i need to learn how the GU functions work...
• 11-10-2005, 01:38 PM
danzel
Using the Gu Functions would be faster :)
Check out the blend sample (samples/gu/blend), that should put you on the right track.

Danzel.
• 11-10-2005, 03:38 PM
seventoes
Ive looked at all of those samples but i just dont understand any of them... Could anyone explain how they work? Maybe a HEAVILY commented example on how to blit and flip one sprite?

EDIT: Well i managed to speed it up alot by adding a little thing to check weather or not the sprite will be transparent. I updated the code above to show that too. If t != 255, then it wont do all of those calculations and it runs much faster now.
• 01-23-2011, 06:25 PM
aybe
Quote:

Originally Posted by DeNitro
new_r = r2 + (((r1 - r2) * alpha) / 256);
new_g = g2 + (((g1 - g2) * alpha) / 256);
new_b = b2 + (((b1 - b2) * alpha) / 256);

That will blend color 1 onto color 2 at "alpha" (0-255) transparency.

There may be other alpha blending algorithms, but that should do the trick =)

Hope that helps :icon_smil

--DeNitro

Thanks !

:clapping: :clapping: :clapping: