File size: 7,603 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { createInterpolator, Globals } from '.'
import { createStringInterpolator } from './stringInterpolation'
import { colors } from './colors'

beforeAll(() => {
  Globals.assign({
    createStringInterpolator,
    colors,
  })
})

describe('Interpolation', () => {
  it('should work with defaults', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: [0, 1],
    })

    expect(interpolation(0)).toBe(0)
    expect(interpolation(0.5)).toBe(0.5)
    expect(interpolation(0.8)).toBe(0.8)
    expect(interpolation(1)).toBe(1)
  })

  it('should work with interpolation function as argument', () => {
    const interpolation = createInterpolator<number, string>(
      value => `scale(${value})`
    )

    expect(interpolation(0)).toBe('scale(0)')
    expect(interpolation(10.5)).toBe('scale(10.5)')
  })

  it('should work with range arrays as arguments', () => {
    const interpolation = createInterpolator([0, 1], [100, 200])

    expect(interpolation(0)).toBe(100)
    expect(interpolation(0.5)).toBe(150)
    expect(interpolation(0.8)).toBe(180)
    expect(interpolation(1)).toBe(200)
  })

  it('should work with output range', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: [100, 200],
    })

    expect(interpolation(0)).toBe(100)
    expect(interpolation(0.5)).toBe(150)
    expect(interpolation(0.8)).toBe(180)
    expect(interpolation(1)).toBe(200)
  })

  it('should work with input range', () => {
    const interpolation = createInterpolator({
      range: [100, 200],
      output: [0, 1],
    })

    expect(interpolation(100)).toBe(0)
    expect(interpolation(150)).toBe(0.5)
    expect(interpolation(180)).toBe(0.8)
    expect(interpolation(200)).toBe(1)
  })

  it('should work with keyframes without extrapolate', () => {
    const interpolation = createInterpolator({
      range: [0, 1, 2],
      output: [0.2, 1, 0.2],
      extrapolate: 'clamp',
    })

    expect(interpolation(5)).toBeCloseTo(0.2)
  })

  it('should work with output ranges as string', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: ['rgba(0, 100, 200, 0)', 'rgba(50, 150, 250, 0.4)'],
    })

    expect(interpolation(0)).toBe('rgba(0, 100, 200, 0)')
    expect(interpolation(0.5)).toBe('rgba(25, 125, 225, 0.2)')
    expect(interpolation(1)).toBe('rgba(50, 150, 250, 0.4)')
  })

  it('should work with output ranges as short hex string', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: ['#024', '#9BF'],
    })

    expect(interpolation(0)).toBe('rgba(0, 34, 68, 1)')
    expect(interpolation(0.5)).toBe('rgba(77, 111, 162, 1)')
    expect(interpolation(1)).toBe('rgba(153, 187, 255, 1)')
  })

  it('should work with output ranges as long hex string', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: ['#FF9500', '#87FC70'],
    })

    expect(interpolation(0)).toBe('rgba(255, 149, 0, 1)')
    expect(interpolation(0.5)).toBe('rgba(195, 201, 56, 1)')
    expect(interpolation(1)).toBe('rgba(135, 252, 112, 1)')
  })

  it('should work with output ranges with mixed hex and rgba strings', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: ['rgba(100, 120, 140, 0.4)', '#87FC70'],
    })

    expect(interpolation(0)).toBe('rgba(100, 120, 140, 0.4)')
    expect(interpolation(0.5)).toBe('rgba(118, 186, 126, 0.7)')
    expect(interpolation(1)).toBe('rgba(135, 252, 112, 1)')
  })

  it('should work with negative and decimal values in string ranges', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: ['-100.5deg', '100deg'],
    })

    expect(interpolation(0)).toBe('-100.5deg')
    expect(interpolation(0.5)).toBe('-0.25deg')
    expect(interpolation(1)).toBe('100deg')
  })

  it('should interpolate between number and unit', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: ['0', '100%'],
    })

    expect(interpolation(0.5)).toBe('50%')
  })

  it('should support a mix of color patterns', () => {
    const interpolation = createInterpolator({
      range: [0, 1, 2],
      output: ['rgba(0, 100, 200, 0)', 'rgb(50, 150, 250)', 'red'],
    })

    expect(interpolation(0)).toBe('rgba(0, 100, 200, 0)')
    expect(interpolation(0.5)).toBe('rgba(25, 125, 225, 0.5)')
    expect(interpolation(1.5)).toBe('rgba(153, 75, 125, 1)')
    expect(interpolation(2)).toBe('rgba(255, 0, 0, 1)')
  })

  it('should round rgb values', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: ['rgba(0, 0, 0, 0)', 'rgba(3, 3, 3, 1)'],
    })

    expect(interpolation(0.5)).toBe('rgba(2, 2, 2, 0.5)')
  })

  it('should not match partial color names', () => {
    const interpolation = createInterpolator({
      range: [0, 1],
      output: ['grayscale(0%)', 'grayscale(100%)'],
    })

    expect(interpolation(0.5)).toBe('grayscale(50%)')
  })

  describe('CSS Variables', () => {
    const originalGetComputedStyle = window.getComputedStyle

    const getComputedStyleMock = () => ({
      getPropertyValue: (variableName: '--from' | '--to' | '--no') => {
        switch (variableName) {
          case '--from':
            return '#3cffd0'
          case '--to':
            return '#277ef4'
          case '--no':
            return undefined
          default:
            throw Error('Should never happen')
        }
      },
    })

    beforeAll(() => {
      // @ts-expect-error for testing purposes only.
      window.getComputedStyle = getComputedStyleMock
    })
    afterAll(() => {
      window.getComputedStyle = originalGetComputedStyle
    })

    it('should correctly parse variables', () => {
      const interpolation = createInterpolator({
        range: [0, 1],
        output: ['var(--to)', 'var(--from)'],
      })

      expect(interpolation(0.0)).toBe('rgba(39, 126, 244, 1)')
      expect(interpolation(0.5)).toBe('rgba(50, 191, 226, 1)')
      expect(interpolation(1.0)).toBe('rgba(60, 255, 208, 1)')
    })

    it('should return the fallbacks if the variable cannot be found', () => {
      const interpolation = createInterpolator({
        range: [0, 1],
        output: ['var(--no, pink)', 'var(--no, purple)'],
      })

      expect(interpolation(0.0)).toBe('rgba(255, 192, 203, 1)')
      expect(interpolation(0.5)).toBe('rgba(192, 96, 166, 1)')
      expect(interpolation(1.0)).toBe('rgba(128, 0, 128, 1)')
    })

    it('should correctly parse the fallback if its a variable', () => {
      let interpolation = createInterpolator({
        range: [0, 1],
        output: ['var(--no, --to)', 'var(--no, --from)'],
      })

      expect(interpolation(0.0)).toBe('rgba(39, 126, 244, 1)')
      expect(interpolation(0.5)).toBe('rgba(50, 191, 226, 1)')
      expect(interpolation(1.0)).toBe('rgba(60, 255, 208, 1)')

      interpolation = createInterpolator({
        range: [0, 1],
        output: ['var(--no, var(--to))', 'var(--no, var(--from))'],
      })

      expect(interpolation(0.0)).toBe('rgba(39, 126, 244, 1)')
      expect(interpolation(0.5)).toBe('rgba(50, 191, 226, 1)')
      expect(interpolation(1.0)).toBe('rgba(60, 255, 208, 1)')

      interpolation = createInterpolator({
        range: [0, 1],
        output: ['var(--no, var(--no, pink))', 'var(--no, var(--no, purple))'],
      })

      expect(interpolation(0.0)).toBe('rgba(255, 192, 203, 1)')
      expect(interpolation(0.5)).toBe('rgba(192, 96, 166, 1)')
      expect(interpolation(1.0)).toBe('rgba(128, 0, 128, 1)')
    })
  })
})