c++ - alpha 混合后获取像素颜色

标签 c++ colors rgb alpha rgba

我有一个 RGBA 颜色,例如

255、0、0、100

如果放在白色背景上,我怎样才能得到 RGB? 例如。这种红色会变得更浅,更像

255、100、100

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

这是否足够有意义?

最好是c++

最佳答案

以我的评论为例:

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);
}

关于c++ - alpha 混合后获取像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5149722/

相关文章:

java - 坐标超出 .setRGB 范围

c++ - 使用 boost io_service 初始化 boost udp 套接字时出错

c# - 在c#中更改标签部分的颜色

javascript - 更改悬停框内文本的颜色

android - 为什么无法识别我的 res 文件中的颜色?

python - 如何输出仅包含特定颜色范围的图像?

c++ - "empty"可变模板特化的地址

c++ - Win32 - 将文本附加到编辑控件

C++ 为什么我的对象 vector 不打印对象的变量?

c - 将BMP图像放入2D数组并在C中编辑RGB值