0

If we define a color with transparency as $(r,g,b,a)$ (red,green,blue,alpha), with $r,g,b,a\in[0,1]$, how do we define $(r_1,g_1,b_1,a_1)+(r_2,g_2,b_2,a_2)$?

The definition must be closed, and must represent the computer science version of adding (blending) two alpha colors.

I tried taking alpha averages, but I don't seem to get closure. I also got that if $a_1r_1=a_2r_2$ then the displayed color is the same.

Addendum

I believe the answer is:

$$\big(\frac{a_1r_1+a_2r_2}{2},\frac{a_1g_1+a_2g_2}{2},\frac{a_1b_1+a_2b_2}{2},\frac{a_1+a_2}{2}\big)$$

JMP
  • 21,771
  • it depends if you are additive (painting, starting from black and adding colors) or substractive (lighting, starting from white and filtering some colors) – reuns Oct 05 '16 at 01:58
  • in the additive model, $a = 0$ means black whatever $rgb$ is, and in the substractive model, $a=1$ means white (no filtering) whatever $rgb$ is – reuns Oct 05 '16 at 02:01

2 Answers2

1

It depends on what kind of mixing you denote with "$+$". If you overlay layers then it is not even a commutative operation (e.g., if both are opaque, the top layer always wins). To really "mix" we should consider what we expect to happen with a coloured background behind the single and the mixed layer. I assume that $(r,g,b,a)$ covering $(r',b',g')$ results in $(ar+(1-a)r',ag+(1-a)g',ab+(1-a)b')$. For $$ (r,g,b,a)=(r_1,g_1,b_1,a_1)+(r_2,g_2,b_2,a_2)$$ I assume we desire that $(ar+(1-a)r',ag+(1-a)g',ab+(1-a)b')$ should be the arithmetic average of $(a_1r_1+(1-a_1)r',a_1g_1+(1-a_1)g',a_1b_1+(1-a_1)b')$ and $(a_2r_2+(1-a_2)r',a_2g_2+(1-a_2)g',a_2b_2+(1-a_2)b')$. This implies that $1-a=\frac{(1-a_1)+(1-a_2)}{2}$, i.e., $$a=\frac{a_1+a_2}2$$ (from the background contribution). And then from $ar=\frac{a_1r_1+a_2r_2}2$ etc. we find $$r=\frac{a_1r_1+a_2r_2}{a_1+a_2} $$ i.e., weighted averages.

0

Say we have a solid background with color $\vec C_0 = (R_0,G_0,B_0)$, and we'd like to paint another color with transparency $(R,G,B,\alpha) = (\vec C,\alpha)$ over it. The result should be $\alpha \vec C + (1-\alpha) \vec C_0$. We can write this as a matrix equation (where the $\vec C$s are column vectors of $R,G,B$) $$ \begin{bmatrix} \alpha \vec C + (1-\alpha) \vec C_0 \\ 1 \end{bmatrix} = \begin{bmatrix} (1-\alpha) I & \alpha \vec C \\ 0 & 1 \end{bmatrix} \begin{bmatrix} \vec C_0 \\ 1 \end{bmatrix}. $$ So if you have two colors with transparency $(C_1,\alpha_1)$ and $(C_2,\alpha_2)$, then the operation of painting the second and then the first would be represented by the matrix $$ \begin{bmatrix} (1-\alpha_1) I & \alpha_1 \vec C_1 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} (1-\alpha_2) I & \alpha_2 \vec C_2 \\ 0 & 1 \end{bmatrix} = \begin{bmatrix} (1-\alpha_1)(1-\alpha_2) I & \alpha_1 \vec C_1 + (1-\alpha_1)\alpha_2 \vec C_2 \\ 0 & 1 \end{bmatrix} $$ This shows that blending $(\vec C_1,\alpha_1)$ on top of $(\vec C_2,\alpha_2)$ gives the same result as blending just the single composite color $$ \left( \frac{\alpha_1 \vec C_1 + (1-\alpha_1) \alpha_2 \vec C_2)}{1 - (1 - \alpha_1)(1 - \alpha_2)}, 1 - (1 - \alpha_1)(1 - \alpha_2) \right). $$

One can see why it would be useful to represent transparent colors with premultiplied alpha $\vec c = \alpha \vec C$, and also to store $\beta = 1 - \alpha$ instead of $\alpha$ (so $\beta = 0$ is opaque and $\beta = 1$ is fully transparent). For then the form of the matrices is $$ \begin{bmatrix} \beta I & \vec c \\ 0 & 1 \end{bmatrix} $$ and composition formula becomes simply $$ (\vec c_1,\beta_1), (\vec c_2,\beta_2) \mapsto (\vec c_1 + \beta_1 \vec c_2, \beta_1 \beta_2) $$


Addendum: If you are really mixing colors rather than overlaying, then we can simply average the color matrices: $$ \frac{1}{2} \left( \begin{bmatrix} (1-\alpha_1) I & \alpha_1 \vec C_1 \\ 0 & 1 \end{bmatrix} + \begin{bmatrix} (1-\alpha_2) I & \alpha_2 \vec C_2 \\ 0 & 1 \end{bmatrix} \right) = \begin{bmatrix} \bigl(1 - \frac{\alpha_1+\alpha_2}{2}\bigr)I & \frac{\alpha_1 \vec C_1 + \alpha_2 \vec C_2}{2} \\ 0 & 1 \end{bmatrix} $$ to get $$(\vec C_1,\alpha_1), (\vec C_2,\alpha_2) \mapsto \left( \frac{\alpha_1 \vec C_1 + \alpha_2 \vec C_2}{\alpha_1 + \alpha_2}, \frac{\alpha_1 + \alpha_2}{2} \right). $$ or in terms of $\vec c$ and $\beta$: $$(\vec c_1,\beta_1), (\vec c_2,\beta_2) \mapsto \left(\frac{\vec c_1 + \vec c_2}{2},\frac{\beta_1 + \beta_2}{2}\right).$$


Moral: Using premultiplied alpha and $\beta = 1-\alpha$ makes things a lot easier.

arkeet
  • 6,695