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 #include "common/rs_color.h"
17
18 #include <algorithm>
19 #include <iomanip>
20 #include <sstream>
21
22 #include "common/rs_common_def.h"
23
24 namespace OHOS {
25 namespace Rosen {
RSColor(uint32_t rgba)26 RSColor::RSColor(uint32_t rgba) noexcept
27 {
28 alpha_ = static_cast<int16_t>(rgba & 0xFF); // 0xff byte mask
29 red_ = static_cast<int16_t>((rgba & 0xFF000000) >> 24); // 0xff000000 red mask, 24 red shift
30 green_ = static_cast<int16_t>((rgba & 0x00FF0000) >> 16); // 0x00ff0000 green mask, 16 green shift
31 blue_ = static_cast<int16_t>((rgba & 0x0000FF00) >> 8); // 0x0000ff00 blue mask, 8 blue shift
32 }
33
RSColor(int16_t red,int16_t green,int16_t blue)34 RSColor::RSColor(int16_t red, int16_t green, int16_t blue) noexcept : RSColor(red, green, blue, UINT8_MAX) {}
35
RSColor(int16_t red,int16_t green,int16_t blue,int16_t alpha)36 RSColor::RSColor(int16_t red, int16_t green, int16_t blue, int16_t alpha) noexcept
37 {
38 alpha_ = alpha;
39 red_ = red;
40 green_ = green;
41 blue_ = blue;
42 }
43
operator ==(const RSColor & rhs) const44 bool RSColor::operator==(const RSColor& rhs) const
45 {
46 return red_ == rhs.red_ && green_ == rhs.green_ && blue_ == rhs.blue_ && alpha_ == rhs.alpha_;
47 }
48
IsNearEqual(const RSColor & other,int16_t threshold) const49 bool RSColor::IsNearEqual(const RSColor& other, int16_t threshold) const
50 {
51 return (std::abs(red_ - other.red_) <= threshold) && (std::abs(green_ - other.green_) <= threshold) &&
52 (std::abs(blue_ - other.blue_) <= threshold) && (std::abs(alpha_ - other.alpha_) <= threshold);
53 }
54
operator +(const RSColor & rhs) const55 RSColor RSColor::operator+(const RSColor& rhs) const
56 {
57 return RSColor(red_ + rhs.red_, green_ + rhs.green_, blue_ + rhs.blue_, alpha_ + rhs.alpha_);
58 }
59
operator -(const RSColor & rhs) const60 RSColor RSColor::operator-(const RSColor& rhs) const
61 {
62 return RSColor(red_ - rhs.red_, green_ - rhs.green_, blue_ - rhs.blue_, alpha_ - rhs.alpha_);
63 }
64
operator *(float scale) const65 RSColor RSColor::operator*(float scale) const
66 {
67 return RSColor(round(red_ * scale), round(green_ * scale), round(blue_ * scale), round(alpha_ * scale));
68 }
69
operator *=(float scale)70 RSColor& RSColor::operator*=(float scale)
71 {
72 red_ = round(red_ * scale);
73 green_ = round(green_ * scale);
74 blue_ = round(blue_ * scale);
75 alpha_ = round(alpha_ * scale);
76 return *this;
77 }
78
operator /(float scale) const79 RSColor RSColor::operator/(float scale) const
80 {
81 if (ROSEN_EQ<float>(scale, 0)) {
82 return *this;
83 }
84 return operator*(1 / scale);
85 }
86
AsRgbaInt() const87 uint32_t RSColor::AsRgbaInt() const
88 {
89 return (static_cast<uint32_t>(std::clamp<int16_t>(alpha_, 0, UINT8_MAX))) |
90 ((static_cast<uint32_t>(std::clamp<int16_t>(red_, 0, UINT8_MAX))) << 24) | // 24 red shift
91 ((static_cast<uint32_t>(std::clamp<int16_t>(green_, 0, UINT8_MAX))) << 16) | // 16 green shift
92 ((static_cast<uint32_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX))) << 8); // 8 blue shift
93 }
94
FromRgbaInt(uint32_t rgba)95 RSColor RSColor::FromRgbaInt(uint32_t rgba)
96 {
97 return RSColor(rgba);
98 }
99
AsArgbInt() const100 uint32_t RSColor::AsArgbInt() const
101 {
102 return ((static_cast<uint32_t>(std::clamp<int16_t>(alpha_, 0, UINT8_MAX))) << 24) | // 24 alpha shift
103 ((static_cast<uint32_t>(std::clamp<int16_t>(red_, 0, UINT8_MAX))) << 16) | // 16 red shift
104 ((static_cast<uint32_t>(std::clamp<int16_t>(green_, 0, UINT8_MAX))) << 8) | // 8 green shift
105 (static_cast<uint32_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX)));
106 }
107
FromArgbInt(uint32_t argb)108 RSColor RSColor::FromArgbInt(uint32_t argb)
109 {
110 uint32_t alpha = (argb >> 24) & 0xFF; // 24 is alpha shift in ARGB, 0xFF is a byte mask
111 return RSColor(((argb << 8) & 0xFFFFFF00) | alpha); // 8 is rgb shift, 0xFFFFFF00 is a rgb mask
112 }
113
AsBgraInt() const114 uint32_t RSColor::AsBgraInt() const
115 {
116 return (static_cast<uint32_t>(std::clamp<int16_t>(alpha_, 0, UINT8_MAX))) |
117 ((static_cast<uint32_t>(std::clamp<int16_t>(red_, 0, UINT8_MAX))) << 8) | // 8 red shift
118 ((static_cast<uint32_t>(std::clamp<int16_t>(green_, 0, UINT8_MAX))) << 16) | // 16 green shift
119 ((static_cast<uint32_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX))) << 24); // 24 blue shift
120 }
121
FromBgraInt(uint32_t bgra)122 RSColor RSColor::FromBgraInt(uint32_t bgra)
123 {
124 union {
125 struct {
126 uint8_t blu_ : 8;
127 uint8_t gre_ : 8;
128 uint8_t red_ : 8;
129 uint8_t alp_ : 8;
130 };
131 uint32_t bgra_;
132 } color;
133 color.bgra_ = bgra;
134 return RSColor(color.red_, color.gre_, color.blu_, color.alp_);
135 }
136
GetBlue() const137 int16_t RSColor::GetBlue() const
138 {
139 return blue_;
140 }
141
GetGreen() const142 int16_t RSColor::GetGreen() const
143 {
144 return green_;
145 }
146
GetRed() const147 int16_t RSColor::GetRed() const
148 {
149 return red_;
150 }
151
GetAlpha() const152 int16_t RSColor::GetAlpha() const
153 {
154 return alpha_;
155 }
156
SetBlue(int16_t blue)157 void RSColor::SetBlue(int16_t blue)
158 {
159 blue_ = blue;
160 }
161
SetGreen(int16_t green)162 void RSColor::SetGreen(int16_t green)
163 {
164 green_ = green;
165 }
166
SetRed(int16_t red)167 void RSColor::SetRed(int16_t red)
168 {
169 red_ = red;
170 }
171
SetAlpha(int16_t alpha)172 void RSColor::SetAlpha(int16_t alpha)
173 {
174 alpha_ = alpha;
175 }
176
MultiplyAlpha(float alpha)177 void RSColor::MultiplyAlpha(float alpha)
178 {
179 alpha_ = static_cast<int16_t>(alpha_ * std::clamp(alpha, 0.0f, 1.0f));
180 }
181
Dump(std::string & out) const182 void RSColor::Dump(std::string& out) const
183 {
184 constexpr int32_t colorStrWidth = 8;
185 std::stringstream ss;
186 ss << "[RGBA-0x" << std::hex << std::setfill('0') << std::setw(colorStrWidth) << std::uppercase;
187 ss << AsRgbaInt() << ']';
188 out += ss.str();
189 }
190 } // namespace Rosen
191 } // namespace OHOS
192