soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SColor.h
1#ifndef __SCOLOR__H__
2#define __SCOLOR__H__
3
4#ifndef WIN32_LEAN_AND_MEAN
5#define WIN32_LEAN_AND_MEAN
6#endif // !WIN32_LEAN_AND_MEAN
7
8#include <windows.h>
9
10#define CR_INVALID 0x00FFFFFF
11
12#ifndef GetAValue
13#define GetAValue(rgb) (LOBYTE(rgb >> 24))
14#endif // GetAValue
15
16#ifndef RGBA
17#define RGBA(r, g, b, a) (RGB(r, g, b) | (a << 24))
18#endif // RGBA
19
20/**
21 * @class SColor
22 * @brief 以ARGB格式存储颜色值的类
23 */
24class SColor {
25 public:
26 /**
27 * @brief 构造函数,使用RGB和Alpha值初始化颜色
28 * @param r 红色分量(0-255)
29 * @param g 绿色分量(0-255)
30 * @param b 蓝色分量(0-255)
31 * @param a 透明度分量(0-255,默认为255)
32 */
33 SColor(BYTE r, BYTE g, BYTE b, BYTE a = 0xFF)
34 : b(b)
35 , g(g)
36 , r(r)
37 , a(a)
38 {
39 }
40
41 /**
42 * @brief 构造函数,使用COLORREF和Alpha值初始化颜色
43 * @param cr COLORREF颜色值
44 * @param alpha 透明度分量(0-255)
45 */
46 SColor(COLORREF cr, BYTE alpha)
47 {
48 r = GetRValue(cr);
49 g = GetGValue(cr);
50 b = GetBValue(cr);
51 a = GetAValue(cr);
52 updateAlpha(alpha);
53 }
54
55 /**
56 * @brief 构造函数,使用COLORREF初始化颜色
57 * @param cr COLORREF颜色值
58 */
59 SColor(COLORREF cr)
60 {
61 r = GetRValue(cr);
62 g = GetGValue(cr);
63 b = GetBValue(cr);
64 a = GetAValue(cr);
65 }
66
67 /**
68 * @brief 将颜色转换为COLORREF格式
69 * @return COLORREF颜色值
70 */
71 const COLORREF toCOLORREF() const
72 {
73 return RGB(r, g, b) | (a << 24);
74 }
75
76 /**
77 * @brief 将颜色转换为DWORD格式(ARGB)
78 * @return DWORD格式的颜色值
79 */
80 const DWORD toARGB() const
81 {
82 DWORD crRet;
83 memcpy(&crRet, this, 4);
84 return crRet;
85 }
86
87 /**
88 * @brief 设置颜色的RGB值
89 * @param cr COLORREF颜色值
90 */
91 void setRGB(COLORREF cr)
92 {
93 r = GetRValue(cr);
94 g = GetGValue(cr);
95 b = GetBValue(cr);
96 a = GetAValue(cr);
97 }
98
99 /**
100 * @brief 设置颜色的RGB值和Alpha值
101 * @param cr COLORREF颜色值
102 * @param alpha 透明度分量(0-255)
103 */
104 void setRGB(COLORREF cr, BYTE alpha)
105 {
106 r = GetRValue(cr);
107 g = GetGValue(cr);
108 b = GetBValue(cr);
109 a = GetAValue(cr);
110 updateAlpha(alpha);
111 }
112
113 /**
114 * @brief 更新颜色的Alpha值
115 * @param alpha 透明度分量(0-255)
116 */
117 void updateAlpha(BYTE alpha)
118 {
119 if (alpha != 0xFF)
120 {
121 if (a == 0xFF)
122 a = alpha;
123 else
124 a = (a * alpha) / 0xFF;
125 }
126 }
127
128 DWORD b : 8; ///< 蓝色分量(0-255)
129 DWORD g : 8; ///< 绿色分量(0-255)
130 DWORD r : 8; ///< 红色分量(0-255)
131 DWORD a : 8; ///< 透明度分量(0-255)
132};
133
134#endif // __SCOLOR__H__
DWORD g
绿色分量(0-255)
Definition SColor.h:129
SColor(BYTE r, BYTE g, BYTE b, BYTE a=0xFF)
构造函数,使用RGB和Alpha值初始化颜色
Definition SColor.h:33
void setRGB(COLORREF cr)
设置颜色的RGB值
Definition SColor.h:91
SColor(COLORREF cr)
构造函数,使用COLORREF初始化颜色
Definition SColor.h:59
void setRGB(COLORREF cr, BYTE alpha)
设置颜色的RGB值和Alpha值
Definition SColor.h:104
const COLORREF toCOLORREF() const
将颜色转换为COLORREF格式
Definition SColor.h:71
const DWORD toARGB() const
将颜色转换为DWORD格式(ARGB)
Definition SColor.h:80
DWORD b
蓝色分量(0-255)
Definition SColor.h:128
DWORD r
红色分量(0-255)
Definition SColor.h:130
SColor(COLORREF cr, BYTE alpha)
构造函数,使用COLORREF和Alpha值初始化颜色
Definition SColor.h:46
DWORD a
透明度分量(0-255)
Definition SColor.h:131
void updateAlpha(BYTE alpha)
更新颜色的Alpha值
Definition SColor.h:117