@@ -35,4 +35,42 @@ describe("Colors", () => {
3535 } ) ;
3636 expect ( result ) . toBe ( true ) ;
3737 } ) ;
38+ it ( "should render the same color regardless of constructor input types" , async ( ) => {
39+ // given (different inputs representing the same color
40+ const colors = [
41+ { kind : "string" as const , value : "red" } ,
42+ { kind : "float32Array" as const , value : [ 1 , 0 , 0 , 1 ] } ,
43+ { kind : "number" as const , value : 0xffff0000 } ,
44+ { kind : "array" as const , value : [ 1 , 0 , 0 , 1 ] } ,
45+ ] ;
46+
47+ // when (for each input we draw a colored canvas and encode it to bytes)
48+ const buffers = await Promise . all (
49+ colors . map ( ( color ) =>
50+ surface
51+ . drawOffscreen (
52+ ( Skia , canvas , ctx ) => {
53+ const c =
54+ ctx . color . kind === "float32Array"
55+ ? // we cannot pass in a Float32Array via ctx, need to construct it inside
56+ new Float32Array ( ctx . color . value )
57+ : ctx . color . value ;
58+ canvas . drawColor ( Skia . Color ( c ) ) ;
59+ } ,
60+ { color }
61+ )
62+ . then ( ( image ) => image . encodeToBytes ( ) )
63+ )
64+ ) ;
65+
66+ // then (expect the encoded bytes are equal)
67+ for ( let i = 1 ; i < buffers . length ; i ++ ) {
68+ const prev = buffers [ i - 1 ] ;
69+ const curr = buffers [ i ] ;
70+ expect ( prev . length ) . toBe ( curr . length ) ;
71+ for ( let j = 0 ; j < prev . length ; j ++ ) {
72+ expect ( prev [ j ] ) . toBe ( curr [ j ] ) ;
73+ }
74+ }
75+ } ) ;
3876} ) ;
0 commit comments