# 『转载』两个RGBA四通道颜色的叠加计算方法与代码实现

1 class CColor
2 {
3 public:
4     CColor(unsigned char a, unsigned char r, unsigned char g, unsigned char b)
5     {
6         m_alph = a;
7         m_red = r;
8         m_green = g;
9         m_blue = b;
10     }
11     // others...
12 private:
13     unsigned char m_alph;
14     unsigned char m_red;
15     unsigned char m_green;
16     unsigned char m_blue;
17 }
18
19 float crCalculateBlend(float a1, float a2, float c1, float c2)
20 {
21     return (c1 * a1 * (1.0 - a2) + c2 * a2) / (a1 + a2 - a1 * a2);
22 }
23
24 const CColor crColorBlend(const CColor& cor1, const CColor& cor2)
25 {
26     float fAlp1 = cor1.GetAlpha() / 255.0;
27     float fAlp2 = cor2.GetAlpha() / 255.0;
28     float fAlpBlend = fAlp1 + fAlp2 - fAlp1 * fAlp2;
29
30     float fRed1 = cor1.GetRed() / 255.0;
31     float fRed2 = cor2.GetRed() / 255.0;
32     float fRedBlend = crCalculateBlend(fAlp1, fAlp2, fRed1, fRed2);
33
34     float fGreen1 = cor1.GetGreen() / 255.0;
35     float fGreen2 = cor2.GetGreen() / 255.0;
36     float fGreenBlend = crCalculateBlend(fAlp1, fAlp2, fGreen1, fGreen2);
37
38     float fBlue1 = cor1.GetBlue() / 255.0;
39     float fBlue2 = cor2.GetBlue() / 255.0;
40     float fBlueBlend = crCalculateBlend(fAlp1, fAlp2, fBlue1, fBlue2);
41
42     return CColor(fAlpBlend * 255, fRedBlend * 255, fGreenBlend * 255, fBlueBlend * 255);
43 }

posted @ 2020-01-02 16:27  夕颜花开  阅读(1395)  评论(1编辑  收藏  举报