Sign in to follow this  
David Clark

RGBA + RGBA = ?

Recommended Posts

Hi, I need a formula for mixing 2 RGBA colours together to get a third one. So far I know the alpha channel of the two can be worked out by this equation: NewColor.A = Bottom.A + Top.A * (1-Bottom.A) But I need to know how to work out ther Red, Green, and Blue values for NewColor as well :) The effect i'm after is when you have layers in PSP with a normal blend effect and both layers have alpha channels Thanks

Share this post


Link to post
Share on other sites

new_color.r = color1.r + ((color2.r - color1.r) * blend_value);
new_color.g = color1.g + ((color2.g - color1.g) * blend_value);
new_color.b = color1.b + ((color2.b - color1.b) * blend_value);
new_color.a = color1.a + ((color2.a - color1.a) * blend_value);


blend_value ranges from 0.0 to 1.0

Share this post


Link to post
Share on other sites
That works great if ur fading one image to another, but i need to achieve the effect of blending or 'mixing' two layers in the paint programs.

If you have psp7, make 2 layers with 2 separate layers, and merge the two layers together so they form a 32 bit channel layer instead of 2 32 bit channels. I need an equation for that 'merge' effect.

Share this post


Link to post
Share on other sites
It depends entirely on what blending you use.

If the layers you are using aren’t blended, but do use transparency you could use the following equation

For each channel (RGB)
NewColor = TopColor * TopAlpha + BottomColor * (1 – TopAlpha)

Share this post


Link to post
Share on other sites
Thanks, but that equation is used if ur rendering a RGBA image onto a RGB surface :(

I need the equation to render it onto a RGBA image, but should the RGBA image be rendered onto a RGB surface it would look the same. I can upload pictures if it'll help explain what i'm after :)

Share this post


Link to post
Share on other sites
Quote:
Original post by David Clark
Thanks, but that equation is used if ur rendering a RGBA image onto a RGB surface :(

I think you want to combine the RGB as skow mentioned, but your resulting alpha should be the avarage of the two input alphas.

Share this post


Link to post
Share on other sites
Here :)

http://www.esnips.com/doc/bbc836eb-a390-4f01-a8d1-f7610c202a31/RGBAtoRGBA.png


Okay this is the effect I'm after. I have image 1, and image 2. I combine the two such that image 2 is on top of image 1, and i result in image 3.

1a, 2a, 3a show the RGB value
1b, 2b, 3b show the alpha value
1c, 2c, 3c show the two combined on a background to help demonstrate what the image looks like.

So I already know how to get 2c out of 2a and 2b, but how do I get 1c ?

Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Not sure, if it is exactly, what you are looking for, but AFAIK the formula for blending surfaces INTO RGBA surface is:

fc, fa - foreground color channel and alpha
bc, ba - background color channel and alpha
dc, da - result color channel and alpha

da = 1 - (1 - fa) * (1 - ba)
dc = (ba * (1 - fa) * bc + fa * fc) / da

It becomes much cleaner, if you use premultiplied alpha, i.e. you color channel values are multiplied with alpha (40% transparent white is thus [0.4, 0.4, 0.4, 0.4] instead of [1 1 1 0.4])

With premultiplied channels:
da = 1 - (1 - fa) * (1 - ba)
dc = (1 - fa) * bc + fc;

Share this post


Link to post
Share on other sites
I don't think there is such a thing as a "correct" method. It all depends on what kind of blending effect you want to acheive. However, I guess what is considered Normal blending in Photoshop and PSP uses the algorithm skow presented for the color. For the alpha on the other hand I'm fairly certain most of the blending modes use the following algorithm:

Treat alpha as the amount of the layer's coverage of the background, i.e. the layer's inverse transparency. If one layer has alpha A0 then (1 - A0) * BackgroundColor is transmitted though the layer. Now if you introduce a second layer on top with alpha A1 this layer will in turn transmit (1 - A1) * BottomLayerColor and as far as the background color is concerned the new layer will transmit (1 - A1) * (1 - A0) * BackgroundColor. This is a measure of how transparent two stacked layers are, but since we want to know the combined alpha value which is the inverse transparency (stated above) we will have to subtract the value from 1.

We get:
CombinedAlpha = 1 - (1 - A0) * (1 - A1)

If you want to stack more than two layers you simply append more factors at the end of the expression.

This formula might not look intuitive at first glance but plugging in some numbers give:

A0 = 0.25 , A1 = 0.25:
CombinedAlpha = 1 - (1 - 0.25) * (1 - 0.25) = 0.4375

A0 = 0.5 , A1 = 0.5:
CombinedAlpha = 1 - (1 - 0.5) * (1 - 0.5) = 0.75

A0 = 0.75 , A1 = 0.75:
CombinedAlpha = 1 - (1 - 0.75) * (1 - 0.75) = 0.9375

which is reasonable since more than one semi-transparent layer will occlude more than a single layer, but still not as much as the sum of the alpha values. Think of it as multiple sheets of colored film held up to a lamp. One sheet will block some amount of light and the more sheets you add, the more light you block.

EDIT: You beat me to it AP.

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