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