1- import { expectToMatchColor } from '../../test/lib/util' ;
2- import interpolate , { isSupportedInterpolationColorSpace } from './interpolate' ;
3- import Color from './color' ;
4- import Padding from './padding' ;
5- import VariableAnchorOffsetCollection from './variable_anchor_offset_collection' ;
6-
7- describe ( 'interpolate' , ( ) => {
8-
9- test ( 'interpolate number' , ( ) => {
10- expect ( interpolate . number ( - 5 , 5 , 0.00 ) ) . toBe ( - 5.0 ) ;
11- expect ( interpolate . number ( - 5 , 5 , 0.25 ) ) . toBe ( - 2.5 ) ;
12- expect ( interpolate . number ( - 5 , 5 , 0.50 ) ) . toBe ( 0 ) ;
13- expect ( interpolate . number ( - 5 , 5 , 0.75 ) ) . toBe ( 2.5 ) ;
14- expect ( interpolate . number ( - 5 , 5 , 1.00 ) ) . toBe ( 5.0 ) ;
15-
16- expect ( interpolate . number ( 0 , 1 , 0.5 ) ) . toBe ( 0.5 ) ;
17- expect ( interpolate . number ( - 10 , - 5 , 0.5 ) ) . toBe ( - 7.5 ) ;
18- expect ( interpolate . number ( 5 , 10 , 0.5 ) ) . toBe ( 7.5 ) ;
1+ import { expectCloseToArray , expectToMatchColor } from '../../../test/lib/util' ;
2+ import Color , { isSupportedInterpolationColorSpace } from './color' ;
3+
4+ describe ( 'Color class' , ( ) => {
5+
6+ describe ( 'parsing' , ( ) => {
7+
8+ test ( 'should parse valid css color strings' , ( ) => {
9+ expectToMatchColor ( Color . parse ( 'RED' ) , 'rgb(100% 0% 0% / 1)' ) ;
10+ expectToMatchColor ( Color . parse ( '#f00C' ) , 'rgb(100% 0% 0% / .8)' ) ;
11+ expectToMatchColor ( Color . parse ( 'rgb(0 0 127.5 / 20%)' ) , 'rgb(0% 0% 50% / .2)' ) ;
12+ expectToMatchColor ( Color . parse ( 'hsl(300deg 100% 25.1% / 0.7)' ) , 'rgb(50.2% 0% 50.2% / .7)' ) ;
13+ } ) ;
14+
15+ test ( 'should return undefined when provided with invalid CSS color string' , ( ) => {
16+ expect ( Color . parse ( undefined ) ) . toBeUndefined ( ) ;
17+ expect ( Color . parse ( null ) ) . toBeUndefined ( ) ;
18+ expect ( Color . parse ( '#invalid' ) ) . toBeUndefined ( ) ;
19+ expect ( Color . parse ( '$123' ) ) . toBeUndefined ( ) ;
20+ expect ( Color . parse ( '0F91' ) ) . toBeUndefined ( ) ;
21+ expect ( Color . parse ( 'rgb(#123)' ) ) . toBeUndefined ( ) ;
22+ expect ( Color . parse ( 'hsl(0,0,0)' ) ) . toBeUndefined ( ) ;
23+ expect ( Color . parse ( 'rgb(0deg,0,0)' ) ) . toBeUndefined ( ) ;
24+ } ) ;
25+
26+ test ( 'should accept instances of Color class' , ( ) => {
27+ const color = new Color ( 0 , 0 , 0 , 0 ) ;
28+ expect ( Color . parse ( color ) ) . toBe ( color ) ;
29+ } ) ;
30+
31+ } ) ;
32+
33+ test ( 'should keep a reference to the original color when alpha=0' , ( ) => {
34+ const color = new Color ( 0 , 0 , 0.5 , 0 , false ) ;
35+ expect ( color ) . toMatchObject ( { r : 0 , g : 0 , b : 0 , a : 0 } ) ;
36+ expect ( Object . hasOwn ( color , 'rgb' ) ) . toBe ( true ) ;
37+ expectCloseToArray ( color . rgb , [ 0 , 0 , 0.5 , 0 ] ) ;
38+ } ) ;
39+
40+ test ( 'should not keep a reference to the original color when alpha!=0' , ( ) => {
41+ const color = new Color ( 0 , 0 , 0.5 , 0.001 , false ) ;
42+ expect ( color ) . toMatchObject ( { r : 0 , g : 0 , b : expect . closeTo ( 0.5 * 0.001 , 5 ) , a : 0.001 } ) ;
43+ expect ( Object . hasOwn ( color , 'rgb' ) ) . toBe ( false ) ;
44+ } ) ;
45+
46+ test ( 'should serialize to rgba format' , ( ) => {
47+ expect ( `${ new Color ( 1 , 1 , 0 , 1 , false ) } ` ) . toBe ( 'rgba(255,255,0,1)' ) ;
48+ expect ( `${ new Color ( 0.2 , 0 , 1 , 0.3 , false ) } ` ) . toBe ( 'rgba(51,0,255,0.3)' ) ;
49+ expect ( `${ new Color ( 1 , 1 , 0 , 0 , false ) } ` ) . toBe ( 'rgba(255,255,0,0)' ) ;
50+ expect ( `${ Color . parse ( 'purple' ) } ` ) . toBe ( 'rgba(128,0,128,1)' ) ;
51+ expect ( `${ Color . parse ( 'rgba(26,207,26,.73)' ) } ` ) . toBe ( 'rgba(26,207,26,0.73)' ) ;
52+ expect ( `${ Color . parse ( 'rgba(26,207,26,0)' ) } ` ) . toBe ( 'rgba(26,207,26,0)' ) ;
1953 } ) ;
2054
2155 describe ( 'interpolation color space' , ( ) => {
@@ -44,7 +78,7 @@ describe('interpolate', () => {
4478 const color = Color . parse ( 'rgba(0,0,255,1)' ) ;
4579 const targetColor = Color . parse ( 'rgba(0,255,0,.6)' ) ;
4680
47- const i11nFn = ( t : number ) => interpolate . color ( color , targetColor , t , 'rgb' ) ;
81+ const i11nFn = ( t : number ) => Color . interpolate ( color , targetColor , t , 'rgb' ) ;
4882 expectToMatchColor ( i11nFn ( 0.00 ) , 'rgb(0% 0% 100% / 1)' ) ;
4983 expectToMatchColor ( i11nFn ( 0.25 ) , 'rgb(0% 25% 75% / 0.9)' ) ;
5084 expectToMatchColor ( i11nFn ( 0.50 ) , 'rgb(0% 50% 50% / 0.8)' ) ;
@@ -56,7 +90,7 @@ describe('interpolate', () => {
5690 const color = Color . parse ( 'rgba(0,0,255,1)' ) ;
5791 const targetColor = Color . parse ( 'rgba(0,255,0,.6)' ) ;
5892
59- const i11nFn = ( t : number ) => interpolate . color ( color , targetColor , t , 'hcl' ) ;
93+ const i11nFn = ( t : number ) => Color . interpolate ( color , targetColor , t , 'hcl' ) ;
6094 expectToMatchColor ( i11nFn ( 0.00 ) , 'rgb(0% 0% 100% / 1)' ) ;
6195 expectToMatchColor ( i11nFn ( 0.25 ) , 'rgb(0% 49.37% 100% / 0.9)' , 4 ) ;
6296 expectToMatchColor ( i11nFn ( 0.50 ) , 'rgb(0% 70.44% 100% / 0.8)' , 4 ) ;
@@ -68,7 +102,7 @@ describe('interpolate', () => {
68102 const color = Color . parse ( 'rgba(0,0,255,1)' ) ;
69103 const targetColor = Color . parse ( 'rgba(0,255,0,.6)' ) ;
70104
71- const i11nFn = ( t : number ) => interpolate . color ( color , targetColor , t , 'lab' ) ;
105+ const i11nFn = ( t : number ) => Color . interpolate ( color , targetColor , t , 'lab' ) ;
72106 expectToMatchColor ( i11nFn ( 0.00 ) , 'rgb(0% 0% 100% / 1)' ) ;
73107 expectToMatchColor ( i11nFn ( 0.25 ) , 'rgb(39.64% 34.55% 83.36% / 0.9)' , 4 ) ;
74108 expectToMatchColor ( i11nFn ( 0.50 ) , 'rgb(46.42% 56.82% 65.91% / 0.8)' , 4 ) ;
@@ -80,7 +114,7 @@ describe('interpolate', () => {
80114 const color = Color . parse ( 'rgba(0,0,255,0)' ) ;
81115 const targetColor = Color . parse ( 'rgba(0,255,0,1)' ) ;
82116
83- const i11nFn = ( t : number ) => interpolate . color ( color , targetColor , t , 'rgb' ) ;
117+ const i11nFn = ( t : number ) => Color . interpolate ( color , targetColor , t , 'rgb' ) ;
84118 expectToMatchColor ( i11nFn ( 0.00 ) , 'rgb(0% 0% 0% / 0)' ) ;
85119 expectToMatchColor ( i11nFn ( 0.25 ) , 'rgb(0% 25% 75% / 0.25)' ) ;
86120 expectToMatchColor ( i11nFn ( 0.50 ) , 'rgb(0% 50% 50% / 0.5)' ) ;
@@ -93,7 +127,7 @@ describe('interpolate', () => {
93127 const targetColor = Color . parse ( 'cyan' ) ;
94128
95129 for ( const space of [ 'rgb' , 'hcl' , 'lab' ] as const ) {
96- const i11nFn = ( t : number ) => interpolate . color ( color , targetColor , t , space ) ;
130+ const i11nFn = ( t : number ) => Color . interpolate ( color , targetColor , t , space ) ;
97131 const colorInBetween = i11nFn ( 0.5 ) ;
98132 for ( const key of [ 'r' , 'g' , 'b' , 'a' ] as const ) {
99133 expect ( colorInBetween [ key ] ) . toBeGreaterThanOrEqual ( 0 ) ;
@@ -104,31 +138,4 @@ describe('interpolate', () => {
104138
105139 } ) ;
106140
107- test ( 'interpolate array' , ( ) => {
108- expect ( interpolate . array ( [ 0 , 0 , 0 , 0 ] , [ 1 , 2 , 3 , 4 ] , 0.5 ) ) . toEqual ( [ 0.5 , 1 , 3 / 2 , 2 ] ) ;
109- } ) ;
110-
111- test ( 'interpolate padding' , ( ) => {
112- const padding = new Padding ( [ 0 , 0 , 0 , 0 ] ) ;
113- const targetPadding = new Padding ( [ 1 , 2 , 6 , 4 ] ) ;
114-
115- const i11nFn = ( t : number ) => interpolate . padding ( padding , targetPadding , t ) ;
116- expect ( i11nFn ( 0.5 ) ) . toBeInstanceOf ( Padding ) ;
117- expect ( i11nFn ( 0.5 ) ) . toEqual ( new Padding ( [ 0.5 , 1 , 3 , 2 ] ) ) ;
118- } ) ;
119-
120- describe ( 'interpolate variableAnchorOffsetCollection' , ( ) => {
121- const i11nFn = interpolate . variableAnchorOffsetCollection ;
122- const parseFn = VariableAnchorOffsetCollection . parse ;
123-
124- test ( 'should throw with mismatched endpoints' , ( ) => {
125- expect ( ( ) => i11nFn ( parseFn ( [ 'top' , [ 0 , 0 ] ] ) , parseFn ( [ 'bottom' , [ 1 , 1 ] ] ) , 0.5 ) ) . toThrow ( 'Cannot interpolate values containing mismatched anchors. from[0]: top, to[0]: bottom' ) ;
126- expect ( ( ) => i11nFn ( parseFn ( [ 'top' , [ 0 , 0 ] ] ) , parseFn ( [ 'top' , [ 1 , 1 ] , 'bottom' , [ 2 , 2 ] ] ) , 0.5 ) ) . toThrow ( 'Cannot interpolate values of different length. from: ["top",[0,0]], to: ["top",[1,1],"bottom",[2,2]]' ) ;
127- } ) ;
128-
129- test ( 'should interpolate offsets' , ( ) => {
130- expect ( i11nFn ( parseFn ( [ 'top' , [ 0 , 0 ] , 'bottom' , [ 2 , 2 ] ] ) , parseFn ( [ 'top' , [ 1 , 1 ] , 'bottom' , [ 4 , 4 ] ] ) , 0.5 ) . values ) . toEqual ( [ 'top' , [ 0.5 , 0.5 ] , 'bottom' , [ 3 , 3 ] ] ) ;
131- } ) ;
132- } ) ;
133-
134141} ) ;
0 commit comments