考虑背景将RGBA转换为RGB

7

可能是重复问题:
将RGBA颜色转换为RGB

我正在尝试将一个带有alpha < 1的RGBA颜色转换为纯RGB表示,并考虑背景颜色。

使用这个问题提供的算法,我成功地将其转换为纯RGB颜色 - 但仅在alpha = 0.5时。

这是我的测试代码:

<!DOCTYPE html>
<html>
<head></head>
<body>
    <script type="text/javascript">
    // Basic RGB(A) to CSS property value
    function _toString(obj) {
        var type = 'rgb', out = obj.red + ', ' + obj.green + ', ' + obj.blue;

        if (obj.alpha !== undefined) {
            type += 'a';
            out += ', ' + obj.alpha;
        }

        return type + '(' + out + ')';
    }

    // Background color, assume this is always RGB
    var bg = {red: 255, green: 51, blue: 0};
    // RGBA color
    var RGBA = {red: 0, green: 102, blue: 204, alpha: 0};
    // Output RGB
    var RGB = {red: null, green: null, blue: null};
    // Just a cache...
    var alpha;

    while (RGBA.alpha < 1) {
        alpha = 1 - RGBA.alpha;
        RGB.red = Math.round((alpha * (RGBA.red / 255) + ((1 - RGBA.alpha) * (bg.red / 255))) * 255);
        RGB.green = Math.round((alpha * (RGBA.green / 255) + ((1 - RGBA.alpha) * (bg.green / 255))) * 255);
        RGB.blue = Math.round((alpha * (RGBA.blue / 255) + ((1 - RGBA.alpha) * (bg.blue / 255))) * 255);

        document.write('<div style="display: block; width: 150px; height: 100px; background-color: ' + _toString(bg) + '">\
            <div style="color: #fff; width: 50px; height: 50px; background-color: ' + _toString(RGBA) + '"><small>RGBA<br>' + RGBA.alpha + '</small></div>\
            <div style="color: #fff; width: 50px; height: 50px; background-color: ' + _toString(RGB) + '"><small>RGB<br>' + RGBA.alpha + '</small></div>\
        </div>');

        // Increment alpha
        RGBA.alpha += 0.25;
    }
    </script>
</body>
</html>

在Chrome和Firefox中运行上述代码,当alpha为0.5时成功地将RGBA转换为RGB,任何偏离0.5的值都会导致不匹配,如果偏差非常小(即alpha为0.55时可能会注意到问题),则非常微妙。
我已经多次重写了逻辑,并将逻辑完全展开为其最基本的部分,但仍未成功。
1个回答

10

看起来你正在尝试使用常见的混合方法,但增量循环让我感到困惑。从OpenGL FAQ中获取:

“如上所述的典型用法[混合]通过其相关联的 Alpha 值修改传入的颜色,并将目标颜色修改为 1 减去传入的 Alpha 值。这两种颜色的和随后写回帧缓冲区。”

因此,不要使用 while 循环,而是使用:

alpha = 1 - RGBA.alpha;
RGB.red = Math.round((RGBA.alpha * (RGBA.red / 255) + (alpha * (bg.red / 255))) * 255);
RGB.green = Math.round((RGBA.alpha * (RGBA.green / 255) + (alpha * (bg.green / 255))) * 255);
RGB.blue = Math.round((RGBA.alpha * (RGBA.blue / 255) + (alpha * (bg.blue / 255))) * 255);

太棒了!将逻辑更改为您自己的(使用背景上计算出的alpha而不是前景),我得到了由循环确定的不同alpha的正确值!太棒了! - seeSaw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接