clamp
clamp

Reputation: 34036

formula for alpha value when blending two transparent colors

lets assume an alpha of 1 means fully opaque and 0 means fully transparent. lets say i have two black images which have 50% transparency (alpha = 0.5).

if they are laid on top of each other, the resulting transparency is 0.75, right?

if they would have an alpha of 0.25 , the result would be around 0.5, right?

if they would have an alpha of 0.9 , the result would be around 0.97, right?

how can you get to these numbers?

in other words i am looking for a function that gets the resulting alpha value from two other alpha value.

float alpha = f(float alphaBelow, float alphaAbove)
{
     //TODO implement
}

Upvotes: 15

Views: 6996

Answers (3)

Mark Ransom
Mark Ransom

Reputation: 308520

This answer is mathematically the same as Jason's answer, but this is the actual formula as you'll find it in reference material.

float blend(float alphaBelow, float alphaAbove) 
{ 
    return alphaBelow + (1.0 - alphaBelow) * alphaAbove; 
} 

Upvotes: 11

CHP
CHP

Reputation: 131

Photoshop does the following calculation:

float blend(float alphaBelow, float alphaAbove)
{
    return min(1,alphaBelow+(1-alphaBelow)*alphaAbove);
}

Upvotes: 4

Jason
Jason

Reputation: 28600

float blend(float alphaBelow, float alphaAbove)
{
    return alphaBelow + alphaAbove - alphaBelow * alphaAbove;
}

This function assumes both parameters are 0..1, where 0 is fully transparent and 1 is fully opaque.

Upvotes: 10

Related Questions