如何将 RGBA 颜色元组,例如 (96, 96, 96, 202) 转换为对应的 RGB 颜色元组?
编辑:
我想要的是在白色背景上获得一个与 RGBA 元组视觉上最相似的 RGB 值。
我赞成约翰内斯的回答,因为他是对的。
* 有人提出了一些意见,说我原来的答案不正确。如果 alpha 值与正常值相反,它会起作用。但是,根据定义,这在大多数情况下都行不通。因此,我更新了下面的公式,使其适用于正常情况。这最终等于下面@hkurabko的答案*
然而,更具体的答案是将 alpha 值合并到基于不透明背景颜色(或称为“哑光”)的实际颜色结果中。
有一个算法(来自这个维基百科链接):
Source
。BGColor
Note - 如果背景颜色也是透明的,那么您必须首先递归该过程(再次选择遮罩)以获得此操作的源 RGB。现在,转换被定义为(这里是完整的伪代码!):
Source => Target = (BGColor + Source) =
Target.R = ((1 - Source.A) * BGColor.R) + (Source.A * Source.R)
Target.G = ((1 - Source.A) * BGColor.G) + (Source.A * Source.G)
Target.B = ((1 - Source.A) * BGColor.B) + (Source.A * Source.B)
要为您获得最终的 0-255 值,Target
只需将所有归一化值乘以 255,如果任何组合值超过 1.0,请确保上限为 255(这是过度曝光,有更复杂的算法处理这个问题涉及整个图像处理等)。
编辑:在你的问题中,你说你想要一个白色背景 - 在这种情况下,只需将 BGColor 修复为 255,255,255。
嗯……关于
http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
Andras Zoltan 提供的解决方案应稍微更改为:
Source => Target = (BGColor + Source) =
Target.R = ((1 - Source.A) * BGColor.R) + (Source.A * Source.R)
Target.G = ((1 - Source.A) * BGColor.G) + (Source.A * Source.G)
Target.B = ((1 - Source.A) * BGColor.B) + (Source.A * Source.B)
这个更改后的版本对我来说很好,因为在 prev。带有哑光 rgb(ff,ff,ff) 的版本 rgba(0,0,0,0) 将更改为 rgb(0,0,0)。
就我而言,我想将 RGBA 图像转换为 RGB 并且以下内容按预期工作:
rgbImage = cv2.cvtColor(npimage, cv2.COLOR_RGBA2RGB)
这取决于您使用的色彩空间。如果 RGBA 在预乘颜色空间中并且是半透明的,则需要除掉 alpha 以获得正确的 RGB 颜色。如果颜色在非预乘颜色空间中,那么您可以丢弃 Alpha 通道。
这是一些java代码(适用于Android API 24):
//int rgb_background = Color.parseColor("#ffffff"); //white background
//int rgba_color = Color.parseColor("#8a000000"); //textViewColor
int defaultTextViewColor = textView.getTextColors().getDefaultColor();
int argb = defaultTextViewColor;
int alpha = 0xFF & (argb >> 24);
int red = 0xFF & (argb >> 16);
int green = 0xFF & (argb >> 8);
int blue = 0xFF & (argb >> 0);
float alphaFloat = (float)alpha / 255;
String colorStr = rgbaToRGB(255, 255, 255, red, green, blue, alphaFloat);
功能:
protected String rgbaToRGB(int rgb_background_red, int rgb_background_green, int rgb_background_blue,
int rgba_color_red, int rgba_color_green, int rgba_color_blue, float alpha) {
float red = (1 - alpha) * rgb_background_red + alpha * rgba_color_red;
float green = (1 - alpha) * rgb_background_green + alpha * rgba_color_green;
float blue = (1 - alpha) * rgb_background_blue + alpha * rgba_color_blue;
String redStr = Integer.toHexString((int) red);
String greenStr = Integer.toHexString((int) green);
String blueStr = Integer.toHexString((int) blue);
String colorHex = "#" + redStr + greenStr + blueStr;
//return Color.parseColor(colorHex);
return colorHex;
}
根据 Andras 和 hkurabko 的回答,这是一个方便的 SASS 函数。
@function rgba_blend($fore, $back) {
$ored: ((1 - alpha($fore)) * red($back) ) + (alpha($fore) * red($fore));
$ogreen: ((1 - alpha($fore)) * green($back) ) + (alpha($fore) * green($fore));
$oblue: ((1 - alpha($fore)) * blue($back) ) + (alpha($fore) * blue($fore));
@return rgb($ored, $ogreen, $oblue);
}
用法:
$my_color: rgba(red, 0.5); // build a color with alpha for below
#a_div {
background-color: rgba_blend($my_color, white);
}
Python函数按照hkurabko的回答。
def rgba2rgb(rgba: tuple[int, int, int, float], background: tuple[int, int, int] = (255, 255, 255)):
return (
round(((1 - rgba[3]) * background[0]) + (rgba[3] * rgba[0])),
round(((1 - rgba[3]) * background[1]) + (rgba[3] * rgba[1])),
round(((1 - rgba[3]) * background[2]) + (rgba[3] * rgba[2])),
)