转换ARBG与alpha混合
问题描述:
比方说,我们有一个ARGB颜色RGB:转换ARBG与alpha混合
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
的实现?
答
它叫做alpha blending。
在psuedocode中,假设背景色(混合)总是有255个字母。还假设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()
注:您可能需要有点(更多)小心浮点/ INT数学和取整的问题,这取决于语言。演员中间体因此
编辑补充:
如果你没有用的255阿尔法背景颜色,代数得到了很多更加复杂。我之前就已经完成了,这对读者来说是一个有趣的练习(如果您真的需要知道,请提出另一个问题:)。
换句话说,什么颜色C混合到一些背景中,就像混合A,然后混合B.这就像计算A + B(它不同于B + A)。
答
如果你不需要知道这个预渲染,我相信你总是可以使用getpixel的win32方法。
注意:在密苏里州中部的iPhone上打字,没有任何访问权限。将查找真正的win32示例,并查看是否存在.net等效项。
万一有人关心,并且不希望使用的(优秀)答案贴上面,你可以通过这个链接得到的.Net像素的颜色值MSDN example
答
我知道这是一个古老的线程,但我想补充一点:
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));
}
我只想说我发现它很棒,“Light Urple”已经存活了4次编辑。 – 2017-09-12 03:42:46