1 /*
2 * Copyright (c) 2022-2025 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 #include "base/json/json_util.h"
18 #include "base/log/log.h"
19
20 namespace OHOS::Ace::NG {
21
AddColor(const GradientColor & color)22 void Gradient::AddColor(const GradientColor& color)
23 {
24 colors_.push_back(color);
25 }
26
ClearColors()27 void Gradient::ClearColors()
28 {
29 colors_.clear();
30 }
31
CreateGradientWithType(GradientType type)32 void Gradient::CreateGradientWithType(GradientType type)
33 {
34 type_ = type;
35 switch (type_) {
36 case GradientType::LINEAR:
37 linearGradient_ = std::make_shared<LinearGradient>();
38 break;
39 case GradientType::RADIAL:
40 radialGradient_ = std::make_shared<RadialGradient>();
41 break;
42 case GradientType::SWEEP:
43 sweepGradient_ = std::make_shared<SweepGradient>();
44 break;
45 default:
46 LOGE("GradientType not supported");
47 }
48 }
49
50 namespace GradientJsonUtils {
51
GetColorsAndRepeating(std::unique_ptr<JsonValue> & resultJson,const Gradient & gradient)52 static void GetColorsAndRepeating(std::unique_ptr<JsonValue>& resultJson, const Gradient& gradient)
53 {
54 auto jsonColorArray = JsonUtil::CreateArray(true);
55 auto colors = gradient.GetColors();
56 for (size_t i = 0; i < colors.size(); ++i) {
57 auto temp = JsonUtil::CreateArray(true);
58 auto value = std::to_string(colors[i].GetDimension().Value() / 100.0);
59 auto color = colors[i].GetColor().ColorToString();
60 temp->Put("0", color.c_str());
61 temp->Put("1", value.c_str());
62 auto index = std::to_string(i);
63 jsonColorArray->Put(index.c_str(), temp);
64 }
65 resultJson->Put("colors", jsonColorArray);
66 resultJson->Put("repeating", gradient.GetRepeat() ? "true" : "false");
67 }
68
LinearGradientToJson(const Gradient & data)69 std::unique_ptr<JsonValue> LinearGradientToJson(const Gradient& data)
70 {
71 auto resultJson = JsonUtil::Create(true);
72 if (GradientType::LINEAR != data.GetType()) {
73 return resultJson;
74 }
75 auto linearGradient = data.GetLinearGradient();
76 CHECK_NULL_RETURN(linearGradient, resultJson);
77 if (linearGradient->angle.has_value()) {
78 resultJson->Put("angle", linearGradient->angle->ToString().c_str());
79 }
80
81 auto linearX = linearGradient->linearX;
82 auto linearY = linearGradient->linearY;
83 if (linearX == GradientDirection::LEFT) {
84 if (linearY == GradientDirection::TOP) {
85 resultJson->Put("direction", "GradientDirection.LeftTop");
86 } else if (linearY == GradientDirection::BOTTOM) {
87 resultJson->Put("direction", "GradientDirection.LeftBottom");
88 } else {
89 resultJson->Put("direction", "GradientDirection.Left");
90 }
91 } else if (linearX == GradientDirection::RIGHT) {
92 if (linearY == GradientDirection::TOP) {
93 resultJson->Put("direction", "GradientDirection.RightTop");
94 } else if (linearY == GradientDirection::BOTTOM) {
95 resultJson->Put("direction", "GradientDirection.RightBottom");
96 } else {
97 resultJson->Put("direction", "GradientDirection.Right");
98 }
99 } else {
100 if (linearY == GradientDirection::TOP) {
101 resultJson->Put("direction", "GradientDirection.Top");
102 } else if (linearY == GradientDirection::BOTTOM) {
103 resultJson->Put("direction", "GradientDirection.Bottom");
104 } else {
105 resultJson->Put("direction", "GradientDirection.None");
106 }
107 }
108 GetColorsAndRepeating(resultJson, data);
109 return resultJson;
110 }
111
SweepGradientToJson(const Gradient & data)112 std::unique_ptr<JsonValue> SweepGradientToJson(const Gradient& data)
113 {
114 auto resultJson = JsonUtil::Create(true);
115
116 if (GradientType::SWEEP != data.GetType()) {
117 return resultJson;
118 }
119 auto sweepGradient = data.GetSweepGradient();
120 CHECK_NULL_RETURN(sweepGradient, resultJson);
121 auto radialCenterX = sweepGradient->centerX;
122 auto radialCenterY = sweepGradient->centerY;
123 if (radialCenterX && radialCenterY) {
124 auto jsPoint = JsonUtil::CreateArray(true);
125 jsPoint->Put("0", radialCenterX->ToString().c_str());
126 jsPoint->Put("1", radialCenterY->ToString().c_str());
127 resultJson->Put("center", jsPoint);
128 }
129
130 auto startAngle = sweepGradient->startAngle;
131 auto endAngle = sweepGradient->endAngle;
132 if (startAngle) {
133 resultJson->Put("start", startAngle->ToString().c_str());
134 }
135 if (endAngle) {
136 resultJson->Put("end", endAngle->ToString().c_str());
137 }
138
139 GetColorsAndRepeating(resultJson, data);
140
141 return resultJson;
142 }
143
RadialGradientToJson(const Gradient & data)144 std::unique_ptr<JsonValue> RadialGradientToJson(const Gradient& data)
145 {
146 auto resultJson = JsonUtil::Create(true);
147 if (GradientType::RADIAL != data.GetType()) {
148 return resultJson;
149 }
150 auto radialGradient = data.GetRadialGradient();
151 CHECK_NULL_RETURN(radialGradient, resultJson);
152
153 auto radialCenterX = radialGradient->radialCenterX;
154 auto radialCenterY = radialGradient->radialCenterY;
155 if (radialCenterX && radialCenterY) {
156 auto jsPoint = JsonUtil::CreateArray(true);
157 jsPoint->Put("0", radialCenterX->ToString().c_str());
158 jsPoint->Put("1", radialCenterY->ToString().c_str());
159 resultJson->Put("center", jsPoint);
160 }
161
162 auto radius = radialGradient->radialVerticalSize;
163 if (radius) {
164 resultJson->Put("radius", radius->ToString().c_str());
165 }
166
167 GetColorsAndRepeating(resultJson, data);
168
169 return resultJson;
170 }
171 } // namespace GradientJsonUtils
172
173 } // namespace OHOS::Ace::NG