react-native-skia icon indicating copy to clipboard operation
react-native-skia copied to clipboard

Handle SkColor creation from rgba tuple

Open louix opened this issue 1 year ago • 5 comments

Skia.Color accepts number[], but the native constructor doesn't actually account for it.

In JsiSkColor::createCtor, if argument[0].isObject() (true for both Array and Float32Array), it's returned as is as the color value.

When the jsi::Value is made into to an SkColor in JsiSkColor::FromValue, the .buffer property (which is of course is not there on an array -- thus undefined) is attempted to be accessed.

I've added support here.

Orthogonally, have you considered using [r: number, g: number, b: number, a: number] instead of number[] for extra type saftey?

Resolves #2113

louix avatar Jan 16 '24 22:01 louix

I've signed the cla. Bit weird it asks for an address.

louix avatar Jan 16 '24 22:01 louix

Thanks a lot I'm excited to sanitize the situation there. I added some tests for it at https://github.com/louix/react-native-skia/pull/1 but it seems to fail on Android. I will look into it.

wcandillon avatar Jan 17 '24 07:01 wcandillon

You were correct about 0-1 vs 0-255! I changed the JSI side slightly to mimic the web one (Array become Float32Array), and added the offscreen canvas test.

The native side was just returning undefined if it wasn't sure how to handle the value, so you'd get an error where you tried to use the SkColor instead of when you created it. I added an throw at creation time (and an exhaustive one to the web side to match).

louix avatar Jan 17 '24 18:01 louix

@louix please give me a bit of time to digest this. Recent work we've done may suggest we need to slightly change how colors are dealt with behind the scene.

wcandillon avatar Jan 30 '24 09:01 wcandillon

but either way the tests you've written will be very useful.

wcandillon avatar Jan 30 '24 09:01 wcandillon