@@ -84,14 +84,22 @@ class JsiSkShaderFactory : public JsiSkHostObject {
84
84
SkPoint pts[] = {p1, p2};
85
85
86
86
std::vector<SkColor> colors = getColors (runtime, arguments[2 ]);
87
+ auto colorsSize = colors.size ();
88
+ if (colorsSize < 2 ) {
89
+ throw std::invalid_argument (" colors must have at least 2 colors" );
90
+ }
87
91
std::vector<SkScalar> positions = getPositions (runtime, arguments[3 ]);
92
+ if (!positions.empty () && positions.size () != colorsSize) {
93
+ throw std::invalid_argument (
94
+ " positions must be empty or have the same size as colors" );
95
+ }
88
96
auto tileMode = getTileMode (arguments, 4 , count);
89
97
auto flag = getFlag (arguments, 6 , count);
90
98
auto localMatrix = getLocalMatrix (runtime, arguments, 5 , count);
91
99
92
100
sk_sp<SkShader> gradient = SkGradientShader::MakeLinear (
93
- pts, colors.data (), positions.data (), static_cast < int >(colors. size ()) ,
94
- tileMode, flag, localMatrix);
101
+ pts, colors.data (), ! positions.empty () ? positions. data () : nullptr ,
102
+ static_cast < int >(colorsSize), tileMode, flag, localMatrix);
95
103
return jsi::Object::createFromHostObject (
96
104
runtime,
97
105
std::make_shared<JsiSkShader>(getContext (), std::move (gradient)));
@@ -103,14 +111,23 @@ class JsiSkShaderFactory : public JsiSkHostObject {
103
111
auto r = arguments[1 ].asNumber ();
104
112
105
113
std::vector<SkColor> colors = getColors (runtime, arguments[2 ]);
114
+ auto colorsSize = colors.size ();
115
+ if (colorsSize < 2 ) {
116
+ throw std::invalid_argument (" colors must have at least 2 colors" );
117
+ }
106
118
std::vector<SkScalar> positions = getPositions (runtime, arguments[3 ]);
119
+ if (!positions.empty () && positions.size () != colorsSize) {
120
+ throw std::invalid_argument (
121
+ " positions must be empty or the same size as colors" );
122
+ }
107
123
auto tileMode = getTileMode (arguments, 4 , count);
108
124
auto flag = getFlag (arguments, 6 , count);
109
125
auto localMatrix = getLocalMatrix (runtime, arguments, 5 , count);
110
126
111
127
sk_sp<SkShader> gradient = SkGradientShader::MakeRadial (
112
- center, r, colors.data (), positions.data (),
113
- static_cast <int >(colors.size ()), tileMode, flag, localMatrix);
128
+ center, r, colors.data (),
129
+ !positions.empty () ? positions.data () : nullptr ,
130
+ static_cast <int >(colorsSize), tileMode, flag, localMatrix);
114
131
return jsi::Object::createFromHostObject (
115
132
runtime,
116
133
std::make_shared<JsiSkShader>(getContext (), std::move (gradient)));
@@ -120,7 +137,15 @@ class JsiSkShaderFactory : public JsiSkHostObject {
120
137
auto x = arguments[0 ].asNumber ();
121
138
auto y = arguments[1 ].asNumber ();
122
139
std::vector<SkColor> colors = getColors (runtime, arguments[2 ]);
140
+ auto colorsSize = colors.size ();
141
+ if (colorsSize < 2 ) {
142
+ throw std::invalid_argument (" colors must have at least 2 colors" );
143
+ }
123
144
std::vector<SkScalar> positions = getPositions (runtime, arguments[3 ]);
145
+ if (!positions.empty () && positions.size () != colorsSize) {
146
+ throw std::invalid_argument (
147
+ " positions must be empty or the same size as colors" );
148
+ }
124
149
auto tileMode = getTileMode (arguments, 4 , count);
125
150
auto localMatrix = getLocalMatrix (runtime, arguments, 5 , count);
126
151
auto flag = getFlag (arguments, 6 , count);
@@ -130,8 +155,9 @@ class JsiSkShaderFactory : public JsiSkHostObject {
130
155
? 360
131
156
: arguments[8 ].asNumber ();
132
157
sk_sp<SkShader> gradient = SkGradientShader::MakeSweep (
133
- x, y, colors.data (), positions.data (), static_cast <int >(colors.size ()),
134
- tileMode, startAngle, endAngle, flag, localMatrix);
158
+ x, y, colors.data (), !positions.empty () ? positions.data () : nullptr ,
159
+ static_cast <int >(colorsSize), tileMode, startAngle, endAngle, flag,
160
+ localMatrix);
135
161
return jsi::Object::createFromHostObject (
136
162
runtime,
137
163
std::make_shared<JsiSkShader>(getContext (), std::move (gradient)));
@@ -147,14 +173,23 @@ class JsiSkShaderFactory : public JsiSkHostObject {
147
173
auto endRadius = arguments[3 ].asNumber ();
148
174
149
175
std::vector<SkColor> colors = getColors (runtime, arguments[4 ]);
176
+ auto colorsSize = colors.size ();
177
+ if (colorsSize < 2 ) {
178
+ throw std::invalid_argument (" colors must have at least 2 colors" );
179
+ }
150
180
std::vector<SkScalar> positions = getPositions (runtime, arguments[5 ]);
181
+ if (!positions.empty () && positions.size () != colorsSize) {
182
+ throw std::invalid_argument (
183
+ " positions must be empty or the same size as colors" );
184
+ }
151
185
auto tileMode = getTileMode (arguments, 6 , count);
152
186
auto localMatrix = getLocalMatrix (runtime, arguments, 7 , count);
153
187
auto flag = getFlag (arguments, 8 , count);
154
188
155
189
sk_sp<SkShader> gradient = SkGradientShader::MakeTwoPointConical (
156
- start, startRadius, end, endRadius, colors.data (), positions.data (),
157
- static_cast <int >(colors.size ()), tileMode, flag, localMatrix);
190
+ start, startRadius, end, endRadius, colors.data (),
191
+ !positions.empty () ? positions.data () : nullptr ,
192
+ static_cast <int >(colorsSize), tileMode, flag, localMatrix);
158
193
159
194
return jsi::Object::createFromHostObject (
160
195
runtime,
0 commit comments