I have two colors, c₀ and c₁. They have variable alpha, red, green and blue values: (a₀, r₀, b₀, g₀) and (a₁, r₁, b₁, g₁). I'm wondering if there is a simple formula for combining these colors to obtain the correct new color (c₂).
Let's say that c₀ overlays c₁. I understand that if they had equal alpha values, then averaging their comparative red, green and blue values would do the trick. But when the alpha values differ, I'm noticing this doesn't work properly—it seems alpha determines the level at which each color's RGB values "contribute" to the final color.
Otherwise stated,
If a₀=a₁ then:
a₂=a₀=a₁,
r₂=(r₀+r₁)/2,
g₂=(g₀+g₁)/2,
b₂=(b₀+b₁)/2
Else:
a₂=(a₀+a₁)/2,
r₂=?,
g₂=?,
b₂=?
Mixing primary colors creates secondary colorsIf you combine two primary colors with each other, you get a so-called secondary color. If you mix red and blue, you get violet, yellow and red become orange, blue and yellow become green. If you mix all the primary colors together, you get black.
To represent a color through which the background can be seen to some extent, a fourth channel is added to the color: the alpha channel. The alpha channel specifies how opaque the color is. For example, the color #8921F2 (also described as rgb(137, 33, 242) or hsl(270, 89%, 54) ) is a nice shade of purple.
In computer graphics, alpha compositing or alpha blending is the process of combining one image with a background to create the appearance of partial or full transparency.
Short answer:
if we want to overlay c0 over c1 both with some alpha then
a01 = (1 - a0)·a1 + a0
r01 = ((1 - a0)·a1·r1 + a0·r0) / a01
g01 = ((1 - a0)·a1·g1 + a0·g0) / a01
b01 = ((1 - a0)·a1·b1 + a0·b0) / a01
Note that division by a01 in the formulas for the components of color. It's important.
Long answer:
The basic formula for the color when c0 overlays opaque c1 with alpha a0 is
r0 over 1 = (1 - a0)·r1 + a0·r0
// the same for g & b components
So if there is another color c2 and c1 actually is not opaque but has an alpha a1 we can overlay first c1 over c2 and then c0 over the resulting color.
r1 over 2 = (1 - a1)·r2 + a1·r1
r0 over (1 over 2) = (1 - a0)·((1 - a1)·r2 + a1·r1) + a0·r0
If we had a color c01 which overlays c2 with the same result as overlaying first c1 and then c0 it would be like this:
r01 over 2 = (1 - a01)·r2 + a01·r01
Ok, let's make them equal:
(1 - a01)·r2 + a01·r01 = (1 - a0)·((1 - a1)·r2 + a1·r1) + a0·r0 = (1 - a0)·(1 - a1)·r2 + (1 - a0)·a1·r1 + a0·r0
so
1 - a01 = (1 - a0)·(1 - a1) = 1 - ((1 - a0)·a1 + a0)
a01·r01 = (1 - a0)·a1·r1 + a0·r0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With