简体   繁体   English

计算RGBA以匹配RGB颜色

[英]computing RGBA to match an RGB color

If I have an RGB color, with 100% opacity. 如果我有RGB颜色,100%不透明度。

I want that same color (or close to it) with a transparent alpha channel. 我想要一个透明的alpha通道相同的颜色(或接近它)。 I will paint the transparent color over a white background. 我将在白色背景上绘制透明色。

How do I compute the RGBA color? 如何计算RGBA颜色?

I guess what I am asking is the opposite of this question . 我想我要问的是这个问题反面

You mean you want the RGBA color with maximum transparency which, when drawn on top of a white background, gives the original RGB color? 你的意思是你想要RGBA颜色具有最大的透明度,当在白色背景上绘制时,它会给出原始的RGB颜色?

Let R 0 , G 0 and B 0 be the components of the original color, each ranging from 0.0 to 1.0, and let R , G , B and A be the components of the new RGBA color (with A = 1 denoting 100% opacity). R 0G 0B 0为原始颜色的分量,每个范围从0.0到1.0,并且让RGBA成为新RGBA颜色的分量( A = 1表示100%不透明度) )。 We know that the colors must satisfy: 我们知道颜色必须满足:

R 0 = A · R + (1 − A ) R 0 = A · R +(1 - A
G 0 = A · G + (1 − A ) G 0 = A · G +(1 - A
B 0 = A · B + (1 − A ) B 0 = A · B +(1 - A

which, if we knew A , we could easily solve for R , G and B : 如果我们知道A ,我们可以很容易地解决RGB

R = ( R 0 − 1 + A ) / A = 1 − (1 − R 0 ) / A R =( R 0-1 + A )/ A = 1 - (1- R 0 )/ A.
G = ( G 0 − 1 + A ) / A = 1 − (1 − G 0 ) / A G =( G 0-1 + A )/ A = 1 - (1- G 0 )/ A.
B = ( B 0 − 1 + A ) / A = 1 − (1 − B 0 ) / A B =( B 0-1 + A )/ A = 1 - (1- B 0 )/ A.

Since we require that R ≥ 0, G ≥ 0 and B ≥ 0, it follows that 1 − R 0 ≥ A, 1 − G 0 ≥ A and 1 − B 0 ≥ A, and therefore the smallest possible value for A is: 由于我们需要的是,R≥0,G≥0B≥0,它遵循一个1 - ,R 0≥A,1 - g ^ 0≥A和1 - B 0≥A,并且因此对于A的最小可能值是:

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

Thus, the color we want is: 因此,我们想要的颜色是:

A = 1 − min( R 0 , G 0 , B 0 ) A = 1 - min( R 0G 0B 0
R = 1 − (1 − R 0 ) / A R = 1 - (1- R 0 )/ A.
G = 1 − (1 − G 0 ) / A G = 1 - (1- G 0 )/ A.
B = 1 − (1 − B 0 ) / A B = 1 - (1- B 0 )/ A.


Ps. PS。 For a black background, the same formulas would be even simpler: 对于黑色背景,相同的公式甚至更简单:

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


Pps. PPS。 Just to clarify, all the formulas above are for non -premultiplied RGBA colors. 只是为了澄清,上面的所有公式都是针对预乘的RGBA颜色。 For premultiplied alpha, just multiply R , G and B as calculated above by A , giving: 对于预乘alpha,只需将上面计算的RGB乘以A ,得出:

R = A · ( 1 − (1 − R 0 ) / A ) = R 0 − (1 − A ) R = A ·(1 - (1 - R 0 )/ A )= R 0 - (1 - A
G = A · ( 1 − (1 − G 0 ) / A ) = G 0 − (1 − A ) G = A ·(1 - (1 - G 0 )/ A )= G 0 - (1 - A
B = A · ( 1 − (1 − B 0 ) / A ) = B 0 − (1 − A ) B = A ·(1 - (1 - B 0 )/ A )= B 0 - (1 - A

(or, for a black background, simply R = R 0 , G = G 0 and B = B 0 .) (或者,对于黑色背景,简单地R = R 0G = G 0B = B 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM