@@ -23,7 +23,7 @@ describe("Colors", () => {
2323 } ;
2424
2525 const c1 = Skia . Color ( "cyan" ) ;
26- const c2 = Skia . Color ( [ 0 , 1 , 1 , 1 ] ) ;
26+ const c2 = Skia . Color ( [ 0 , 255 , 255 , 255 ] ) ;
2727 const c3 = Skia . Color ( 0xff00ffff ) ;
2828 const c4 = Skia . Color ( Float32Array . of ( 0 , 1 , 1 , 1 ) ) ;
2929
@@ -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 : "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+ } ) ;
3876} ) ;
0 commit comments