• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ROSEN_TEST_TESTING_COLOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ROSEN_TEST_TESTING_COLOR_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 namespace OHOS::Ace::Testing {
23 namespace {
24 constexpr int32_t ZERO = 0;
25 constexpr int32_t EIGHT = 8;
26 constexpr int32_t SIXTEEN = 16;
27 constexpr int32_t TWENTY_FOUR = 24;
28 constexpr int32_t BIT_LENGTH = 8;
29 constexpr int32_t CALC_BIT_LENGTH = 4;
30 constexpr uint32_t COLOR_DEFAULT = 0xff;
31 constexpr char HEX[] = "0123456789ABCDEF";
32 } // namespace
33 class TestingColor {
34 public:
35     constexpr static uint32_t COLOR_TRANSPARENT = 0;
36     constexpr static uint32_t COLOR_BLACK = 0xFF000000;
37     constexpr static uint32_t COLOR_DKGRAY = 0xFF444444;
38     constexpr static uint32_t COLOR_GRAY = 0xFF888888;
39     constexpr static uint32_t COLOR_LTGRAY = 0xFFCCCCCC;
40     constexpr static uint32_t COLOR_WHITE = 0xFFFFFFFF;
41     constexpr static uint32_t COLOR_RED = 0xFFFF0000;
42     constexpr static uint32_t COLOR_GREEN = 0xFF00FF00;
43     constexpr static uint32_t COLOR_BLUE = 0xFF0000FF;
44     constexpr static uint32_t COLOR_YELLOW = 0xFFFFFF00;
45     constexpr static uint32_t COLOR_CYAN = 0xFF00FFFF;
46     constexpr static uint32_t COLOR_MAGENTA = 0xFFFF00FF;
47     TestingColor() = default;
TestingColor(uint32_t red,uint32_t green,uint32_t blue,uint32_t alpha)48     TestingColor(uint32_t red, uint32_t green, uint32_t blue, uint32_t alpha)
49         : red_(red), green_(green), blue_(blue), alpha_(alpha)
50     {}
TestingColor(uint32_t rgba)51     TestingColor(uint32_t rgba)
52         : red_((rgba >> SIXTEEN) & COLOR_DEFAULT), green_((rgba >> EIGHT) & COLOR_DEFAULT),
53           blue_((rgba >> ZERO) & COLOR_DEFAULT), alpha_(rgba >> TWENTY_FOUR)
54     {}
55     virtual ~TestingColor() = default;
56 
57     bool operator==(const TestingColor& rhs) const
58     {
59         return red_ == rhs.red_ && green_ == rhs.green_ && blue_ == rhs.blue_ && alpha_ == rhs.alpha_;
60     }
61 
GetAlphaF()62     virtual float GetAlphaF()
63     {
64         return 1.0f;
65     }
66 
GetRed()67     uint32_t GetRed() const
68     {
69         return red_;
70     }
71 
GetGreen()72     uint32_t GetGreen() const
73     {
74         return green_;
75     }
76 
GetBlue()77     uint32_t GetBlue() const
78     {
79         return blue_;
80     }
81 
SetAlphaF(float alpha)82     void SetAlphaF(float alpha) {}
83 
Color(uint32_t rgba)84     virtual void Color(uint32_t rgba)
85     {
86         alpha_ = rgba >> TWENTY_FOUR;
87         red_ = (rgba >> SIXTEEN) & 0xff;
88         green_ = (rgba >> EIGHT) & 0xff;
89         blue_ = (rgba >> ZERO) & 0xff;
90     }
91 
GetValue()92     uint32_t GetValue() const
93     {
94         return (static_cast<uint32_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX))) |
95                (static_cast<uint32_t>((std::clamp<int16_t>(green_, 0, UINT8_MAX)) << 8)) |
96                (static_cast<uint32_t>((std::clamp<int16_t>(red_, 0, UINT8_MAX)) << 16)) |
97                (static_cast<uint32_t>((std::clamp<int16_t>(alpha_, 0, UINT8_MAX)) << 24));
98     }
99 
ColorToString()100     std::string ColorToString() const
101     {
102         std::string colorStr;
103         int count = 0;
104         uint32_t value = GetValue();
105         while (count++ < BIT_LENGTH) {
106             colorStr = HEX[(value & 0xf)] + colorStr;
107             value >>= CALC_BIT_LENGTH;
108         }
109         colorStr = "#" + colorStr;
110         return colorStr;
111     }
112 
113     uint32_t red_;
114     uint32_t green_;
115     uint32_t blue_;
116     uint32_t alpha_;
117 };
118 } // namespace OHOS::Ace::Testing
119 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ROSEN_TEST_TESTING_COLOR_H
120