1 /*
2 * Copyright (c) 2021-2022 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
20 #include "common/rs_common_def.h"
21
22 namespace OHOS {
23 namespace Rosen {
RSColor(uint32_t rgba)24 RSColor::RSColor(uint32_t rgba) noexcept
25 {
26 alpha_ = static_cast<int16_t>(rgba & 0xFF); // 0xff byte mask
27 red_ = static_cast<int16_t>((rgba & 0xFF000000) >> 24); // 0xff000000 red mask, 24 red shift
28 green_ = static_cast<int16_t>((rgba & 0x00FF0000) >> 16); // 0x00ff0000 green mask, 16 green shift
29 blue_ = static_cast<int16_t>((rgba & 0x0000FF00) >> 8); // 0x0000ff00 blue mask, 8 blue shift
30 }
31
RSColor(int16_t red,int16_t green,int16_t blue)32 RSColor::RSColor(int16_t red, int16_t green, int16_t blue) noexcept : RSColor(red, green, blue, UINT8_MAX) {}
33
RSColor(int16_t red,int16_t green,int16_t blue,int16_t alpha)34 RSColor::RSColor(int16_t red, int16_t green, int16_t blue, int16_t alpha) noexcept
35 {
36 alpha_ = alpha;
37 red_ = red;
38 green_ = green;
39 blue_ = blue;
40 }
41
operator ==(const RSColor & rhs) const42 bool RSColor::operator==(const RSColor& rhs) const
43 {
44 return red_ == rhs.red_ && green_ == rhs.green_ && blue_ == rhs.blue_ && alpha_ == rhs.alpha_;
45 }
46
operator +(const RSColor & rhs) const47 RSColor RSColor::operator+(const RSColor& rhs) const
48 {
49 return RSColor(red_ + rhs.red_, green_ + rhs.green_, blue_ + rhs.blue_, alpha_ + rhs.alpha_);
50 }
51
operator -(const RSColor & rhs) const52 RSColor RSColor::operator-(const RSColor& rhs) const
53 {
54 return RSColor(red_ - rhs.red_, green_ - rhs.green_, blue_ - rhs.blue_, alpha_ - rhs.alpha_);
55 }
56
operator *(float scale) const57 RSColor RSColor::operator*(float scale) const
58 {
59 return RSColor(round(red_ * scale), round(green_ * scale), round(blue_ * scale), round(alpha_ * scale));
60 }
61
operator *=(float scale)62 RSColor& RSColor::operator*=(float scale)
63 {
64 red_ = round(red_ * scale);
65 green_ = round(green_ * scale);
66 blue_ = round(blue_ * scale);
67 alpha_ = round(alpha_ * scale);
68 return *this;
69 }
70
operator /(float scale) const71 RSColor RSColor::operator/(float scale) const
72 {
73 if (ROSEN_EQ<float>(scale, 0)) {
74 return *this;
75 }
76 return operator*(1 / scale);
77 }
78
AsRgbaInt() const79 uint32_t RSColor::AsRgbaInt() const
80 {
81 return (static_cast<uint32_t>(std::clamp<int16_t>(alpha_, 0, UINT8_MAX))) |
82 ((static_cast<uint32_t>(std::clamp<int16_t>(red_, 0, UINT8_MAX))) << 24) | // 24 red shift
83 ((static_cast<uint32_t>(std::clamp<int16_t>(green_, 0, UINT8_MAX))) << 16) | // 16 green shift
84 ((static_cast<uint32_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX))) << 8); // 8 blue shift
85 }
86
FromRgbaInt(uint32_t rgba)87 RSColor RSColor::FromRgbaInt(uint32_t rgba)
88 {
89 return RSColor(rgba);
90 }
91
AsArgbInt() const92 uint32_t RSColor::AsArgbInt() const
93 {
94 return ((static_cast<uint32_t>(std::clamp<int16_t>(alpha_, 0, UINT8_MAX))) << 24) | // 24 alpha shift
95 ((static_cast<uint32_t>(std::clamp<int16_t>(red_, 0, UINT8_MAX))) << 16) | // 16 red shift
96 ((static_cast<uint32_t>(std::clamp<int16_t>(green_, 0, UINT8_MAX))) << 8) | // 8 green shift
97 (static_cast<uint32_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX)));
98 }
99
FromArgbInt(uint32_t argb)100 RSColor RSColor::FromArgbInt(uint32_t argb)
101 {
102 uint32_t alpha = (argb >> 24) & 0xFF; // 24 is alpha shift in ARGB, 0xFF is a byte mask
103 return RSColor(((argb << 8) & 0xFFFFFF00) | alpha); // 8 is rgb shift, 0xFFFFFF00 is a rgb mask
104 }
105
AsBgraInt() const106 uint32_t RSColor::AsBgraInt() const
107 {
108 return (static_cast<uint32_t>(std::clamp<int16_t>(alpha_, 0, UINT8_MAX))) |
109 ((static_cast<uint32_t>(std::clamp<int16_t>(red_, 0, UINT8_MAX))) << 8) | // 8 red shift
110 ((static_cast<uint32_t>(std::clamp<int16_t>(green_, 0, UINT8_MAX))) << 16) | // 16 green shift
111 ((static_cast<uint32_t>(std::clamp<int16_t>(blue_, 0, UINT8_MAX))) << 24); // 24 blue shift
112 }
113
FromBgraInt(uint32_t bgra)114 RSColor RSColor::FromBgraInt(uint32_t bgra)
115 {
116 union {
117 struct {
118 uint8_t blu_ : 8;
119 uint8_t gre_ : 8;
120 uint8_t red_ : 8;
121 uint8_t alp_ : 8;
122 };
123 uint32_t bgra_;
124 } color;
125 color.bgra_ = bgra;
126 return RSColor(color.red_, color.gre_, color.blu_, color.alp_);
127 }
128
GetBlue() const129 int16_t RSColor::GetBlue() const
130 {
131 return blue_;
132 }
133
GetGreen() const134 int16_t RSColor::GetGreen() const
135 {
136 return green_;
137 }
138
GetRed() const139 int16_t RSColor::GetRed() const
140 {
141 return red_;
142 }
143
GetAlpha() const144 int16_t RSColor::GetAlpha() const
145 {
146 return alpha_;
147 }
148
SetBlue(int16_t blue)149 void RSColor::SetBlue(int16_t blue)
150 {
151 blue_ = blue;
152 }
153
SetGreen(int16_t green)154 void RSColor::SetGreen(int16_t green)
155 {
156 green_ = green;
157 }
158
SetRed(int16_t red)159 void RSColor::SetRed(int16_t red)
160 {
161 red_ = red;
162 }
163
SetAlpha(int16_t alpha)164 void RSColor::SetAlpha(int16_t alpha)
165 {
166 alpha_ = alpha;
167 }
168
MultiplyAlpha(float alpha)169 void RSColor::MultiplyAlpha(float alpha)
170 {
171 alpha_ = static_cast<int16_t>(alpha_ * std::clamp(alpha, 0.0f, 1.0f));
172 }
173 } // namespace Rosen
174 } // namespace OHOS
175