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
19 namespace OHOS {
20 namespace Rosen {
21 namespace Drawing {
Color()22 Color::Color() noexcept : alpha_(RGB_MAX), red_(0), green_(0), blue_(0) {}
23
Color(const Color & c)24 Color::Color(const Color& c) noexcept : alpha_(c.GetAlpha()), red_(c.GetRed()), green_(c.GetGreen()), blue_(c.GetBlue())
25 {}
26
Color(uint32_t r,uint32_t g,uint32_t b,uint32_t a)27 Color::Color(uint32_t r, uint32_t g, uint32_t b, uint32_t a) noexcept : alpha_(a), red_(r), green_(g), blue_(b) {}
28
29 // Return alpha byte, red component, green component and blue component of color rgba.
Color(ColorQuad rgba)30 Color::Color(ColorQuad rgba) noexcept
31 {
32 alpha_ = rgba >> 24;
33 red_ = (rgba >> 16) & 0xff;
34 green_ = (rgba >> 8) & 0xff;
35 blue_ = (rgba >> 0) & 0xff;
36 }
37
GetRed() const38 uint32_t Color::GetRed() const
39 {
40 return red_;
41 }
42
GetGreen() const43 uint32_t Color::GetGreen() const
44 {
45 return green_;
46 }
47
GetBlue() const48 uint32_t Color::GetBlue() const
49 {
50 return blue_;
51 }
52
GetAlpha() const53 uint32_t Color::GetAlpha() const
54 {
55 return alpha_;
56 }
57
SetRed(uint32_t r)58 void Color::SetRed(uint32_t r)
59 {
60 red_ = r;
61 }
62
SetGreen(uint32_t g)63 void Color::SetGreen(uint32_t g)
64 {
65 green_ = g;
66 }
67
SetBlue(uint32_t b)68 void Color::SetBlue(uint32_t b)
69 {
70 blue_ = b;
71 }
72
SetAlpha(uint32_t a)73 void Color::SetAlpha(uint32_t a)
74 {
75 alpha_ = a;
76 }
77
GetRedF() const78 scalar Color::GetRedF() const
79 {
80 return static_cast<scalar>(red_) / RGB_MAX;
81 }
82
GetGreenF() const83 scalar Color::GetGreenF() const
84 {
85 return static_cast<scalar>(green_) / RGB_MAX;
86 }
87
GetBlueF() const88 scalar Color::GetBlueF() const
89 {
90 return static_cast<scalar>(blue_) / RGB_MAX;
91 }
92
GetAlphaF() const93 scalar Color::GetAlphaF() const
94 {
95 return static_cast<scalar>(alpha_) / RGB_MAX;
96 }
97
GetColor4f()98 Color4f Color::GetColor4f()
99 {
100 color4f_.redF_ = GetRedF();
101 color4f_.greenF_ = GetGreenF();
102 color4f_.blueF_ = GetBlueF();
103 color4f_.alphaF_ = GetAlphaF();
104 return color4f_;
105 }
106
SetRedF(scalar r)107 void Color::SetRedF(scalar r)
108 {
109 red_ = static_cast<uint8_t>(std::clamp(r, 0.0f, 1.0f) * RGB_MAX);
110 }
111
SetGreenF(scalar g)112 void Color::SetGreenF(scalar g)
113 {
114 green_ = static_cast<uint8_t>(std::clamp(g, 0.0f, 1.0f) * RGB_MAX);
115 }
116
SetBlueF(scalar b)117 void Color::SetBlueF(scalar b)
118 {
119 blue_ = static_cast<uint8_t>(std::clamp(b, 0.0f, 1.0f) * RGB_MAX);
120 }
121
SetAlphaF(scalar a)122 void Color::SetAlphaF(scalar a)
123 {
124 alpha_ = static_cast<uint8_t>(std::clamp(a, 0.0f, 1.0f) * RGB_MAX);
125 }
126
SetRgb(uint32_t r,uint32_t g,uint32_t b,uint32_t a)127 void Color::SetRgb(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
128 {
129 alpha_ = a;
130 red_ = r;
131 green_ = g;
132 blue_ = b;
133 }
134
SetRgbF(scalar r,scalar g,scalar b,scalar a)135 void Color::SetRgbF(scalar r, scalar g, scalar b, scalar a)
136 {
137 alpha_ = static_cast<uint32_t>(round(std::clamp(a, 0.0f, 1.0f) * RGB_MAX));
138 red_ = static_cast<uint32_t>(round(std::clamp(r, 0.0f, 1.0f) * RGB_MAX));
139 green_ = static_cast<uint32_t>(round(std::clamp(g, 0.0f, 1.0f) * RGB_MAX));
140 blue_ = static_cast<uint32_t>(round(std::clamp(b, 0.0f, 1.0f) * RGB_MAX));
141 }
142
SetColorQuad(uint32_t c)143 void Color::SetColorQuad(uint32_t c)
144 {
145 alpha_ = Color::ColorQuadGetA(c);
146 red_ = Color::ColorQuadGetR(c);
147 green_ = Color::ColorQuadGetG(c);
148 blue_ = Color::ColorQuadGetB(c);
149 }
150
CastToColorQuad() const151 ColorQuad Color::CastToColorQuad() const
152 {
153 return Color::ColorQuadSetARGB(red_, green_, blue_, alpha_);
154 }
155
operator ==(const Color & c1,const Color & c2)156 bool operator==(const Color& c1, const Color& c2)
157 {
158 return c1.alpha_ == c2.alpha_ && c1.red_ == c2.red_ && c1.green_ == c2.green_ && c1.blue_ == c2.blue_;
159 }
operator !=(const Color & c1,const Color & c2)160 bool operator!=(const Color& c1, const Color& c2)
161 {
162 return c1.alpha_ != c2.alpha_ || c1.red_ != c2.red_ || c1.green_ != c2.green_ || c1.blue_ != c2.blue_;
163 }
164 } // namespace Drawing
165 } // namespace Rosen
166 } // namespace OHOS
167