@@ -45,10 +45,7 @@ class JsiSkColor : public RNJsi::JsiHostObject {
45
45
static SkColor fromValue (jsi::Runtime &runtime, const jsi::Value &obj) {
46
46
const auto &object = obj.asObject (runtime);
47
47
jsi::ArrayBuffer buffer =
48
- object
49
- .getProperty (runtime, jsi::PropNameID::forAscii (runtime, " buffer" ))
50
- .asObject (runtime)
51
- .getArrayBuffer (runtime);
48
+ object.getPropertyAsObject (runtime, " buffer" ).getArrayBuffer (runtime);
52
49
auto bfrPtr = reinterpret_cast <float *>(buffer.data (runtime));
53
50
if (bfrPtr[0 ] > 1 || bfrPtr[1 ] > 1 || bfrPtr[2 ] > 1 || bfrPtr[3 ] > 1 ) {
54
51
return SK_ColorBLACK;
@@ -77,22 +74,23 @@ class JsiSkColor : public RNJsi::JsiHostObject {
77
74
runtime, SkColorSetARGB (color.a * 255 , color.r , color.g , color.b ));
78
75
} else if (arguments[0 ].isObject ()) {
79
76
auto obj = arguments[0 ].getObject (runtime);
80
- if (obj.isArrayBuffer (runtime)) {
81
- return obj;
82
- } else if (obj.isArray (runtime)) {
77
+ if (obj.isArray (runtime)) {
83
78
auto array = obj.asArray (runtime);
84
- double r = std::clamp ( array. getValueAtIndex (runtime, 0 ). getNumber (),
85
- 0.0 , 255.0 );
86
- double g = std::clamp (array. getValueAtIndex (runtime, 1 ). getNumber (),
87
- 0.0 , 255.0 );
88
- double b = std::clamp (array. getValueAtIndex (runtime, 2 ). getNumber (),
89
- 0.0 , 255.0 );
90
- double a = std::clamp (array. getValueAtIndex (runtime, 3 ). getNumber (),
91
- 0.0 , 255.0 );
92
- return JsiSkColor::toValue (runtime, SkColorSetARGB (a, r, g, b)) ;
79
+ // turn the array into a Float32Array
80
+ return runtime. global ()
81
+ . getPropertyAsFunction (runtime, " Float32Array " )
82
+ . callAsConstructor (runtime, array)
83
+ . getObject (runtime);
84
+ } else if (obj. hasProperty (runtime, " buffer " ) &&
85
+ obj. getPropertyAsObject (runtime, " buffer " )
86
+ . isArrayBuffer (runtime)) {
87
+ return obj ;
93
88
}
94
89
}
95
- return jsi::Value::undefined ();
90
+ throw jsi::JSError (runtime,
91
+ " Skia.Color expected number, Float32Array, number[], "
92
+ " or string and got: " +
93
+ arguments[0 ].toString (runtime).utf8 (runtime));
96
94
};
97
95
}
98
96
};
0 commit comments