需要帮助了解 Alpha 通道

发布于 2024-10-15 02:43:10 字数 150 浏览 2 评论 0原文

我有一个像素的 RGB 元组,我们将其称为 P。

(255, 0, 0) 是 P 的颜色,alpha 通道为 1.0。

当 Alpha 通道为 0.8 时,P 的颜色变为 (255, 51, 51)。

如何获取影响 P 颜色的像素颜色?

I have the RGB tuple of a pixel we'll call P.

(255, 0, 0) is the color of P with the alpha channel at 1.0.

With the alpha channel at 0.8, P's color becomes (255, 51, 51).

How can I get the color of the pixel that is influencing P's color?

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

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

发布评论

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

评论(2

鲜肉鲜肉永远不皱 2024-10-22 02:43:10

让我们从头开始吧。具有 Alpha 的像素只有在与其他内容混合时才有意义。如果您有一个带有 alpha 的上层 U 和一个完全不透明的下层 L,则方程为:

P = (alpha * U) + ((1.0 - alpha) * L)

重新排列公式,您将得到:

L = (P - (alpha * U)) / (1.0 - alpha)

显然,当 alpha 为 1.0 时,该方程没有意义,因为您需要除以为零。

代入数字后,像素 L 的 R=255、G=255 和 B=255。

按照惯例,图像中的最低层几乎都是白色的 (255,255,255),这几乎是普遍现象。

Let's start from the beginning. A pixel with alpha only makes sense when it is blended with something else. If you have an upper layer U with alpha and a lower layer L that is totally opaque, the equation is:

P = (alpha * U) + ((1.0 - alpha) * L)

Rearranging the formula, you obtain:

L = (P - (alpha * U)) / (1.0 - alpha)

Obviously the equation doesn't make sense when the alpha is 1.0, as you'd be dividing by zero.

Plugging your numbers in reveals that R=255, G=255, and B=255 for the pixel L.

It is almost universal that the lowest layer in an image will be all white (255,255,255) by convention.

幸福%小乖 2024-10-22 02:43:10

只需查看您提供的数字即可:

(1.0-0.8)*255 ~= 50.9 = 51

其中:

  • 1.0 是最大 alpha 强度
  • 0.8 是当前设置的 alpha 强度
  • 255 是每个 RGB 通道的最大强度(背景颜色)

这适合您的 B 和 G 通道例子。

因此,在一般情况下,它似乎是通道值(RGB)和背景颜色(在您的情况下为白色 - 255)之间的简单加权平均值。 Alpha 被用作权重。

下面是一些 Python 代码:

MIN_ALPHA=0.0
MAX_ALPHA=1.0
MIN_CH=0
MAX_CH=255
BG_VAL=255

def apply_alpha(old, alpha, bg=255):
    assert alpha >= MIN_ALPHA
    assert alpha <= MAX_ALPHA
    assert old >= MIN_CH
    assert old <= MAX_CH

    new = old*alpha + (MAX_ALPHA - alpha)*bg
    return new

if __name__ == '__main__':
    import sys
    old, alpha = map(float, sys.argv[1:])
    print apply_alpha(old, alpha)

和一些输出:

misha@misha-K42Jr:~/Desktop/stackoverflow$ python alpha.py 255 0.8
255.0
misha@misha-K42Jr:~/Desktop/stackoverflow$ python alpha.py 0 0.8
51.0

尝试其他示例(特别是非白色背景)——可能就是这么简单。如果没有,请使用新示例编辑您的答案,我会再看一下。

Just looking at the numbers you provided:

(1.0-0.8)*255 ~= 50.9 = 51

Where:

  • 1.0 is the maximum alpha intensity
  • 0.8 is the currently set alpha intensity
  • 255 is the maximum intensity of each of the RGB channels (the color of the background)

This fits the B and G channels of your example.

So, in the general case, it seems to be a simple weighted average between the channel value (either of RGB) and the background color (in your case, white -- 255). Alpha is being used as the weight.

Here's some Python code:

MIN_ALPHA=0.0
MAX_ALPHA=1.0
MIN_CH=0
MAX_CH=255
BG_VAL=255

def apply_alpha(old, alpha, bg=255):
    assert alpha >= MIN_ALPHA
    assert alpha <= MAX_ALPHA
    assert old >= MIN_CH
    assert old <= MAX_CH

    new = old*alpha + (MAX_ALPHA - alpha)*bg
    return new

if __name__ == '__main__':
    import sys
    old, alpha = map(float, sys.argv[1:])
    print apply_alpha(old, alpha)

And some output:

misha@misha-K42Jr:~/Desktop/stackoverflow$ python alpha.py 255 0.8
255.0
misha@misha-K42Jr:~/Desktop/stackoverflow$ python alpha.py 0 0.8
51.0

Try this for other examples (in particular, non-white backgrounds) -- it's probably that simple. If not, edit your answer with new examples, and I'll have another look.

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