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