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 "bridge/declarative_frontend/jsview/canvas/js_canvas_gradient.h"
17
18 #include "bridge/declarative_frontend/jsview/canvas/js_rendering_context.h"
19 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
20
21 namespace OHOS::Ace::Framework {
22
JSCanvasGradient()23 JSCanvasGradient::JSCanvasGradient() {}
24
Constructor(const JSCallbackInfo & args)25 void JSCanvasGradient::Constructor(const JSCallbackInfo& args)
26 {
27 auto jsCanvasGradient = Referenced::MakeRefPtr<JSCanvasGradient>();
28 jsCanvasGradient->IncRefCount();
29 args.SetReturnValue(Referenced::RawPtr(jsCanvasGradient));
30 }
31
Destructor(JSCanvasGradient * controller)32 void JSCanvasGradient::Destructor(JSCanvasGradient* controller)
33 {
34 if (controller != nullptr) {
35 controller->DecRefCount();
36 }
37 }
38
JSBind(BindingTarget globalObj)39 void JSCanvasGradient::JSBind(BindingTarget globalObj)
40 {
41 JSClass<JSCanvasGradient>::Declare("CanvasGradient");
42 JSClass<JSCanvasGradient>::CustomMethod("addColorStop", &JSCanvasGradient::AddColorStop);
43 JSClass<JSCanvasGradient>::Bind(globalObj, JSCanvasGradient::Constructor, JSCanvasGradient::Destructor);
44 }
45
46 // if the params is invalid, fill the shape with transparent
AddColorTransparent()47 void JSCanvasGradient::AddColorTransparent()
48 {
49 CHECK_NULL_VOID(gradient_);
50 isColorStopValid_ = false;
51 gradient_->ClearColors();
52 GradientColor color;
53 color.SetColor(Color::TRANSPARENT);
54 color.SetDimension(0.0);
55 gradient_->AddColor(color);
56 gradient_->AddColor(color);
57 }
58
AddColorStop(const JSCallbackInfo & info)59 void JSCanvasGradient::AddColorStop(const JSCallbackInfo& info)
60 {
61 CHECK_NULL_VOID(gradient_);
62 if (!isColorStopValid_ && gradient_->GetColors().empty()) {
63 isColorStopValid_ = true;
64 }
65 if (!isColorStopValid_ || !info[0]->IsNumber() || (!info[1]->IsString() && !info[1]->IsObject())) {
66 return;
67 }
68 double offset = 0.0;
69 JSViewAbstract::ParseJsDouble(info[0], offset);
70 if (offset < 0 || offset > 1) {
71 AddColorTransparent();
72 return;
73 }
74 GradientColor gradientColor;
75 Color color = Color::WHITE;
76 ColorSpace colorSpace = ColorSpace::SRGB;
77 if (info[1]->IsString()) {
78 std::string colorStr;
79 JSViewAbstract::ParseJsString(info[1], colorStr);
80 if (!Color::ParseColorString(colorStr, color)) {
81 AddColorTransparent();
82 return;
83 }
84 } else {
85 if (!JSViewAbstract::ParseColorMetricsToColor(info[1], color)) {
86 AddColorTransparent();
87 return;
88 }
89 colorSpace = color.GetColorSpace();
90 }
91 if (!gradient_->GetColors().empty() && colorSpace != colorSpace_) {
92 JSException::Throw(
93 ERROR_CODE_CANVAS_PARAM_INVALID, "%s", "The color's ColorSpace is not the same as the last color's.");
94 return;
95 }
96 gradientColor.SetColor(color);
97 gradientColor.SetDimension(offset);
98 if (gradient_->GetColors().empty()) {
99 colorSpace_ = colorSpace;
100 // prevent setting only one colorStop
101 gradient_->AddColor(gradientColor);
102 }
103 gradient_->AddColor(gradientColor);
104 }
105
106 } // namespace OHOS::Ace::Framework
107