1 | // [Blend2D] |
2 | // 2D Vector Graphics Powered by a JIT Compiler. |
3 | // |
4 | // [License] |
5 | // Zlib - See LICENSE.md file in the package. |
6 | |
7 | #ifndef BLEND2D_BLRGBA_H |
8 | #define BLEND2D_BLRGBA_H |
9 | |
10 | #include "./blapi.h" |
11 | |
12 | BL_DIAGNOSTIC_PUSH(BL_DIAGNOSTIC_NO_SHADOW) |
13 | |
14 | //! \addtogroup blend2d_api_styling |
15 | //! \{ |
16 | |
17 | // ============================================================================ |
18 | // [BLRgba32] |
19 | // ============================================================================ |
20 | |
21 | //! 32-bit RGBA color (8-bit per component) stored as `0xAARRGGBB`. |
22 | struct BLRgba32 { |
23 | union { |
24 | uint32_t value; |
25 | struct { |
26 | #if BL_BYTE_ORDER == 1234 |
27 | uint32_t b : 8; |
28 | uint32_t g : 8; |
29 | uint32_t r : 8; |
30 | uint32_t a : 8; |
31 | #else |
32 | uint32_t a : 8; |
33 | uint32_t r : 8; |
34 | uint32_t g : 8; |
35 | uint32_t b : 8; |
36 | #endif |
37 | }; |
38 | }; |
39 | |
40 | // -------------------------------------------------------------------------- |
41 | #ifdef __cplusplus |
42 | //! \name Construction & Destruction |
43 | //! \{ |
44 | |
45 | BL_INLINE BLRgba32() noexcept = default; |
46 | constexpr BLRgba32(const BLRgba32&) noexcept = default; |
47 | constexpr explicit BLRgba32(uint32_t rgba32) noexcept : value(rgba32) {} |
48 | |
49 | BL_INLINE explicit BLRgba32(const BLRgba64& rgba64) noexcept { reset(rgba64); } |
50 | constexpr BLRgba32(uint32_t r, uint32_t g, uint32_t b, uint32_t a = 0xFFu) noexcept |
51 | : value((a << 24) | |
52 | (r << 16) | |
53 | (g << 8) | |
54 | (b ) ) {} |
55 | |
56 | //! \} |
57 | |
58 | //! \name Overloaded Operators |
59 | //! \{ |
60 | |
61 | constexpr explicit operator bool() const noexcept { return this->value != 0; } |
62 | |
63 | BL_INLINE bool operator==(const BLRgba32& other) const noexcept { return equals(other); } |
64 | BL_INLINE bool operator!=(const BLRgba32& other) const noexcept { return !equals(other); } |
65 | |
66 | //! \} |
67 | |
68 | //! \name Common Functionality |
69 | //! \{ |
70 | |
71 | BL_INLINE void reset() noexcept { this->value = 0u; } |
72 | BL_INLINE void reset(uint32_t rgba32) noexcept { this->value = rgba32;} |
73 | BL_INLINE void reset(uint32_t r, uint32_t g, uint32_t b, uint32_t a = 0xFFu) noexcept { *this = BLRgba32(r, g, b, a); } |
74 | |
75 | BL_INLINE void reset(const BLRgba32& rgba32) noexcept { value = rgba32.value; } |
76 | BL_INLINE void reset(const BLRgba64& rgba64) noexcept; |
77 | |
78 | BL_INLINE bool equals(const BLRgba32& other) const noexcept { return blEquals(this->value, other.value); } |
79 | |
80 | //! \} |
81 | |
82 | //! \name Utilities |
83 | //! \{ |
84 | |
85 | //! Tests whether the color is fully-opaque (alpha equals 0xFFFF). |
86 | constexpr bool isOpaque() const noexcept { return this->value >= 0xFF000000u; } |
87 | //! Tests whether the color is fully-transparent (alpha equals 0). |
88 | constexpr bool isTransparent() const noexcept { return this->value <= 0x00FFFFFFu; } |
89 | |
90 | //! \} |
91 | #endif |
92 | // -------------------------------------------------------------------------- |
93 | }; |
94 | |
95 | // ============================================================================ |
96 | // [BLRgba64] |
97 | // ============================================================================ |
98 | |
99 | //! 64-bit RGBA color (16-bit per component) stored as `0xAAAARRRRGGGGBBBB`. |
100 | struct BLRgba64 { |
101 | union { |
102 | uint64_t value; |
103 | struct { |
104 | #if BL_BYTE_ORDER == 1234 |
105 | uint32_t b : 16; |
106 | uint32_t g : 16; |
107 | uint32_t r : 16; |
108 | uint32_t a : 16; |
109 | #else |
110 | uint32_t a : 16; |
111 | uint32_t r : 16; |
112 | uint32_t g : 16; |
113 | uint32_t b : 16; |
114 | #endif |
115 | }; |
116 | }; |
117 | |
118 | // -------------------------------------------------------------------------- |
119 | #ifdef __cplusplus |
120 | //! \name Construction & Destruction |
121 | //! \{ |
122 | |
123 | BL_INLINE BLRgba64() noexcept = default; |
124 | constexpr BLRgba64(const BLRgba64&) noexcept = default; |
125 | constexpr explicit BLRgba64(uint64_t rgba64) noexcept : value(rgba64) {} |
126 | |
127 | constexpr BLRgba64(uint32_t r, uint32_t g, uint32_t b, uint32_t a = 0xFFFFu) noexcept |
128 | : value(((uint64_t)a << 48) | |
129 | ((uint64_t)r << 32) | |
130 | ((uint64_t)g << 16) | |
131 | ((uint64_t)b ) ) {} |
132 | |
133 | BL_INLINE explicit BLRgba64(const BLRgba32& rgba32) noexcept { reset(rgba32); } |
134 | |
135 | //! \} |
136 | |
137 | //! \name Overloaded Operators |
138 | //! \{ |
139 | |
140 | constexpr explicit operator bool() const noexcept { return this->value != 0; } |
141 | |
142 | BL_INLINE bool operator==(const BLRgba64& other) const noexcept { return equals(other); } |
143 | BL_INLINE bool operator!=(const BLRgba64& other) const noexcept { return !equals(other); } |
144 | |
145 | //! \} |
146 | |
147 | //! \name Common Functionality |
148 | //! \{ |
149 | |
150 | BL_INLINE void reset() noexcept { this->value = 0u; } |
151 | BL_INLINE void reset(uint64_t rgba64) noexcept { this->value = rgba64; } |
152 | BL_INLINE void reset(uint32_t r, uint32_t g, uint32_t b, uint32_t a = 0xFFFFu) noexcept { *this = BLRgba64(r, g, b, a); } |
153 | |
154 | BL_INLINE void reset(const BLRgba64& rgba64) noexcept { this->value = rgba64.value; } |
155 | BL_INLINE void reset(const BLRgba32& rgba32) noexcept { |
156 | reset(rgba32.r | (uint32_t(rgba32.r) << 8u), |
157 | rgba32.g | (uint32_t(rgba32.g) << 8u), |
158 | rgba32.b | (uint32_t(rgba32.b) << 8u), |
159 | rgba32.a | (uint32_t(rgba32.a) << 8u)); |
160 | } |
161 | |
162 | BL_INLINE bool equals(const BLRgba64& other) const noexcept { return blEquals(this->value, other.value); } |
163 | |
164 | //! \} |
165 | |
166 | //! \name Utilities |
167 | //! \{ |
168 | |
169 | //! Tests whether the color is fully-opaque (alpha equals 0xFFFF). |
170 | constexpr bool isOpaque() const noexcept { return this->value >= 0xFFFF000000000000u; } |
171 | //! Tests whether the color is fully-transparent (alpha equals 0). |
172 | constexpr bool isTransparent() const noexcept { return this->value <= 0x0000FFFFFFFFFFFFu; } |
173 | |
174 | //! \} |
175 | #endif |
176 | // -------------------------------------------------------------------------- |
177 | }; |
178 | |
179 | // ============================================================================ |
180 | // [BLRgba128] |
181 | // ============================================================================ |
182 | |
183 | //! 128-bit RGBA color stored as 4 32-bit floating point values in [RGBA] order. |
184 | struct BLRgba128 { |
185 | float r; |
186 | float g; |
187 | float b; |
188 | float a; |
189 | |
190 | // -------------------------------------------------------------------------- |
191 | #ifdef __cplusplus |
192 | //! \name Construction & Destruction |
193 | //! \{ |
194 | |
195 | BL_INLINE BLRgba128() noexcept = default; |
196 | constexpr BLRgba128(const BLRgba128&) noexcept = default; |
197 | |
198 | constexpr BLRgba128(float r, float g, float b, float a = 1.0f) noexcept |
199 | : r(r), |
200 | g(g), |
201 | b(b), |
202 | a(a) {} |
203 | |
204 | //! \} |
205 | |
206 | //! \name Overloaded Operators |
207 | //! \{ |
208 | |
209 | constexpr explicit operator bool() const noexcept { |
210 | return (this->r == 0.0f) & |
211 | (this->g == 0.0f) & |
212 | (this->b == 0.0f) & |
213 | (this->a == 0.0f) ; |
214 | } |
215 | |
216 | BL_INLINE bool operator==(const BLRgba128& other) const noexcept { return equals(other); } |
217 | BL_INLINE bool operator!=(const BLRgba128& other) const noexcept { return !equals(other); } |
218 | |
219 | //! \} |
220 | |
221 | //! \name Common Functionality |
222 | //! \{ |
223 | |
224 | BL_INLINE void reset() noexcept { |
225 | reset(0.0f, 0.0f, 0.0f, 0.0f); |
226 | } |
227 | |
228 | BL_INLINE void reset(float r, float g, float b, float a = 1.0f) noexcept { |
229 | this->r = r; |
230 | this->g = g; |
231 | this->b = b; |
232 | this->a = a; |
233 | } |
234 | |
235 | BL_INLINE bool equals(const BLRgba128& other) const noexcept { |
236 | return blEquals(this->r, other.r) & |
237 | blEquals(this->g, other.g) & |
238 | blEquals(this->b, other.b) & |
239 | blEquals(this->a, other.a) ; |
240 | } |
241 | |
242 | //! \} |
243 | |
244 | //! \name Utilities |
245 | //! \{ |
246 | |
247 | //! Tests whether the color is fully-opaque (alpha equals 1.0). |
248 | constexpr bool isOpaque() const noexcept { return this->a >= 1.0; } |
249 | //! Tests whether the color is fully-transparent (alpha equals 0.0). |
250 | constexpr bool isTransparent() const noexcept { return this->a == 0.0; } |
251 | |
252 | //! \} |
253 | #endif |
254 | // -------------------------------------------------------------------------- |
255 | }; |
256 | |
257 | // ============================================================================ |
258 | // [Out of Class] |
259 | // ============================================================================ |
260 | |
261 | #ifdef __cplusplus |
262 | BL_INLINE void BLRgba32::reset(const BLRgba64& rgba64) noexcept { |
263 | uint32_t hi = uint32_t(rgba64.value >> 32); |
264 | uint32_t lo = uint32_t(rgba64.value & 0xFFFFFFFFu); |
265 | |
266 | this->value = ((hi & 0xFF000000) ) + |
267 | ((lo & 0xFF000000) >> 16) + |
268 | ((hi & 0x0000FF00) << 8) + |
269 | ((lo & 0x0000FF00) >> 8) ; |
270 | } |
271 | #endif |
272 | |
273 | // ============================================================================ |
274 | // [Constraints] |
275 | // ============================================================================ |
276 | |
277 | #ifdef __cplusplus |
278 | static_assert(sizeof(BLRgba32) == 4, "'BLRgba32' struct must be exactly 4 bytes long" ); |
279 | static_assert(sizeof(BLRgba64) == 8, "'BLRgba64' struct must be exactly 8 bytes long" ); |
280 | static_assert(sizeof(BLRgba128) == 16, "'BLRgba128' struct must be exactly 16 bytes long" ); |
281 | #endif |
282 | |
283 | //! \} |
284 | |
285 | BL_DIAGNOSTIC_POP |
286 | |
287 | #endif // BLEND2D_BLRGBA_H |
288 | |