C# 使用 alpha 混合将 ARBG 转换为 RGB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting ARBG to RGB with alpha blending
提问by Bob
Let's say that we have an ARGB color:
假设我们有一个 ARGB 颜色:
Color argb = Color.FromARGB(127, 69, 12, 255); //Light Urple.
When this is painted on top of an existing color, the colors will blend. So when it is blended with white, the resulting color is Color.FromARGB(255, 162, 133, 255);
当它被绘制在现有颜色的顶部时,颜色将混合。所以当它与白色混合时,产生的颜色是Color.FromARGB(255, 162, 133, 255);
The solution should work like this:
解决方案应该是这样的:
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);
What is ToRGB
's implementation?
什么是ToRGB
实现?
采纳答案by Louis Brandy
It's called alpha blending.
这称为alpha 混合。
In psuedocode, assuming the background color (blend) always has 255 alpha. Also assumes alpha is 0-255.
在伪代码中,假设背景颜色(混合)始终具有 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()
note: you probably need to be a bit (more) careful about floating-point/int math and rounding issues, depending on language. Cast intermediates accordingly
注意:根据语言,您可能需要对浮点/整数数学和舍入问题多加小心。相应地铸造中间体
Edited to add:
编辑添加:
If you don't have a background color with an alpha of 255, the algebra gets alot more complicated. I've done it before and it's a fun exercise left to the reader (if you really need to know, ask another question :).
如果您没有 alpha 为 255 的背景颜色,则代数会变得更加复杂。我以前做过,这是一个有趣的练习留给读者(如果你真的需要知道,问另一个问题:)。
In other words, what color C blends into some background the same as blending A, then blending B. This is sort of like calculating A+B (which isn't the same as B+A).
换句话说,什么颜色 C 混合到一些背景中与混合 A,然后混合 B 相同。这有点像计算 A+B(与 B+A 不同)。
回答by Matt Dawdy
if you don't need to know this pre-render, you could always use the win32 method of getpixel, I believe.
如果你不需要知道这个预渲染,我相信你总是可以使用 getpixel 的 win32 方法。
Note: typing on iPhone in the middle of Missouri with no inet access. Will look up real win32 example and see if there is a .net equivalent.
注意:在没有 inet 访问权限的密苏里州中部的 iPhone 上打字。将查找真正的 win32 示例,看看是否有 .net 等效项。
In case anyone cares, and doesn't want to use the (excellent) answer posted above, you can get the color value of a pixel in .Net via this link MSDN example
如果有人关心,并且不想使用上面发布的(优秀)答案,您可以通过此链接MSDN 示例获取 .Net 中像素的颜色值
回答by Paul Ishak
I know this is an old thread, but I want to add this:
我知道这是一个旧线程,但我想添加以下内容:
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));
}