[英]Algorithm to convert semi-transparent to solid color
Let's say I have the color rgba(255, 0, 0, 0.5) on a white background. 假设我在白色背景上具有颜色rgba(255,0,0,0.5)。 The color that can bee seen is almost identical to rgba(255, 140, 140, 1) which is a solid color.
可以看到的颜色与纯色rgba(255,140,140,1)几乎相同。 I'm looking for an algorithm that converts a semi-transparent color (over white) to a solid color so that it looks (pretty much) the same when it's one near each other (like in the photo below)
我正在寻找一种将半透明颜色(白色)转换为纯色的算法,以便当彼此靠近时看起来(几乎)相同(如下图所示)
I noticed that if there are 2 components (r, g, or b) that are 0, then you have to keep the not-null component's value and adjust the null components by the same value so that it matches the wight color. 我注意到,如果有2个分量(r,g或b)为0,则必须保留非空分量的值,并以相同的值调整空分量,以使其与怀特颜色匹配。 This value (I guess) depends on the alpha component of semi-transparent color.
这个值(我猜)取决于半透明颜色的alpha分量。 But I can't seem to find the formula.
但我似乎找不到公式。 How about the rest of the case?
其余的情况如何? I think there must be a general formula that can be applied.
我认为必须有一个通用的公式可以应用。
I'm looking for an answer in pseudocode, javascript, Java, Python, C# or C++. 我正在寻找伪代码,javascript,Java,Python,C#或C ++的答案。
Also, I am not working on any project. 另外,我没有从事任何项目。 This question is for learning purposes and for helping people that might need this.
这个问题是为了学习目的,是为了帮助可能需要它的人。
I know this formula to convert from rgba to rgb 我知道这个公式可以从rgba转换为rgb
function rgba2rgb(background, color) {
const alpha = color[3]
return [Math.floor((1 - alpha) * background[0] + alpha * color[0] + 0.5),
Math.floor((1 - alpha) * background[1] + alpha * color[1] + 0.5),
Math.floor((1 - alpha) * background[2] + alpha * color[2] + 0.5)]
}
console.log(rgba2rgb([255, 255, 255], [255, 0, 0, 0.5])) // [ 255, 128, 128 ]
But it's not right for your example 但这不适合您的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.