# Converting color to equivalent with opacity

Assume a color X with alpha 1 and RGB 255,230,210.

How can i find another color with alpha A ( or an array of colors ) that blended with a background of color Y ( e.g 255,255,255 ) would produce the original color ?

This depends on what function you're blending with. Since you don't say where you're doing this, I'll just take OpenGL as an example.

As you can see in the API, there are several ways of blending two colours (or rather how to interpret these two colours for a given blend function).

You speak of a colour and a background colour, so let's say your result is $$c_r$$, your object in the foreground has colour $$c_s$$ (for Source, the new colour value being "added" someway) and your background has colour $$c_d$$ (for Destination, the old colour value that has been in the buffer already).

Let $$c_s = \left(\begin{array}{c} R_s\\G_s\\B_s\\A_s \end{array}\right)$$ (and analogously for $$c_r$$ and $$c_d$$).

To determine the blended RGBA values of a pixel, the system uses the following equations: $$R_d = min(k_{R'}, R_s s_R + R_d d_R) \ G_d = min(k_{G'}, G_s S_G + G_d d_G) \ B_d = min(k_{B'}, B_s s_B + B_d d_B) A_d = min(k_{A'}, A_s s_A + A_d d_A)$$

A bit confusing with the doubled $$R_d$$ and so forth, so I'll rewrite it to the denomination I defined earlier:

$$R_r = min(k_{R'}, R_s s_R + R_d d_R) \ G_r = min(k_{G'}, G_s S_G + G_d d_G) \ B_r = min(k_{B'}, B_s s_B + B_d d_B) A_r = min(k_{A'}, A_s s_A + A_d d_A)$$

The min function is there for not going above a certain constant (and $$k_{R'},\ k_{G'},\ k_{B'},\ k_{A'}$$ are simply constants). For our purposes, let's just assume that we don't surpass this threshold, so we end up with

$$R_r = R_s s_R + R_d d_R$$ (and of course the same for the other channels).

The rest of the components $$s_R$$ and $$d_R$$ which are some scaling factors for combining the two colours.

Transparency is best implemented using blend function (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with primitives sorted from farthest to nearest.

This helps, because now we know what the factors $$s_R$$ and $$d_R$$ actually are: the alpha values of the Source.

This leaves us with

$$R_r = R_s A_s + (1 - R_d A_d)$$

Therefore, to come back to your example, we have (for the green channel, since that is not $$255$$):

$$G_r = 230$$, an undefined $$\alpha$$ value for the source, the background green $$G_d = 255$$ and probably a solid background, meaning $$A_d = 255$$. We need the values to be in $$\left[0, 1\right]$$, therefore we get $$G_r = \frac{230}{255} = 0.90196078431$$, $$G_d = 1$$ and $$A_d = 1$$.

Filling this in we get:

$$0.90196078431 = G_s A_s + (1 - 1 \cdot A_s)$$

Your question lets me believe, that you know $$A_s$$, thus only $$G_s$$ (and of course the same for the other colour channels) is missing. This can then be calculated by isolating the respective colour component.

• It's worth mentioning that for some alphas and colour combinations, the colours may come out of the equation negative e.g. if you want to turn a pure red background into pure green, there's only a positive solution if $\alpha = 1$ Nov 8, 2018 at 12:53
• thank you this is exactly what i was looking for. Nov 8, 2018 at 19:09