获取 Alpha 混合后的像素颜色

发布于 2024-10-19 19:13:29 字数 171 浏览 0 评论 0原文

我有一个 RGBA 颜色,例如

255, 0, 0, 100

如果将其放在白色背景上,如何获得 RGB? 例如。这种红色会变得更亮,更像

255, 100, 100

如果把它放在黑色背景上也是一样。

这有足够的意义吗?

最好是c++

I have a RGBA color like

255, 0, 0, 100

How can I get the RGB if it was put onto a white background?
eg. this red would become lighter and be more like

255, 100, 100

And the same thing if it were put onto a black background.

Does this make enough sense?

Preferably c++

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-10-26 19:13:29

作为我的评论的一个例子:

struct Color
{
    int R;
    int G;
    int B;
    int A;
};

Color Blend(Color c1, Color c2)
{
    Color result;
    double a1 = c1.A / 255.0;
    double a2 = c2.A / 255.0;

    result.R = (int) (a1 * c1.R + a2 * (1 - a1) * c2.R);
    result.G = (int) (a1 * c1.G + a2 * (1 - a1) * c2.G);
    result.B = (int) (a1 * c1.B + a2 * (1 - a1) * c2.B);
    result.A = (int) (255 * (a1 + a2 * (1 - a1)));
    return result;
}


void Example()
{
    Color c1;
    c1.R = 255;
    c1.G = 0;
    c1.B = 0;
    c1.A = 100;

    Color c2;
    c2.R = 255;
    c2.G = 255;
    c2.B = 255;

    Color blended = Blend(c1, c2);
    int x = 50;
    int y = 100;

    // Pretend function that draws a pixel at (x, y) with rgb values
    DrawPixel(x, y, blended.R, blended.G, blended.B);
}

As an example to my comment:

struct Color
{
    int R;
    int G;
    int B;
    int A;
};

Color Blend(Color c1, Color c2)
{
    Color result;
    double a1 = c1.A / 255.0;
    double a2 = c2.A / 255.0;

    result.R = (int) (a1 * c1.R + a2 * (1 - a1) * c2.R);
    result.G = (int) (a1 * c1.G + a2 * (1 - a1) * c2.G);
    result.B = (int) (a1 * c1.B + a2 * (1 - a1) * c2.B);
    result.A = (int) (255 * (a1 + a2 * (1 - a1)));
    return result;
}


void Example()
{
    Color c1;
    c1.R = 255;
    c1.G = 0;
    c1.B = 0;
    c1.A = 100;

    Color c2;
    c2.R = 255;
    c2.G = 255;
    c2.B = 255;

    Color blended = Blend(c1, c2);
    int x = 50;
    int y = 100;

    // Pretend function that draws a pixel at (x, y) with rgb values
    DrawPixel(x, y, blended.R, blended.G, blended.B);
}
泪意 2024-10-26 19:13:29

It depends on the blending function.

Nine different blending functions, with a formula for each, are described in the glBlendFunc documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文