RGBA + RGBA = ?

Started by
8 comments, last by David Clark 18 years ago
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
Advertisement
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
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.
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)

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 :)
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.
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
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;
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.
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
Thanks so much! I believe that is exactly what I am after! Now to work out a way of implimenting it hehe :)

This topic is closed to new replies.

Advertisement