将 RGBA 颜色转换为 RGB [英] Convert RGBA color to RGB

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

问题描述

如何将 RGBA 颜色元组(例如 (96, 96, 96, 202))转换为相应的 RGB 颜色元组?

How to convert a RGBA color tuple, example (96, 96, 96, 202), to corresponding RGB color tuple?

我想要的是获得一个 RGB 值,该值与白色背景上的 RGBA 元组在视觉上最相似.

What I want is to get a RGB value which is most similar to the RGBA tuple visually on white background.

推荐答案

我赞成 Johannes 的回答,因为他是对的.

I've upvoted Johannes' answer because he's right about that.

* 有一些评论指出我的原始答案不正确.如果 alpha 值与正常值相反,它会起作用.但是,根据定义,这在大多数情况下不起作用.因此,我更新了下面的公式以使其适用于正常情况.这最终等于@hkurabko 在下面的回答 *

然而,更具体的答案是将 alpha 值合并到基于不透明背景颜色(或称为哑光")的实际颜色结果中.

A more specific answer, however, incorporates the alpha value into the actual colour result based on an opaque background colour (or 'matte' as it's referred to).

对此有一个算法(来自 this 维基百科链接):

There is an algorithm for this (from this wikipedia link):

  • 标准化 RGBA 值,使它们都在 0 和 1 之间 - 只需将每个值除以 255 即可.我们将调用结果 Source.
  • 还对哑光颜色(黑色、白色等)进行标准化.我们将结果称为 BGColor 注意 - 如果背景颜色也是透明的,那么您必须首先递归该过程(再次选择遮罩)获取此操作的源 RGB.
  • 现在,转换定义为(此处为完整的伪代码!):

  • Normalise the RGBA values so that they're all between 0 and 1 - just divide each value by 255 to do this. We'll call the result Source.
  • Normalise also the matte colour (black, white whatever). We'll call the result BGColor Note - if the background colour is also transparent, then you'll have to recurse the process for that first (again, choosing a matte) to get the source RGB for this operation.
  • Now, the conversion is defined as (in complete psuedo code here!):

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)

要获得 Target 的最终 0-255 值,您只需将所有归一化值乘以 255,如果任何组合值超过 1.0(这已经结束),请确保将上限设为 255-曝光,还有更复杂的算法来处理这个问题,包括全图像处理等).

To get the final 0-255 values for Target you simply multiply all the normalised values back up by 255, making sure you cap at 255 if any of the combined values exceed 1.0 (this is over-exposure and there are more complex algorithms dealing with this that involve whole-image processing etc.).

在你的问题中,你说你想要一个白色背景 - 在这种情况下,只需将 BGColor 修复为 255,255,255.

In your question you said you want a white background - in that case just fix BGColor to 255,255,255.

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

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