Skip to main content
GameDev.net gamedev.net
🔒 Locked

Alpha Transparency

Started by andrew_j_w Jan 7, 2003 at 6:25 AM 4 replies 400+ views
Original Post
andrew_j_w
andrew_j_w
Hi, I''m trying to manually merge two images with alpha transparency, but I can''t come up with a formula that looks good. I''ve got as far as R = Rs*(1-An) + Rn*An G = Gs*(1-An) + Gn*An B = Bs*(1-An) + Bn*An where Rs,Gs,Bs are the colurs of the bottom image and Rn,Gb,Bn are the colours of the top image. An is the alpha value (0..1) for the top image. This works fine, but I can''t get a formula to give me a final alpha for the combined image. What should the alpha value be for two pixels with 0.5 as their alpha value? What about 0.75 and 0.3? Any help would be apprciated, Andrew
edotorpedo
edotorpedo

Your formula seems to be the correct formula. About the alpha value of two pixels, the most logical would be:

(AlphaPixel1 + AlphaPixel2) / 2

so alpha = (0.75 + 0.3) / 2 = 0.525

Have you tried this already?

Edo
Edo
andrew_j_w
andrew_j_w
The problem with that is if the bottom pixel has an alpha value of 1.0 and the top pixel has a value of 0.5 then the final alpha value would be 0.75, not 1.0.

I hope this makes my problem clearer,
Andrew
twanvl
twanvl
What about:
1.0 - ((1.0 - Alpha1) * (1.0 - Alpha2))
Two layers of transparent images will always be more opaque then one layer.
edotorpedo
edotorpedo

Or you can try this:

alphaTotal = alpha1 + (1 - alpha1) * alpha2

Edo
Edo
andrew_j_w
andrew_j_w
Ah, of course. It looks so simple now!

Many thanks,
Andrew

[edited by - andrew_j_w on January 7, 2003 11:48:49 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.