BIMHome v1.0.0
BIMHome接口文档说明
Color.h
浏览该文件的文档.
1/************************************************************************
2* @file Color.h
3*
4* @brief 颜色
5*
6* @details
7*
8* @author wukx
9*
10* @version 1.0
11*
12* @date 2025-06-5
13*
14* @license 北京华科软科技有限公司
15*
16*************************************************************************/
17
18#ifndef BIMHOMEAPP_APP_COLOR_H
19#define BIMHOMEAPP_APP_COLOR_H
20
21#include <sstream>
22#include <iomanip>
23#include "BHGlobal.h"
24
25namespace App
26{
27
33 class AppExport Color
34 {
35 public:
44 explicit Color(float R = 0.0, float G = 0.0, float B = 0.0, float A = 0.0)
45 : r(R), g(G), b(B), a(A) {}
46
52 Color(uint32_t rgba)
53 {
54 setPackedValue(rgba);
55 }
56
62 Color(const Color& c)
63 : r(c.r), g(c.g), b(c.b), a(c.a) {}
64
71 bool operator==(const Color& c) const
72 {
73 return getPackedValue() == c.getPackedValue();
74 }
75
82 bool operator!=(const Color& c) const
83 {
84 return !operator==(c);
85 }
86
95 void set(float R, float G, float B, float A = 0.0)
96 {
97 r = R; g = G; b = B; a = A;
98 }
99
107 {
108 r = c.r; g = c.g; b = c.b; a = c.a;
109 return *this;
110 }
111
118 Color& setPackedValue(uint32_t rgba)
119 {
120 this->set((rgba >> 24) / 255.0f,
121 ((rgba >> 16) & 0xff) / 255.0f,
122 ((rgba >> 8) & 0xff) / 255.0f,
123 (rgba & 0xff) / 255.0f);
124 return *this;
125 }
126
132 uint32_t getPackedValue() const
133 {
134 return (static_cast<uint32_t>(r * 255.0f + 0.5f) << 24 |
135 static_cast<uint32_t>(g * 255.0f + 0.5f) << 16 |
136 static_cast<uint32_t>(b * 255.0f + 0.5f) << 8 |
137 static_cast<uint32_t>(a * 255.0f + 0.5f));
138 }
139
145 template <typename T>
146 void setValue(const T& q)
147 {
148 set(q.redF(), q.greenF(), q.blueF());
149 }
150
156 void setValue(const Color& q)
157 {
158 set(q.r, q.g, q.b);
159 }
160
166 template <typename T>
167 inline T asValue() const {
168 return T(int(r * 255.0f), int(g * 255.0f), int(b * 255.0f));
169 }
170
176 std::string asHexString() const {
177 std::stringstream ss;
178 ss << "#" << std::hex << std::uppercase << std::setfill('0') << std::setw(2) << int(r * 255.0f)
179 << std::setw(2) << int(g * 255.0f)
180 << std::setw(2) << int(b * 255.0f);
181 return ss.str();
182 }
183
189 std::string asCSSString() const {
190 return asHexString();
191 }
192
199 bool fromHexString(const std::string& hex) {
200 if (hex.size() < 7 || hex[0] != '#')
201 return false;
202 // #RRGGBB
203 if (hex.size() == 7) {
204 std::stringstream ss(hex);
205 unsigned int rgb;
206 char c;
207
208 ss >> c >> std::hex >> rgb;
209 int rc = (rgb >> 16) & 0xff;
210 int gc = (rgb >> 8) & 0xff;
211 int bc = rgb & 0xff;
212
213 r = rc / 255.0f;
214 g = gc / 255.0f;
215 b = bc / 255.0f;
216
217 return true;
218 }
219 // #RRGGBBAA
220 if (hex.size() == 9) {
221 std::stringstream ss(hex);
222 unsigned int rgba;
223 char c;
224
225 ss >> c >> std::hex >> rgba;
226 int rc = (rgba >> 24) & 0xff;
227 int gc = (rgba >> 16) & 0xff;
228 int bc = (rgba >> 8) & 0xff;
229 int ac = rgba & 0xff;
230
231 r = rc / 255.0f;
232 g = gc / 255.0f;
233 b = bc / 255.0f;
234 a = ac / 255.0f;
235
236 return true;
237 }
238
239 return false;
240 }
241
243 float r, g, b, a;
244 };
245}
246
247#endif // BIMHOMEAPP_APP_COLOR_H
A
Definition ISnapProcessedBase.h:49
G
Definition ISnapProcessedBase.h:49
B
Definition ISnapProcessedBase.h:49
R
Definition ISnapProcessedBase.h:49
T
Definition ISnapProcessedBase.h:49
Color & setPackedValue(uint32_t rgba)
设置颜色值
Definition Color.h:118
std::string asHexString() const
将颜色转换为十六进制字符串
Definition Color.h:176
Color & operator=(const Color &c)
赋值运算符
Definition Color.h:106
void setValue(const Color &q)
从 Color 类型设置颜色值
Definition Color.h:156
float r
颜色值,公共可访问
Definition Color.h:243
float a
Definition Color.h:243
void setValue(const T &q)
从模板类型设置颜色值
Definition Color.h:146
Color(uint32_t rgba)
构造颜色对象
Definition Color.h:52
bool fromHexString(const std::string &hex)
从十六进制字符串设置颜色
Definition Color.h:199
float b
Definition Color.h:243
Color(float R=0.0, float G=0.0, float B=0.0, float A=0.0)
构造颜色对象
Definition Color.h:44
T asValue() const
将颜色转换为模板类型
Definition Color.h:167
float g
Definition Color.h:243
bool operator==(const Color &c) const
比较两个颜色对象是否相等
Definition Color.h:71
bool operator!=(const Color &c) const
比较两个颜色对象是否不相等
Definition Color.h:82
uint32_t getPackedValue() const
获取颜色的打包值
Definition Color.h:132
void set(float R, float G, float B, float A=0.0)
设置颜色分量
Definition Color.h:95
std::string asCSSString() const
将颜色转换为 CSS 样式的字符串
Definition Color.h:189
Color(const Color &c)
复制构造函数
Definition Color.h:62
表示颜色的类
Definition Color.h:34
Definition BaseFigureFactory.h:28