1 /*
2 * Copyright (c) 2021 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 "draw/color.h"
17 #include <algorithm>
18 #include <iomanip>
19 #include <sstream>
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
Color()24 Color::Color() noexcept : alpha_(RGB_MAX), red_(0), green_(0), blue_(0) {}
25
Color(const Color & c)26 Color::Color(const Color& c) noexcept : alpha_(c.GetAlpha()), red_(c.GetRed()), green_(c.GetGreen()), blue_(c.GetBlue())
27 {}
28
Color(uint32_t r,uint32_t g,uint32_t b,uint32_t a)29 Color::Color(uint32_t r, uint32_t g, uint32_t b, uint32_t a) noexcept : alpha_(a), red_(r), green_(g), blue_(b) {}
30
31 // Return alpha byte, red component, green component and blue component of color rgba.
Color(ColorQuad rgba)32 Color::Color(ColorQuad rgba) noexcept
33 {
34 alpha_ = rgba >> 24;
35 red_ = (rgba >> 16) & 0xff;
36 green_ = (rgba >> 8) & 0xff;
37 blue_ = (rgba >> 0) & 0xff;
38 }
39
GetRed() const40 uint32_t Color::GetRed() const
41 {
42 return red_;
43 }
44
GetGreen() const45 uint32_t Color::GetGreen() const
46 {
47 return green_;
48 }
49
GetBlue() const50 uint32_t Color::GetBlue() const
51 {
52 return blue_;
53 }
54
GetAlpha() const55 uint32_t Color::GetAlpha() const
56 {
57 return alpha_;
58 }
59
SetRed(uint32_t r)60 void Color::SetRed(uint32_t r)
61 {
62 red_ = r;
63 }
64
SetGreen(uint32_t g)65 void Color::SetGreen(uint32_t g)
66 {
67 green_ = g;
68 }
69
SetBlue(uint32_t b)70 void Color::SetBlue(uint32_t b)
71 {
72 blue_ = b;
73 }
74
SetAlpha(uint32_t a)75 void Color::SetAlpha(uint32_t a)
76 {
77 alpha_ = a;
78 }
79
GetRedF() const80 scalar Color::GetRedF() const
81 {
82 return static_cast<scalar>(red_) / RGB_MAX;
83 }
84
GetGreenF() const85 scalar Color::GetGreenF() const
86 {
87 return static_cast<scalar>(green_) / RGB_MAX;
88 }
89
GetBlueF() const90 scalar Color::GetBlueF() const
91 {
92 return static_cast<scalar>(blue_) / RGB_MAX;
93 }
94
GetAlphaF() const95 scalar Color::GetAlphaF() const
96 {
97 return static_cast<scalar>(alpha_) / RGB_MAX;
98 }
99
GetColor4f()100 const Color4f& Color::GetColor4f()
101 {
102 color4f_.redF_ = GetRedF();
103 color4f_.greenF_ = GetGreenF();
104 color4f_.blueF_ = GetBlueF();
105 color4f_.alphaF_ = GetAlphaF();
106 return color4f_;
107 }
108
SetRedF(scalar r)109 void Color::SetRedF(scalar r)
110 {
111 red_ = static_cast<uint8_t>(std::clamp(r, 0.0f, 1.0f) * RGB_MAX);
112 }
113
SetGreenF(scalar g)114 void Color::SetGreenF(scalar g)
115 {
116 green_ = static_cast<uint8_t>(std::clamp(g, 0.0f, 1.0f) * RGB_MAX);
117 }
118
SetBlueF(scalar b)119 void Color::SetBlueF(scalar b)
120 {
121 blue_ = static_cast<uint8_t>(std::clamp(b, 0.0f, 1.0f) * RGB_MAX);
122 }
123
SetAlphaF(scalar a)124 void Color::SetAlphaF(scalar a)
125 {
126 alpha_ = static_cast<uint8_t>(std::clamp(a, 0.0f, 1.0f) * RGB_MAX);
127 }
128
SetRgb(uint32_t r,uint32_t g,uint32_t b,uint32_t a)129 void Color::SetRgb(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
130 {
131 alpha_ = a;
132 red_ = r;
133 green_ = g;
134 blue_ = b;
135 }
136
SetRgbF(scalar r,scalar g,scalar b,scalar a)137 void Color::SetRgbF(scalar r, scalar g, scalar b, scalar a)
138 {
139 alpha_ = static_cast<uint32_t>(round(std::clamp(a, 0.0f, 1.0f) * RGB_MAX));
140 red_ = static_cast<uint32_t>(round(std::clamp(r, 0.0f, 1.0f) * RGB_MAX));
141 green_ = static_cast<uint32_t>(round(std::clamp(g, 0.0f, 1.0f) * RGB_MAX));
142 blue_ = static_cast<uint32_t>(round(std::clamp(b, 0.0f, 1.0f) * RGB_MAX));
143 }
144
SetColorQuad(uint32_t c)145 void Color::SetColorQuad(uint32_t c)
146 {
147 alpha_ = Color::ColorQuadGetA(c);
148 red_ = Color::ColorQuadGetR(c);
149 green_ = Color::ColorQuadGetG(c);
150 blue_ = Color::ColorQuadGetB(c);
151 }
152
operator ==(const Color & c1,const Color & c2)153 bool operator==(const Color& c1, const Color& c2)
154 {
155 return c1.alpha_ == c2.alpha_ && c1.red_ == c2.red_ && c1.green_ == c2.green_ && c1.blue_ == c2.blue_;
156 }
operator !=(const Color & c1,const Color & c2)157 bool operator!=(const Color& c1, const Color& c2)
158 {
159 return c1.alpha_ != c2.alpha_ || c1.red_ != c2.red_ || c1.green_ != c2.green_ || c1.blue_ != c2.blue_;
160 }
161
Dump(std::string & out) const162 void Color::Dump(std::string& out) const
163 {
164 constexpr int32_t colorStrWidth = 8;
165 std::stringstream ss;
166 ss << "[ARGB-0x" << std::hex << std::setfill('0') << std::setw(colorStrWidth) << std::uppercase;
167 ss << CastToColorQuad() << ']';
168 out += ss.str();
169 }
170 } // namespace Drawing
171 } // namespace Rosen
172 } // namespace OHOS
173