• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (std::clamp<uint32_t>(alpha_, 0, UINT8_MAX)) | (std::clamp<uint32_t>(red_, 0, UINT8_MAX) << 24) |
82            (std::clamp<uint32_t>(green_, 0, UINT8_MAX) << 16) | (std::clamp<uint32_t>(blue_, 0, UINT8_MAX) << 8);
83 }
84 
FromRgbaInt(uint32_t rgba)85 RSColor RSColor::FromRgbaInt(uint32_t rgba)
86 {
87     return RSColor(rgba);
88 }
89 
AsArgbInt() const90 uint32_t RSColor::AsArgbInt() const
91 {
92     return ((std::clamp<uint32_t>(alpha_, 0, UINT8_MAX)) << 24) | ((std::clamp<uint32_t>(red_, 0, UINT8_MAX)) << 16) |
93            ((std::clamp<uint32_t>(green_, 0, UINT8_MAX)) << 8) | (std::clamp<uint32_t>(blue_, 0, UINT8_MAX));
94 }
95 
FromArgbInt(uint32_t argb)96 RSColor RSColor::FromArgbInt(uint32_t argb)
97 {
98     uint32_t alpha = (argb >> 24) & 0xFF;               // 24 is alpha shift in ARGB, 0xFF is a byte mask
99     return RSColor(((argb << 8) & 0xFFFFFF00) | alpha); // 8 is rgb shift, 0xFFFFFF00 is a rgb mask
100 }
101 
AsBgraInt() const102 uint32_t RSColor::AsBgraInt() const
103 {
104     return (std::clamp<uint32_t>(alpha_, 0, UINT8_MAX)) | ((std::clamp<uint32_t>(red_, 0, UINT8_MAX)) << 8) |
105            ((std::clamp<uint32_t>(green_, 0, UINT8_MAX)) << 16) | ((std::clamp<uint32_t>(blue_, 0, UINT8_MAX)) << 24);
106 }
107 
FromBgraInt(uint32_t bgra)108 RSColor RSColor::FromBgraInt(uint32_t bgra)
109 {
110     union {
111         struct {
112             uint8_t blu_ : 8;
113             uint8_t gre_ : 8;
114             uint8_t red_ : 8;
115             uint8_t alp_ : 8;
116         };
117         uint32_t bgra_;
118     } color;
119     color.bgra_ = bgra;
120     return RSColor(color.red_, color.gre_, color.blu_, color.alp_);
121 }
122 
GetBlue() const123 int16_t RSColor::GetBlue() const
124 {
125     return blue_;
126 }
127 
GetGreen() const128 int16_t RSColor::GetGreen() const
129 {
130     return green_;
131 }
132 
GetRed() const133 int16_t RSColor::GetRed() const
134 {
135     return red_;
136 }
137 
GetAlpha() const138 int16_t RSColor::GetAlpha() const
139 {
140     return alpha_;
141 }
142 
SetBlue(int16_t blue)143 void RSColor::SetBlue(int16_t blue)
144 {
145     blue_ = blue;
146 }
147 
SetGreen(int16_t green)148 void RSColor::SetGreen(int16_t green)
149 {
150     green_ = green;
151 }
152 
SetRed(int16_t red)153 void RSColor::SetRed(int16_t red)
154 {
155     red_ = red;
156 }
157 
SetAlpha(int16_t alpha)158 void RSColor::SetAlpha(int16_t alpha)
159 {
160     alpha_ = alpha;
161 }
162 
MultiplyAlpha(float alpha)163 void RSColor::MultiplyAlpha(float alpha)
164 {
165     alpha_ = static_cast<int16_t>(alpha_ * std::clamp(alpha, 0.0f, 1.0f));
166 }
167 } // namespace Rosen
168 } // namespace OHOS
169