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/brush.h"
17
18 #include "static_factory.h"
19
20 namespace OHOS {
21 namespace Rosen {
22 namespace Drawing {
Brush()23 Brush::Brush() noexcept
24 : color_(),
25 blendMode_(BlendMode::SRC_OVER),
26 filter_(),
27 colorSpace_(nullptr),
28 shaderEffect_(nullptr),
29 blender_(nullptr),
30 antiAlias_(false)
31 {}
32
Brush(const Color & c)33 Brush::Brush(const Color& c) noexcept
34 : color_(c),
35 blendMode_(BlendMode::SRC_OVER),
36 filter_(),
37 colorSpace_(nullptr),
38 shaderEffect_(nullptr),
39 blender_(nullptr),
40 antiAlias_(false)
41 {}
42
Brush(int rgba)43 Brush::Brush(int rgba) noexcept
44 : color_(rgba),
45 blendMode_(BlendMode::SRC_OVER),
46 filter_(),
47 colorSpace_(nullptr),
48 shaderEffect_(nullptr),
49 blender_(nullptr),
50 antiAlias_(false)
51 {}
52
Brush(std::shared_ptr<ShaderEffect> e)53 Brush::Brush(std::shared_ptr<ShaderEffect> e) noexcept
54 : color_(), blendMode_(BlendMode::SRC_OVER), filter_(), colorSpace_(nullptr), shaderEffect_(e), blender_(nullptr),
55 antiAlias_(false)
56 {}
57
GetColor() const58 const Color& Brush::GetColor() const
59 {
60 return color_;
61 }
62
SetColor(const Color & c)63 void Brush::SetColor(const Color& c)
64 {
65 color_ = c;
66 }
67
SetColor(uint32_t c)68 void Brush::SetColor(uint32_t c)
69 {
70 color_.SetColorQuad(c);
71 }
72
SetARGB(int a,int r,int g,int b)73 void Brush::SetARGB(int a, int r, int g, int b)
74 {
75 color_.SetRgb(r, g, b, a);
76 }
77
GetColor4f()78 Color4f Brush::GetColor4f()
79 {
80 return color_.GetColor4f();
81 }
82
GetColorSpace() const83 std::shared_ptr<ColorSpace> Brush::GetColorSpace() const
84 {
85 return colorSpace_;
86 }
87
SetColor(const Color4f & cf,std::shared_ptr<ColorSpace> s)88 void Brush::SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s)
89 {
90 color_.SetRgbF(cf.redF_, cf.greenF_, cf.blueF_, cf.alphaF_);
91 colorSpace_ = s;
92 }
93
SetAlpha(uint32_t a)94 void Brush::SetAlpha(uint32_t a)
95 {
96 color_.SetAlpha(a);
97 }
98
SetAlphaF(scalar a)99 void Brush::SetAlphaF(scalar a)
100 {
101 color_.SetAlphaF(a);
102 }
103
SetBlendMode(const BlendMode & mode)104 void Brush::SetBlendMode(const BlendMode& mode)
105 {
106 blendMode_ = mode;
107 }
108
SetFilter(const Filter & filter)109 void Brush::SetFilter(const Filter& filter)
110 {
111 filter_ = filter;
112 hasFilter_ = true;
113 }
114
GetFilter() const115 const Filter& Brush::GetFilter() const
116 {
117 return filter_;
118 }
119
SetShaderEffect(std::shared_ptr<ShaderEffect> e)120 void Brush::SetShaderEffect(std::shared_ptr<ShaderEffect> e)
121 {
122 shaderEffect_ = e;
123 }
124
GetShaderEffect() const125 std::shared_ptr<ShaderEffect> Brush::GetShaderEffect() const
126 {
127 return shaderEffect_;
128 }
129
SetBlender(std::shared_ptr<Blender> blender)130 void Brush::SetBlender(std::shared_ptr<Blender> blender)
131 {
132 blender_ = blender;
133 }
134
IsAntiAlias() const135 bool Brush::IsAntiAlias() const
136 {
137 return antiAlias_;
138 }
139
SetAntiAlias(bool aa)140 void Brush::SetAntiAlias(bool aa)
141 {
142 antiAlias_ = aa;
143 }
144
CanComputeFastBounds()145 bool Brush::CanComputeFastBounds()
146 {
147 return StaticFactory::CanComputeFastBounds(*this);
148 }
149
ComputeFastBounds(const Rect & orig,Rect * storage)150 const Rect& Brush::ComputeFastBounds(const Rect& orig, Rect* storage)
151 {
152 return StaticFactory::ComputeFastBounds(*this, orig, storage);
153 }
154
Reset()155 void Brush::Reset()
156 {
157 *this = Brush();
158 }
159
AsBlendMode()160 bool Brush::AsBlendMode()
161 {
162 return StaticFactory::AsBlendMode(*this);
163 }
164
operator ==(const Brush & b1,const Brush & b2)165 bool operator==(const Brush& b1, const Brush& b2)
166 {
167 return b1.color_ == b2.color_ && b1.blendMode_ == b2.blendMode_ && b1.shaderEffect_ == b2.shaderEffect_ &&
168 b1.blender_ == b2.blender_ && b1.colorSpace_ == b2.colorSpace_ && b1.filter_ == b2.filter_ &&
169 b1.antiAlias_ == b2.antiAlias_;
170 }
171
operator !=(const Brush & b1,const Brush & b2)172 bool operator!=(const Brush& b1, const Brush& b2)
173 {
174 return !(b1 == b2);
175 }
176 } // namespace Drawing
177 } // namespace Rosen
178 } // namespace OHOS
179