25

假设我们有一个 ARGB 颜色:

Color argb = Color.FromARGB(127, 69, 12, 255); //Light Urple.

当这被涂在现有颜色的顶部时,颜色将混合。所以当它与白色混合时,得到的颜色是Color.FromARGB(255, 162, 133, 255);

解决方案应该像这样工作:

Color blend = Color.White; 
Color argb = Color.FromARGB(127, 69, 12, 255); //Light Urple.      
Color rgb = ToRGB(argb, blend); //Same as Color.FromARGB(255, 162, 133, 255);

什么是ToRGB执行?

4

3 回答 3

19

它被称为阿尔法混合

在伪代码中,假设背景颜色(混合)始终具有 255 alpha。还假设 alpha 为 0-255。

alpha=argb.alpha()
r = (alpha/255)*argb.r() + (1 - alpha/255)*blend.r()
g = (alpha/255)*argb.g() + (1 - alpha/255)*blend.g()
b = (alpha/255)*argb.b() + (1 - alpha/255)*blend.b()

注意:您可能需要对浮点/整数数学和舍入问题更加小心,具体取决于语言。相应地铸造中间体

编辑添加:

如果您没有 alpha 为 255 的背景颜色,则代数会变得更加复杂。我以前做过,这对读者来说是一个有趣的练习(如果你真的需要知道,问另一个问题:)。

换句话说,什么颜色 C 混合到某个背景中,就像混合 A,然后混合 B。这有点像计算 A+B(与 B+A 不同)。

于 2008-08-05T20:16:03.583 回答
4

我知道这是一个旧线程,但我想添加这个:

Public Shared Function AlphaBlend(ByVal ForeGround As Color, ByVal BackGround As Color) As Color
    If ForeGround.A = 0 Then Return BackGround
    If BackGround.A = 0 Then Return ForeGround
    If ForeGround.A = 255 Then Return ForeGround
    Dim Alpha As Integer = CInt(ForeGround.A) + 1
    Dim B As Integer = Alpha * ForeGround.B + (255 - Alpha) * BackGround.B >> 8
    Dim G As Integer = Alpha * ForeGround.G + (255 - Alpha) * BackGround.G >> 8
    Dim R As Integer = Alpha * ForeGround.R + (255 - Alpha) * BackGround.R >> 8
    Dim A As Integer = ForeGround.A

    If BackGround.A = 255 Then A = 255
    If A > 255 Then A = 255
    If R > 255 Then R = 255
    If G > 255 Then G = 255
    If B > 255 Then B = 255

    Return Color.FromArgb(Math.Abs(A), Math.Abs(R), Math.Abs(G), Math.Abs(B))
End Function

public static Color AlphaBlend(Color ForeGround, Color BackGround)
{
    if (ForeGround.A == 0)
        return BackGround;
    if (BackGround.A == 0)
        return ForeGround;
    if (ForeGround.A == 255)
        return ForeGround;

    int Alpha = Convert.ToInt32(ForeGround.A) + 1;
    int B = Alpha * ForeGround.B + (255 - Alpha) * BackGround.B >> 8;
    int G = Alpha * ForeGround.G + (255 - Alpha) * BackGround.G >> 8;
    int R = Alpha * ForeGround.R + (255 - Alpha) * BackGround.R >> 8;
    int A = ForeGround.A;

    if (BackGround.A == 255)
        A = 255;
    if (A > 255)
        A = 255;
    if (R > 255)
        R = 255;
    if (G > 255)
        G = 255;
    if (B > 255)
        B = 255;

    return Color.FromArgb(Math.Abs(A), Math.Abs(R), Math.Abs(G), Math.Abs(B));
}
于 2013-06-26T10:52:29.007 回答
2

如果你不需要知道这个预渲染,你总是可以使用 getpixel 的 win32 方法,我相信。

注意:在密苏里州中部的 iPhone 上打字,没有 inet 访问权限。将查找真正的 win32 示例并查看是否有 .net 等价物。

如果有人关心,并且不想使用上面发布的(优秀)答案,您可以通过此链接MSDN 示例获取 .Net 中像素的颜色值

于 2008-08-06T06:39:12.173 回答