@@ -23,7 +23,7 @@ describe("Colors", () => {
23
23
} ;
24
24
25
25
const c1 = Skia . Color ( "cyan" ) ;
26
- const c2 = Skia . Color ( [ 0 , 1 , 1 , 1 ] ) ;
26
+ const c2 = Skia . Color ( [ 0 , 255 , 255 , 255 ] ) ;
27
27
const c3 = Skia . Color ( 0xff00ffff ) ;
28
28
const c4 = Skia . Color ( Float32Array . of ( 0 , 1 , 1 , 1 ) ) ;
29
29
@@ -35,4 +35,42 @@ describe("Colors", () => {
35
35
} ) ;
36
36
expect ( result ) . toBe ( true ) ;
37
37
} ) ;
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 : "floatArray32" as const , value : [ 1 , 0 , 0 , 1 ] } ,
43
+ { kind : "number" as const , value : 0xff0000ff } ,
44
+ { kind : "array" as const , value : [ 255 , 0 , 0 , 255 ] } ,
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 === "floatArray32"
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
+ } ) ;
38
76
} ) ;
0 commit comments