1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#pragma once
14
15#include <stdint.h>
16#include <byteswap.h>
17#include <endian.h>
18
19typedef union colour_rgb24 {
20    struct {
21        uint8_t __padding;
22        uint8_t red;
23        uint8_t green;
24        uint8_t blue;
25    } as_rgb;
26    uint32_t as_u32;
27} colour_rgb24_t;
28
29static inline colour_rgb24_t colour_rgb24_create(uint8_t red, uint8_t green, uint8_t blue) {
30    return (colour_rgb24_t) { .as_rgb = {
31        .__padding = 0,
32        .red = red,
33        .green = green,
34        .blue = blue
35    }};
36}
37
38static inline colour_rgb24_t colour_rgb24_decode_u32(uint32_t colour) {
39#if __BYTE_ORDER == __BIG_ENDIAN
40    return (colour_rgb24_t) { .as_u32 = colour };
41#else
42    return (colour_rgb24_t) { .as_u32 = __bswap_32(colour) };
43#endif
44}
45
46static inline uint32_t colour_rgb24_encode_u32(colour_rgb24_t colour) {
47#if __BYTE_ORDER == __BIG_ENDIAN
48    return colour.as_u32;
49#else
50    return __bswap_32(colour.as_u32);
51#endif
52}
53