RGBA calculation according to RGB color

If I have RGB color, with 100% opacity.

I need the same color (or close to it) with a transparent alpha channel. I will draw a transparent color on a white background.

How to calculate RGBA color?

I suppose I ask opposite of this question .

+6
source share
1 answer

Do you want to have an RGBA color with maximum transparency, which when painted over a white background gives the original RGB color?

Let R 0 , G 0, and B 0 be components of the original color, each in the range from 0.0 to 1.0, and let R, G, B, and A be the components of the new RGBA color (with A = 1, which means 100% opacity). We know that colors must satisfy:

R 0 = A? R + (1 minus A)
G 0 = A? G + (1 minus A)
B 0 = A? B + (1 minus A)

if we knew A, we could easily solve for R, G, and B:

R = (R 0 min. 1 + A) / A = 1 min. (1 & minus R 0 ) / A
G = (G 0 ? 1 + A) / A = 1? (1 & minus; G 0 ) / A
B = (B 0 min. 1 + A) / A = 1 min. (1 & minus; B 0 ) / A

Since we require that R & ge; 0, g? 0 and B? 0, it follows that 1 & minus; R 0 ? A, 1? G 0 ? A and 1 ± B 0 ? A and therefore the smallest possible value for A:

A = max (1 min. R 0 , 1 min. G 0 , 1 min. B 0 ) = 1 min. min (R 0 , G 0 , B 0 )

So we want:

A = 1 & minus; min (R 0 , G 0 , B 0 )
R = 1 ± (1 & minus R 0 ) / A
G = 1 & minus; (1 & minus; G 0 ) / A
B = 1 & minus; (1 & minus; B 0 ) / A


Ps. For a black background, the same formulas will be even simpler:

A = max (R 0 , G 0 , B 0 )
R = R 0 / A
G = G 0 / A
B = B 0 / A


Pps To clarify, all of the above formulas are for non-multiplex RGBA colors. For pre-multiplied alpha, simply multiply R, G and B as calculated above by A, giving:

R = A? (1 minus (1 <R 0 ) / A) = R 0 minus (1 <A)
G = A? (1 minus (1> G 0 ) / A) = G 0 minus (1 <A)
B = A? (1 minus (1 <B 0 ) / A) = B 0 min (1 & A)

(or for a black background, just R = R 0 , G = G 0 and B = B 0. )

+4
source

All Articles