使用Alpha混合将ARBG转换为RGB [英] Converting ARBG to RGB with alpha blending

查看:385
本文介绍了使用Alpha混合将ARBG转换为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个ARGB颜色:

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

当在现有颜色上绘制时,颜色会混合。因此,当它与白色混合时,生成的颜色为 Color.FromARGB(255,162,133,255);



解决方案应该像这样:

 颜色混合= Color.White; 
颜色argb = Color.FromARGB(127,69,12,255); // Light Urple。
颜色rgb = ToRGB(argb,blend); //与Color.FromARGB(255,162,133,255)相同;

什么是 ToRGB 的实现?

解决方案

这称为 alpha混合



在伪码中,假设背景颜色(混合)总是有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 $ bb =(alpha / 255)* argb.b()+(1 - alpha / 255)* blend.b()

注意:根据语言的不同,您可能需要对浮点数/整数数学和舍入问题有点(多)小心。



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



换句话说,什么颜色C混合成一些背景,就像混合A,然后混合B.这就像计算A + B与B + A相同)。


Let's say that we have an ARGB color:

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);

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?

解决方案

It's called alpha blending.

In psuedocode, assuming the background color (blend) always has 255 alpha. Also assumes alpha is 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 :).

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).

这篇关于使用Alpha混合将ARBG转换为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆