• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #ifndef COLOR_H
17 #define COLOR_H
18 
19 #include <cstdint>
20 #include <stdint.h>
21 
22 #include "effect/color_space.h"
23 #include "utils/scalar.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 enum ColorType {
29     COLORTYPE_UNKNOWN = 0,
30     COLORTYPE_ALPHA_8,
31     COLORTYPE_RGB_565,
32     COLORTYPE_ARGB_4444,
33     COLORTYPE_RGBA_8888,
34     COLORTYPE_BGRA_8888,
35     COLORTYPE_N32,
36 };
37 
38 enum AlphaType {
39     ALPHATYPE_UNKNOWN = 0,
40     ALPHATYPE_OPAQUE,
41     ALPHATYPE_PREMUL,
42     ALPHATYPE_UNPREMUL,
43 };
44 
45 // Default RGBA color value is black.
46 struct Color4f {
47     scalar redF_ = 0;
48     scalar greenF_ = 0;
49     scalar blueF_ = 0;
50     scalar alphaF_ = 1;
51 };
52 
53 typedef uint32_t ColorQuad;
54 class Color {
55 public:
56     constexpr static ColorQuad COLOR_TRANSPARENT = 0;
57     constexpr static ColorQuad COLOR_BLACK = 0xFF000000;
58     constexpr static ColorQuad COLOR_DKGRAY = 0xFF444444;
59     constexpr static ColorQuad COLOR_GRAY = 0xFF888888;
60     constexpr static ColorQuad COLOR_LTGRAY = 0xFFCCCCCC;
61     constexpr static ColorQuad COLOR_WHITE = 0xFFFFFFFF;
62     constexpr static ColorQuad COLOR_RED = 0xFFFF0000;
63     constexpr static ColorQuad COLOR_GREEN = 0xFF00FF00;
64     constexpr static ColorQuad COLOR_BLUE = 0xFF0000FF;
65     constexpr static ColorQuad COLOR_YELLOW = 0xFFFFFF00;
66     constexpr static ColorQuad COLOR_CYAN = 0xFF00FFFF;
67     constexpr static ColorQuad COLOR_MAGENTA = 0xFFFF00FF;
68 
69     constexpr static uint8_t RGB_MAX = 255;
70     // Return color value from component values.
ColorQuadSetARGB(uint32_t r,uint32_t g,uint32_t b,uint32_t a)71     static inline ColorQuad ColorQuadSetARGB(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
72     {
73         return ((a & 0xffu) << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | ((b & 0xffu) << 0);
74     }
75 
76     // Return alpha byte from color value.
ColorQuadGetA(ColorQuad c)77     static inline uint32_t ColorQuadGetA(ColorQuad c)
78     {
79         return ((c >> 24) & 0xff);
80     }
81 
82     // Return red component of color, from zero to 255.
ColorQuadGetR(ColorQuad c)83     static inline uint32_t ColorQuadGetR(ColorQuad c)
84     {
85         return ((c >> 16) & 0xff);
86     }
87 
88     // Return green component of color, from zero to 255.
ColorQuadGetG(ColorQuad c)89     static inline uint32_t ColorQuadGetG(ColorQuad c)
90     {
91         return ((c >> 8) & 0xff);
92     }
93 
94     // Return blue component of color, from zero to 255.
ColorQuadGetB(ColorQuad c)95     static inline uint32_t ColorQuadGetB(ColorQuad c)
96     {
97         return ((c >> 0) & 0xff);
98     }
99 
~Color()100     ~Color() {}
101 
102 public:
103     Color() noexcept;
104     Color(const Color& c) noexcept;
105     Color(uint32_t r, uint32_t g, uint32_t b, uint32_t a) noexcept;
106     Color(ColorQuad rgba) noexcept;
107 
108     uint32_t GetRed() const;
109     uint32_t GetGreen() const;
110     uint32_t GetBlue() const;
111     uint32_t GetAlpha() const;
112     void SetRed(uint32_t r);
113     void SetGreen(uint32_t g);
114     void SetBlue(uint32_t b);
115     void SetAlpha(uint32_t a);
116 
117     scalar GetRedF() const;
118     scalar GetGreenF() const;
119     scalar GetBlueF() const;
120     scalar GetAlphaF() const;
121     Color4f GetColor4f();
122     void SetRedF(scalar r);
123     void SetGreenF(scalar g);
124     void SetBlueF(scalar b);
125     void SetAlphaF(scalar a);
126 
127     void SetRgb(uint32_t r, uint32_t g, uint32_t b, uint32_t a = 255);
128     void SetRgbF(scalar r, scalar g, scalar b, scalar a = 1.0);
129 
130     void SetColorQuad(uint32_t c);
131     ColorQuad CastToColorQuad() const;
132 
133     friend bool operator==(const Color& c1, const Color& c2);
134     friend bool operator!=(const Color& c1, const Color& c2);
135 
136 private:
137     uint32_t alpha_;
138     uint32_t red_;
139     uint32_t green_;
140     uint32_t blue_;
141     Color4f color4f_;
142 };
143 } // namespace Drawing
144 } // namespace Rosen
145 } // namespace OHOS
146 #endif
147