• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup UI_Utils
18  * @{
19  *
20  * @brief Defines basic UI utils.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file color.h
28  *
29  * @brief Defines color attributes for the graphics system and implements common color functions.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_COLOR_H
36 #define GRAPHIC_LITE_COLOR_H
37 
38 #include <cstdint>
39 
40 #include "graphic_config.h"
41 #include "gfx_utils/heap_base.h"
42 
43 namespace OHOS {
44 /**
45  * @brief Defines the color attribute when the color depth is <b>16</b>.
46  */
47 typedef union {
48     struct {
49         /** Blue */
50         uint16_t blue : 5;
51         /** Green */
52         uint16_t green : 6;
53         /** Red */
54         uint16_t red : 5;
55     };
56     /** Full RGB data */
57     uint16_t full;
58 } Color16;
59 
60 /**
61  * @brief Defines the color attribute when the color depth is <b>24</b>.
62  */
63 struct Color24 {
64     /** Blue */
65     uint8_t blue;
66     /** Green */
67     uint8_t green;
68     /** Red */
69     uint8_t red;
70 };
71 
72 /**
73  * @brief Defines the color attribute when the color depth is <b>32</b>.
74  */
75 typedef union {
76     struct {
77         /** Blue */
78         uint8_t blue;
79         /** Green */
80         uint8_t green;
81         /** Red */
82         uint8_t red;
83         /** Alpha (how opaque each pixel is) */
84         uint8_t alpha;
85     };
86     /** Full RGB data */
87     uint32_t full;
88 } Color32;
89 
90 #if COLOR_DEPTH == 16
91 typedef Color16 ColorType;
92 #elif COLOR_DEPTH == 32
93 typedef Color32 ColorType;
94 #else
95 #error "Invalid COLOR_DEPTH, Set it to 16 or 32!"
96 #endif
97 
98 using OpacityType = uint8_t;
99 
100 /**
101  * @brief Enumerates opacity values.
102  */
103 enum {
104     /** The opacity is 0. */
105     OPA_TRANSPARENT = 0,
106     /** The opacity is 100%. */
107     OPA_OPAQUE = 255,
108 };
109 
110 /**
111  * @brief Converts colors in different formats and defines common colors.
112  *
113  * @since 1.0
114  * @version 1.0
115  */
116 class Color : public HeapBase {
117 public:
118     /**
119      * @brief Mixes two colors (color 1 and color 2) based on a specified opacity.
120      *
121      * @param c1 Indicates color 1.
122      * @param c2 Indicates color 2.
123      * @param mix Indicates the alpha, that is, how opaque each pixel is.
124 
125      * @return Returns the color data after mixing.
126      * @since 1.0
127      * @version 1.0
128      */
129     static ColorType GetMixColor(ColorType c1, ColorType c2, uint8_t mix);
130 
131     /**
132      * @brief Obtains the color based on the RGB color value.
133      *
134      * @param r8 Indicates the intensity of red.
135      * @param g8 Indicates the intensity of green.
136      * @param b8 Indicates the intensity of blue.
137      *
138      * @return Returns the color data generated.
139      * @since 1.0
140      * @version 1.0
141      */
142     static ColorType GetColorFromRGB(uint8_t r8, uint8_t g8, uint8_t b8);
143 
144     /**
145      * @brief Obtains the color based on the RGBA color value.
146      *
147      * @param r8 Indicates the intensity of red.
148      * @param g8 Indicates the intensity of green.
149      * @param b8 Indicates the intensity of blue.
150      * @param alpha Indicates the alpha, that is, how opaque each pixel is.
151      *
152      * @return Returns the color data generated.
153      * @since 1.0
154      * @version 1.0
155      */
156     static ColorType GetColorFromRGBA(uint8_t r8, uint8_t g8, uint8_t b8, uint8_t alpha);
157 
158     /**
159      * @brief Converts color data into the RGBA8888 format.
160      *
161      * The color data definition varies according to the color depth.
162      *
163      * @param color Indicates the color data, which is defined by {@link ColorType}.
164      * @return Returns the RGBA8888 color data.
165      * @since 1.0
166      * @version 1.0
167      */
168     static uint32_t ColorTo32(ColorType color);
169 
170     /**
171      * @brief Converts color data with the 16-bit color depth into the RGBA8888 format.
172      *
173      * @param color Indicates the color data with the 16-bit color depth, which is defined by {@link Color16}.
174      * @param alpha Indicates the alpha, that is, how opaque each pixel is.
175      * @return Returns the RGBA8888 color data.
176      * @since 1.0
177      * @version 1.0
178      */
179     static uint32_t ColorTo32(Color16 color, uint8_t alpha);
180 
181     /**
182      * @brief Converts color data from the RGBA8888 format into the RGB565 format.
183      *
184      * @param color Indicates the color data with the 32-bit color depth, which is defined by {@link Color32}.
185      * @return Returns the RGB565 color data.
186      * @since 1.0
187      * @version 1.0
188      */
189     static uint16_t ColorTo16(Color32 color);
190 
191     /**
192      * @brief Obtains the color data of white.
193      *
194      * @return Returns the color data.
195      * @since 1.0
196      * @version 1.0
197      */
198     static ColorType White();
199 
200     /**
201      * @brief Obtains the color data of silver.
202      *
203      * @return Returns the color data.
204      * @since 1.0
205      * @version 1.0
206      */
207     static ColorType Silver();
208 
209     /**
210      * @brief Obtains the color data of gray.
211      *
212      * @return Returns the color data.
213      * @since 1.0
214      * @version 1.0
215      */
216     static ColorType Gray();
217 
218     /**
219      * @brief Obtains the color data of black.
220      *
221      * @return Returns the color data.
222      * @since 1.0
223      * @version 1.0
224      */
225     static ColorType Black();
226 
227     /**
228      * @brief Obtains the color data of red.
229      *
230      * @return Returns the color data.
231      * @since 1.0
232      * @version 1.0
233      */
234     static ColorType Red();
235 
236     /**
237      * @brief Obtains the color data of maroon.
238      *
239      * @return Returns the color data.
240      * @since 1.0
241      * @version 1.0
242      */
243     static ColorType Maroon();
244 
245     /**
246      * @brief Obtains the color data of yellow.
247      *
248      * @return Returns the color data.
249      * @since 1.0
250      * @version 1.0
251      */
252     static ColorType Yellow();
253 
254     /**
255      * @brief Obtains the color data of olive.
256      *
257      * @return Returns the color data.
258      * @since 1.0
259      * @version 1.0
260      */
261     static ColorType Olive();
262 
263     /**
264      * @brief Obtains the color data of lime.
265      *
266      * @return Returns the color data.
267      * @since 1.0
268      * @version 1.0
269      */
270     static ColorType Lime();
271 
272     /**
273      * @brief Obtains the color data of green.
274      *
275      * @return Returns the color data.
276      * @since 1.0
277      * @version 1.0
278      */
279     static ColorType Green();
280 
281     /**
282      * @brief Obtains the color data of cyan.
283      *
284      * @return Returns the color data.
285      * @since 1.0
286      * @version 1.0
287      */
288     static ColorType Cyan();
289 
290     /**
291      * @brief Obtains the color data of aqua.
292      *
293      * @return Returns the color data.
294      * @since 1.0
295      * @version 1.0
296      */
297     static ColorType Aqua();
298 
299     /**
300      * @brief Obtains the color data of teal.
301      *
302      * @return Returns the color data.
303      * @since 1.0
304      * @version 1.0
305      */
306     static ColorType Teal();
307 
308     /**
309      * @brief Obtains the color data of blue.
310      *
311      * @return Returns the color data.
312      * @since 1.0
313      * @version 1.0
314      */
315     static ColorType Blue();
316 
317     /**
318      * @brief Obtains the color data of navy.
319      *
320      * @return Returns the color data.
321      * @since 1.0
322      * @version 1.0
323      */
324     static ColorType Navy();
325 
326     /**
327      * @brief Obtains the color data of magenta.
328      *
329      * @return Returns the color data.
330      * @since 1.0
331      * @version 1.0
332      */
333     static ColorType Magenta();
334 
335     /**
336      * @brief Obtains the color data of purple.
337      *
338      * @return Returns the color data.
339      * @since 1.0
340      * @version 1.0
341      */
342     static ColorType Purple();
343 
344     /**
345      * @brief Obtains the color data of orange.
346      *
347      * @return Returns the color data.
348      * @since 1.0
349      * @version 1.0
350      */
351     static ColorType Orange();
352 };
353 } // namespace OHOS
354 #endif // GRAPHIC_LITE_COLOR_H
355