22#define KLIB_COLOR_HPP
33
44#include < cstdint>
5+ #include < span>
56
67#include < klib/math.hpp>
78
@@ -21,10 +22,13 @@ namespace klib::graphics {
2122 // blue color
2223 uint8_t blue;
2324
24- // is pixel is transparant
25- bool transparant ;
25+ // alpha channel
26+ uint8_t alpha ;
2627 };
2728
29+ // validate the size of the color struct is the correct size
30+ static_assert (sizeof (color) == sizeof (uint32_t ), " Invalid size for color structure" );
31+
2832 // some default colors
2933 [[maybe_unused]]
3034 constexpr color white = {0xff , 0xff , 0xff , false };
@@ -172,9 +176,19 @@ namespace klib::graphics::detail {
172176 constexpr static uint32_t bits = 24 ;
173177 };
174178
179+ // specialization for argb8888
180+ template <>
181+ struct pixel_conversion <mode::argb8888> {
182+ using type = uint32_t ;
183+
184+ // amount of bits used in type
185+ constexpr static uint32_t bits = 32 ;
186+ };
187+
175188 /* *
176- * @brief Generic Conversion from a klib color to a display mode. Transparant
177- * pixels are not handled in this conversion
189+ * @brief Generic Conversion from a klib color to a display mode. The alpha
190+ * channel is not handled in this conversion for color types except the
191+ * A versions
178192 *
179193 * @tparam Mode
180194 * @param col
@@ -207,6 +221,9 @@ namespace klib::graphics::detail {
207221 else if constexpr (Mode == mode::rgb666) {
208222 return ((col.blue >> 2 ) << 12 ) | ((col.green >> 2 ) << 6 ) | (col.red >> 2 );
209223 }
224+ else if constexpr (Mode == mode::argb8888) {
225+ return (col.alpha << 24 ) | (col.red << 16 ) | (col.green << 8 ) | (col.blue );
226+ }
210227 else {
211228 // return rgb888 when not in other cases
212229 return (col.red << 16 ) | (col.green << 8 ) | (col.blue );
@@ -226,7 +243,7 @@ namespace klib::graphics::detail {
226243 .red = klib::map (static_cast <uint8_t >((raw >> 4 ) & 0x3 ), 0 , 0x3 , 0 , 255 ),
227244 .green = klib::map (static_cast <uint8_t >((raw >> 2 ) & 0x3 ), 0 , 0x3 , 0 , 255 ),
228245 .blue = klib::map (static_cast <uint8_t >(raw & 0x3 ), 0 , 0x3 , 0 , 255 ),
229- .transparant = false
246+ .alpha = 0xff ,
230247 };
231248 }
232249 else if constexpr (Mode == mode::rgb232) {
@@ -235,7 +252,7 @@ namespace klib::graphics::detail {
235252 .red = klib::map (static_cast <uint8_t >((raw >> 5 ) & 0x3 ), 0 , 0x3 , 0 , 255 ),
236253 .green = klib::map (static_cast <uint8_t >((raw >> 2 ) & 0x7 ), 0 , 0x7 , 0 , 255 ),
237254 .blue = klib::map (static_cast <uint8_t >(raw & 0x3 ), 0 , 0x3 , 0 , 255 ),
238- .transparant = false
255+ .alpha = 0xff ,
239256 };
240257 }
241258 else if constexpr (Mode == mode::rgb242) {
@@ -244,39 +261,47 @@ namespace klib::graphics::detail {
244261 .red = klib::map (static_cast <uint8_t >((raw >> 6 ) & 0x3 ), 0 , 0x3 , 0 , 255 ),
245262 .green = klib::map (static_cast <uint8_t >((raw >> 2 ) & 0xf ), 0 , 0xf , 0 , 255 ),
246263 .blue = klib::map (static_cast <uint8_t >(raw & 0x3 ), 0 , 0x3 , 0 , 255 ),
247- .transparant = false
264+ .alpha = 0xff ,
248265 };
249266 }
250267 else if constexpr (Mode == mode::rgb444) {
251268 return {
252269 .red = klib::map (static_cast <uint8_t >((raw >> 8 ) & 0xf ), 0 , 0xf , 0 , 255 ),
253270 .green = klib::map (static_cast <uint8_t >((raw >> 4 ) & 0xf ), 0 , 0xf , 0 , 255 ),
254271 .blue = klib::map (static_cast <uint8_t >(raw & 0xf ), 0 , 0xf , 0 , 255 ),
255- .transparant = false
272+ .alpha = 0xff ,
256273 };
257274 }
258275 else if constexpr (Mode == mode::rgb565) {
259276 return {
260277 .red = klib::map (static_cast <uint8_t >((raw >> 11 ) & 0x1f ), 0 , 0x1f , 0 , 255 ),
261278 .green = klib::map (static_cast <uint8_t >((raw >> 5 ) & 0x3f ), 0 , 0x3f , 0 , 255 ),
262279 .blue = klib::map (static_cast <uint8_t >(raw & 0x1f ), 0 , 0x1f , 0 , 255 ),
263- .transparant = false
280+ .alpha = 0xff ,
264281 };
265282 }
266283 else if constexpr (Mode == mode::bgr565) {
267284 return {
268285 .red = klib::map (static_cast <uint8_t >(raw & 0x1f ), 0 , 0x1f , 0 , 255 ),
269286 .green = klib::map (static_cast <uint8_t >((raw >> 5 ) & 0x3f ), 0 , 0x3f , 0 , 255 ),
270287 .blue = klib::map (static_cast <uint8_t >((raw >> 11 ) & 0x1f ), 0 , 0x1f , 0 , 255 ),
271- .transparant = false
288+ .alpha = 0xff ,
272289 };
273290 }
274291 else if constexpr (Mode == mode::rgb666) {
275292 return {
276293 .red = klib::map (static_cast <uint8_t >((raw >> 12 ) & 0x3f ), 0 , 0x3f , 0 , 255 ),
277294 .green = klib::map (static_cast <uint8_t >((raw >> 6 ) & 0x3f ), 0 , 0x3f , 0 , 255 ),
278295 .blue = klib::map (static_cast <uint8_t >(raw & 0x3f ), 0 , 0x3f , 0 , 255 ),
279- .transparant = false
296+ .alpha = 0xff ,
297+ };
298+ }
299+ else if constexpr (Mode == mode::argb8888) {
300+ return {
301+ .red = (static_cast <uint8_t >((raw >> 16 ) & 0xff )),
302+ .green = (static_cast <uint8_t >((raw >> 8 ) & 0xff )),
303+ .blue = (static_cast <uint8_t >(raw & 0xff )),
304+ .alpha = (static_cast <uint8_t >((raw >> 24 ) & 0xff )),
280305 };
281306 }
282307 else {
@@ -285,7 +310,7 @@ namespace klib::graphics::detail {
285310 .red = (static_cast <uint8_t >((raw >> 16 ) & 0xff )),
286311 .green = (static_cast <uint8_t >((raw >> 8 ) & 0xff )),
287312 .blue = (static_cast <uint8_t >(raw & 0xff )),
288- .transparant = false
313+ .alpha = 0xff
289314 };
290315 }
291316 }
0 commit comments