【发布时间】:2013-06-05 13:37:45
【问题描述】:
我只需要一个函数,它接受两种颜色并返回它们的混合版本。它应该以与 GDI 相同的方式混合它们。
我认为在 GDI 中混合两种 ARGB 颜色时得到的 alpha 值是这样计算的:
Private Function blend(alphaBelow As Single, alphaAbove As Single) As Single
Return alphaBelow + (1.0 - alphaBelow) * alphaAbove
End Function
我还发现了this page,其中微软说 R、G 和 B 值是使用这个公式计算的:
displayColor = sourceColor × alpha / 255 + backgroundColor × (255 – alpha) / 255
但是,我无法让它工作:
Color1: A=164, R=111, G=78, B=129
Color2: A=241, R=152, G=22, B=48
Blended in GDI: A=250, R=150, G=24, B=50
R:
150 = 152 * x / 255 + 111 * (255 - x) / 255
x = 9945/41 = 242.5609756097560975609756097561
G:
24 = 22 * x / 255 + 78 * (255 - x) / 255
x = 6885/28 = 245.89285714285714285714285714286
B:
50 = 48 * x / 255 + 129 * (255 - x) / 255
x = 6715/27 = 248.7037037037037037037037037037
如您所见,对于 R、G 和 B 值,我得到了不同的 alpha 值。这个数字是怎么计算出来的?
编辑:
Color1 和 Color2 只是我想混合在一起的一些随机 ARGB 颜色。 “在 GDI 中混合”是您在位图中将它们相互叠加绘制的结果。
将颜色与 GDI 混合的代码:
Dim B As New Bitmap(Width, Height, Imaging.PixelFormat.Format32bppPArgb)
Dim G = Graphics.FromImage(B)
Dim w = B.Width - 1, h = B.Height - 1
G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Dim pth As New Drawing2D.GraphicsPath
pth.AddRectangle(New Rectangle(0, 0, w, h))
Dim c1 = RandomColor(), c2 = RandomColor()
G.FillPath(New SolidBrush(c1), pth)
G.FillPath(New SolidBrush(c2), pth)
Dim resoult As Color = B.GetPixel(w / 2, h / 2)
【问题讨论】:
-
你能解释一下你是如何获得第一个表格中的数字的吗?
-
Color1 和 Color2 只是我想混合在一起的一些随机 ARGB 颜色。 “在 GDI 中混合”是您在位图中将它们相互叠加绘制的结果。
-
您究竟是如何“将它们相互叠加”的。一些代码可以确保我们确切地知道您在说什么。
-
您需要处理 两个 alpha 值,您的公式只使用一个。也不清楚您使用的样本 RGB 值是否被预乘,就像 32bppPArgb 这样的像素格式发生的那样。