如何混合两个ARGB像素?

发布于 2024-08-15 05:10:42 字数 218 浏览 3 评论 0原文

如何混合两个 ARGB 像素?

示例

来源:en.wikipedia.org/wiki/Alpha_compositing#mediaviewer/File:Alpha_compositing.svg

这里 A 是(红色Alpha) 和 B 是 ( 蓝色与 Alpha )。

How can I mix two ARGB pixels ?

Example

Source: en.wikipedia.org/wiki/Alpha_compositing#mediaviewer/File:Alpha_compositing.svg

Here A is (Red with Alpha) and B is ( Blue with Alpha ).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲娇萝莉攻 2024-08-22 05:10:43

摘自您获取图像的同一篇维基百科文章:

C_o = C_a \alpha_a + C_b \alpha_b \left(1 - \ alpha_a\right)

转换为 0 到 255 范围内的值:

rOut = (rA * aA / 255) + (rB * aB * (255 - aA) / (255*255))
gOut = (gA * aA / 255) + (gB * aB * (255 - aA) / (255*255))
bOut = (bA * aA / 255) + (bB * aB * (255 - aA) / (255*255))
aOut = aA + (aB * (255 - aA) / 255)

Taken from the same Wikipedia article where you got the image:

C_o = C_a \alpha_a + C_b \alpha_b \left(1 - \alpha_a\right)

Translating to values which range from 0 to 255:

rOut = (rA * aA / 255) + (rB * aB * (255 - aA) / (255*255))
gOut = (gA * aA / 255) + (gB * aB * (255 - aA) / (255*255))
bOut = (bA * aA / 255) + (bB * aB * (255 - aA) / (255*255))
aOut = aA + (aB * (255 - aA) / 255)
白龙吟 2024-08-22 05:10:43

以上是错误的算法。
这可以通过代入 aA = 0 来验证。事实证明,如果您尝试将颜色 B 与完全透明的颜色 A(即不可见)混合,那么从逻辑上讲,颜色 B 保持不变,但根据此公式,它会发生变化。
与渐变透明度错误混合的结果。
因此,正确的解决方案应该是这样的:

aOut = aA + (aB * (255 - aA) / 255)
rOut = (rA * aA + rB * aB * (255 - aA) / 255)/aOut 
gOut = (gA * aA + gB * aB * (255 - aA) / 255)/aOut 
bOut = (bA * aA + bB * aB * (255 - aA) / 255)/aOut 

与渐变透明度正确混合的结果。

The above is the wrong algorithm.
This can be verified by substituting aA = 0. Then it turns out that if you are trying to blend color B with completely transparent color A, i.e. invisible, then logically the color B remains unchanged, but according to this formula it will be changed.
The result of incorrect blending with gradient transparency.
Therefore, the correct solution would be like this:

aOut = aA + (aB * (255 - aA) / 255)
rOut = (rA * aA + rB * aB * (255 - aA) / 255)/aOut 
gOut = (gA * aA + gB * aB * (255 - aA) / 255)/aOut 
bOut = (bA * aA + bB * aB * (255 - aA) / 255)/aOut 

The result of correct blending with gradient transparency.

不再让梦枯萎 2024-08-22 05:10:43

看来这就是您想要的: http://en.wikipedia.org/wiki/Alpha_compositing #Alpha_blending,但我对你的符号有点困惑,因为维基百科说 argb 值的范围应该是从 0.0 到 1.0。所以我不认为这个公式会给你 FA=19。你能澄清一下吗?

编辑:既然你已经排除了关于 FA=19 的问题,我倾向于采用这个公式。

It seems like this is what you want: http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending, but I'm a little confused by your notation since wikipedia says that argb values should range from 0.0 to 1.0. So I don't think this formula will give you FA=19. Can you clarify?

Edit: now that you took out the business about FA=19, I'm inclined to go with that formula.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文