• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_INTERFACES_INNER_API_ACE_KIT_INCLUDE_BASE_PROPERTIES_LINEAR_COLOR_H
17 #define FOUNDATION_ACE_INTERFACES_INNER_API_ACE_KIT_INCLUDE_BASE_PROPERTIES_LINEAR_COLOR_H
18 
19 #include "ui/properties/color.h"
20 
21 namespace OHOS::Ace {
22 
23 class ACE_FORCE_EXPORT LinearColor {
24 public:
25     LinearColor() = default;
LinearColor(uint32_t argb)26     explicit LinearColor(uint32_t argb)
27     {
28         alpha_ = static_cast<int16_t>((argb & 0xFF000000) >> 24);
29         red_ = static_cast<int16_t>((argb & 0x00FF0000) >> 16);
30         green_ = static_cast<int16_t>((argb & 0x0000FF00) >> 8);
31         blue_ = static_cast<int16_t>(argb & 0xFF);
32     }
LinearColor(const Color & color)33     explicit LinearColor(const Color& color)
34         : alpha_(color.GetAlpha()), red_(color.GetRed()), green_(color.GetGreen()), blue_(color.GetBlue())
35     {}
LinearColor(int16_t alpha,int16_t red,int16_t green,int16_t blue)36     LinearColor(int16_t alpha, int16_t red, int16_t green, int16_t blue)
37         : alpha_(alpha), red_(red), green_(green), blue_(blue)
38     {}
39     ~LinearColor() = default;
40 
41     ACE_FORCE_EXPORT static const LinearColor TRANSPARENT;
42     static const LinearColor WHITE;
43     static const LinearColor BLACK;
44     static const LinearColor RED;
45     static const LinearColor GREEN;
46     static const LinearColor BLUE;
47     static const LinearColor GRAY;
48 
49     LinearColor operator+(const LinearColor& color) const
50     {
51         return LinearColor(GetAlpha() + color.GetAlpha(), GetRed() + color.GetRed(), GetGreen() + color.GetGreen(),
52             GetBlue() + color.GetBlue());
53     }
54 
55     LinearColor operator-(const LinearColor& color) const
56     {
57         return LinearColor(GetAlpha() - color.GetAlpha(), GetRed() - color.GetRed(), GetGreen() - color.GetGreen(),
58             GetBlue() - color.GetBlue());
59     }
60 
61     LinearColor operator*(double value) const
62     {
63         return LinearColor(static_cast<int16_t>(round(GetAlpha() * value)),
64             static_cast<int16_t>(round(GetRed() * value)), static_cast<int16_t>(round(GetGreen() * value)),
65             static_cast<int16_t>(round(GetBlue() * value)));
66     }
67 
68     bool operator==(const LinearColor& color) const
69     {
70         return alpha_ == color.GetAlpha() && red_ == color.GetRed() && green_ == color.GetGreen() &&
71                blue_ == color.GetBlue();
72     }
73 
74     LinearColor& operator+=(const LinearColor& color) {
75         alpha_ = alpha_ + color.GetAlpha();
76         red_ = red_ + color.GetRed();
77         green_ = green_ + color.GetGreen();
78         blue_ = blue_ + color.GetBlue();
79         return *this;
80     }
81 
82     LinearColor& operator*=(float scale) {
83         alpha_ = alpha_ * scale;
84         red_ = red_ * scale;
85         green_ = green_ * scale;
86         blue_ = blue_ * scale;
87         return *this;
88     }
89 
90     LinearColor& operator-=(const LinearColor& color) {
91         alpha_ = alpha_ - color.GetAlpha();
92         red_ = red_ - color.GetRed();
93         green_ = green_ - color.GetGreen();
94         blue_ = blue_ - color.GetBlue();
95         return *this;
96     }
97 
GetRed()98     int16_t GetRed() const
99     {
100         return red_;
101     }
102 
GetGreen()103     int16_t GetGreen() const
104     {
105         return green_;
106     }
107 
GetBlue()108     int16_t GetBlue() const
109     {
110         return blue_;
111     }
112 
GetAlpha()113     int16_t GetAlpha() const
114     {
115         return alpha_;
116     }
117 
GetValue()118     uint32_t GetValue() const
119     {
120         return (static_cast<uint32_t>(std::clamp<uint16_t>(blue_, 0, UINT8_MAX))) |
121                (static_cast<uint32_t>((std::clamp<uint16_t>(green_, 0, UINT8_MAX)) << 8)) |
122                (static_cast<uint32_t>((std::clamp<uint16_t>(red_, 0, UINT8_MAX)) << 16)) |
123                (static_cast<uint32_t>((std::clamp<uint16_t>(alpha_, 0, UINT8_MAX)) << 24));
124     }
125 
BlendOpacity(double opacityRatio)126     Color BlendOpacity(double opacityRatio) const
127     {
128         auto alpha = static_cast<int16_t>(GetAlpha() * opacityRatio);
129         return Color::FromARGB(static_cast<uint8_t>(std::clamp<int16_t>(alpha, 0, UINT8_MAX)),
130             static_cast<uint8_t>(std::clamp<int16_t>(red_, 0, UINT8_MAX)),
131             static_cast<uint8_t>(std::clamp<int16_t>(green_, 0, UINT8_MAX)),
132             static_cast<uint8_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX)));
133     }
134 
ToColor()135     Color ToColor() const
136     {
137         return Color::FromARGB(static_cast<uint8_t>(std::clamp<int16_t>(alpha_, 0, UINT8_MAX)),
138             static_cast<uint8_t>(std::clamp<int16_t>(red_, 0, UINT8_MAX)),
139             static_cast<uint8_t>(std::clamp<int16_t>(green_, 0, UINT8_MAX)),
140             static_cast<uint8_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX)));
141     }
142 
143 private:
144     int16_t alpha_;
145     int16_t red_;
146     int16_t green_;
147     int16_t blue_;
148 };
149 
150 } // namespace OHOS::Ace
151 
152 #endif // FOUNDATION_ACE_INTERFACES_INNER_API_ACE_KIT_INCLUDE_BASE_PROPERTIES_LINEAR_COLOR_H
153