microui  14.2.0
microui
ui_color.h
1 /*
2  * C
3  *
4  * Copyright 2020-2024 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
8 #if !defined UI_COLOR_H
9 #define UI_COLOR_H
10 #if defined __cplusplus
11 extern "C" {
12 #endif
13 
14 /*
15  * @file
16  * @brief Provides macros to manipulate the color channels of the ARGB8888 pixel
17  * format.
18  * @author MicroEJ Developer Team
19  * @version 14.2.0
20  */
21 
22 // -----------------------------------------------------------------------------
23 // Macros and Defines
24 // -----------------------------------------------------------------------------
25 
26 /*
27  * @brief Mask of a channel in ARGB8888 format
28  */
29 #define COLOR_ARGB8888_CHANNEL_SIZE 8
30 
31 /*
32  * @brief Mask of a channel in ARGB8888 format
33  */
34 #define COLOR_ARGB8888_CHANNEL_MASK \
35  ((1 << COLOR_ARGB8888_CHANNEL_SIZE) - 1)
36 
37 /*
38  * @brief Mask of a channel
39  *
40  * @param format: The color format
41  * @param channel: The channel
42  */
43 #define COLOR_CHANNEL_MASK(format, channel) \
44  ((1 << COLOR_CHANNEL_SIZE(format, channel)) - 1)
45 
46 /*
47  * @brief Size of a channel
48  *
49  * @param format: The color format
50  * @param channel: The channel
51  */
52 #define COLOR_CHANNEL_SIZE(format, channel) \
53  COLOR_ ## format ## _ ## channel ## _SIZE
54 
55 /*
56  * @brief Offset of a channel
57  *
58  * @param format: The color format
59  * @param channel: The channel
60  */
61 #define COLOR_CHANNEL_OFFSET(format, channel) \
62  COLOR_ ## format ## _ ## channel ## _OFFSET
63 
64 /*
65  * @brief Alpha channel size for ARGB8888 format
66  */
67 #define COLOR_ARGB8888_ALPHA_SIZE COLOR_ARGB8888_CHANNEL_SIZE
68 
69 /*
70  * @brief Alpha channel offset for ARGB8888 format
71  */
72 #define COLOR_ARGB8888_ALPHA_OFFSET 24
73 
74 /*
75  * @brief Alpha channel mask for ARGB8888 format
76  */
77 #define COLOR_ARGB8888_ALPHA_MASK \
78  COLOR_CHANNEL_MASK(ARGB8888, ALPHA)
79 
80 /*
81  * @brief Red channel size for ARGB8888 format
82  */
83 #define COLOR_ARGB8888_RED_SIZE COLOR_ARGB8888_CHANNEL_SIZE
84 
85 /*
86  * @brief Red channel offset for ARGB8888 format
87  */
88 #define COLOR_ARGB8888_RED_OFFSET 16
89 
90 /*
91  * @brief Red channel mask for ARGB8888 format
92  */
93 #define COLOR_ARGB8888_RED_MASK \
94  COLOR_CHANNEL_MASK(ARGB8888, RED)
95 
96 /*
97  * @brief Green channel size for ARGB8888 format
98  */
99 #define COLOR_ARGB8888_GREEN_SIZE COLOR_ARGB8888_CHANNEL_SIZE
100 
101 /*
102  * @brief Green channel offset for ARGB8888 format
103  */
104 #define COLOR_ARGB8888_GREEN_OFFSET 8
105 
106 /*
107  * @brief Green channel mask for ARGB8888 format
108  */
109 #define COLOR_ARGB8888_GREEN_MASK \
110  COLOR_CHANNEL_MASK(ARGB8888, GREEN)
111 
112 /*
113  * @brief Blue channel size for ARGB8888 format
114  */
115 #define COLOR_ARGB8888_BLUE_SIZE COLOR_ARGB8888_CHANNEL_SIZE
116 
117 /*
118  * @brief Blue channel offset for ARGB8888 format
119  */
120 #define COLOR_ARGB8888_BLUE_OFFSET 0
121 
122 /*
123  * @brief Blue channel mask for ARGB8888 format
124  */
125 #define COLOR_ARGB8888_BLUE_MASK \
126  COLOR_CHANNEL_MASK(ARGB8888, BLUE)
127 
128 /*
129  * @brief Gets a channel value from a color
130  *
131  * param color: The color from which to retrieve a channel
132  * param format: The color format (i.e. ARGB8888, etc...)
133  */
134 #define COLOR_GET_CHANNEL(color, format, channel) \
135  (uint8_t)( \
136  ((color) >> COLOR_CHANNEL_OFFSET(format, channel)) \
137  &COLOR_CHANNEL_MASK(format, channel) \
138  )
139 
140 /*
141  * @brief Sets a color
142  *
143  * param alpha: The alpha channel value
144  * param red: The red channel value
145  * param green: The green channel value
146  * param blue: The blue channel value
147  * param format: The color format (i.e. ARGB8888, etc...)
148  */
149 #define COLOR_SET_COLOR(alpha, red, green, blue, format) \
150  (uint32_t)(0 \
151  | (((alpha)&COLOR_ ## format ## _ALPHA_MASK) \
152  << (COLOR_ ## format ## _ALPHA_OFFSET)) \
153  | (((red)&COLOR_ ## format ## _RED_MASK) \
154  << (COLOR_ ## format ## _RED_OFFSET)) \
155  | (((green)&COLOR_ ## format ## _GREEN_MASK) \
156  << (COLOR_ ## format ## _GREEN_OFFSET)) \
157  | (((blue)&COLOR_ ## format ## _BLUE_MASK) \
158  << (COLOR_ ## format ## _BLUE_OFFSET)) \
159  )
160 
161 // -----------------------------------------------------------------------------
162 // EOF
163 // -----------------------------------------------------------------------------
164 
165 #ifdef __cplusplus
166 }
167 #endif
168 
169 #endif // !defined UI_COLOR_H