1 /*
2 * Copyright (c) 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 "core/components_ng/property/gradient_property.h"
17
18 namespace OHOS::Ace::NG {
19
AddColor(const GradientColor & color)20 void Gradient::AddColor(const GradientColor& color)
21 {
22 colors_.push_back(color);
23 }
24
ClearColors()25 void Gradient::ClearColors()
26 {
27 colors_.clear();
28 }
29
GetColorsAndRepeating(std::unique_ptr<JsonValue> & resultJson,const Gradient & gradient)30 static void GetColorsAndRepeating(std::unique_ptr<JsonValue>& resultJson, const Gradient& gradient)
31 {
32 auto jsonColorArray = JsonUtil::CreateArray(true);
33 auto colors = gradient.GetColors();
34 for (size_t i = 0; i < colors.size(); ++i) {
35 auto temp = JsonUtil::CreateArray(true);
36 auto value = std::to_string(colors[i].GetDimension().Value() / 100.0);
37 auto color = colors[i].GetColor().ColorToString();
38 temp->Put("0", color.c_str());
39 temp->Put("1", value.c_str());
40 auto index = std::to_string(i);
41 jsonColorArray->Put(index.c_str(), temp);
42 }
43 resultJson->Put("colors", jsonColorArray);
44 resultJson->Put("repeating", gradient.GetRepeat() ? "true" : "false");
45 }
46
LinearGradientToJson() const47 std::unique_ptr<JsonValue> Gradient::LinearGradientToJson() const
48 {
49 auto resultJson = JsonUtil::Create(true);
50 if (GradientType::LINEAR != GetType()) {
51 return resultJson;
52 }
53 CHECK_NULL_RETURN(linearGradient_, resultJson);
54 if (linearGradient_->angle.has_value()) {
55 resultJson->Put("angle", linearGradient_->angle->ToString().c_str());
56 }
57
58 auto linearX = linearGradient_->linearX;
59 auto linearY = linearGradient_->linearY;
60 if (linearX == GradientDirection::LEFT) {
61 if (linearY == GradientDirection::TOP) {
62 resultJson->Put("direction", "GradientDirection.LeftTop");
63 } else if (linearY == GradientDirection::BOTTOM) {
64 resultJson->Put("direction", "GradientDirection.LeftBottom");
65 } else {
66 resultJson->Put("direction", "GradientDirection.Left");
67 }
68 } else if (linearX == GradientDirection::RIGHT) {
69 if (linearY == GradientDirection::TOP) {
70 resultJson->Put("direction", "GradientDirection.RightTop");
71 } else if (linearY == GradientDirection::BOTTOM) {
72 resultJson->Put("direction", "GradientDirection.RightBottom");
73 } else {
74 resultJson->Put("direction", "GradientDirection.Right");
75 }
76 } else {
77 if (linearY == GradientDirection::TOP) {
78 resultJson->Put("direction", "GradientDirection.Top");
79 } else if (linearY == GradientDirection::BOTTOM) {
80 resultJson->Put("direction", "GradientDirection.Bottom");
81 } else {
82 resultJson->Put("direction", "GradientDirection.None");
83 }
84 }
85 GetColorsAndRepeating(resultJson, *this);
86 return resultJson;
87 }
88
SweepGradientToJson() const89 std::unique_ptr<JsonValue> Gradient::SweepGradientToJson() const
90 {
91 auto resultJson = JsonUtil::Create(true);
92
93 if (GradientType::SWEEP != GetType()) {
94 return resultJson;
95 }
96 CHECK_NULL_RETURN(sweepGradient_, resultJson);
97 auto radialCenterX = sweepGradient_->centerX;
98 auto radialCenterY = sweepGradient_->centerY;
99 if (radialCenterX && radialCenterY) {
100 auto jsPoint = JsonUtil::CreateArray(true);
101 jsPoint->Put("0", radialCenterX->ToString().c_str());
102 jsPoint->Put("1", radialCenterY->ToString().c_str());
103 resultJson->Put("center", jsPoint);
104 }
105
106 auto startAngle = sweepGradient_->startAngle;
107 auto endAngle = sweepGradient_->endAngle;
108 if (startAngle) {
109 resultJson->Put("start", startAngle->ToString().c_str());
110 }
111 if (endAngle) {
112 resultJson->Put("end", endAngle->ToString().c_str());
113 }
114
115 GetColorsAndRepeating(resultJson, *this);
116
117 return resultJson;
118 }
119
RadialGradientToJson() const120 std::unique_ptr<JsonValue> Gradient::RadialGradientToJson() const
121 {
122 auto resultJson = JsonUtil::Create(true);
123 if (GradientType::RADIAL != GetType()) {
124 return resultJson;
125 }
126 CHECK_NULL_RETURN(radialGradient_, resultJson);
127
128 auto radialCenterX = radialGradient_->radialCenterX;
129 auto radialCenterY = radialGradient_->radialCenterY;
130 if (radialCenterX && radialCenterY) {
131 auto jsPoint = JsonUtil::CreateArray(true);
132 jsPoint->Put("0", radialCenterX->ToString().c_str());
133 jsPoint->Put("1", radialCenterY->ToString().c_str());
134 resultJson->Put("center", jsPoint);
135 }
136
137 auto radius = radialGradient_->radialVerticalSize;
138 if (radius) {
139 resultJson->Put("radius", radius->ToString().c_str());
140 }
141
142 GetColorsAndRepeating(resultJson, *this);
143
144 return resultJson;
145 }
146
147 } // namespace OHOS::Ace::NG