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