-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (34 loc) · 1.03 KB
/
index.js
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
const colorCache = {
}
module.exports = function (startColor, endColor, percent){
let key = startColor+endColor
if(!colorCache.hasOwnProperty(key)){
key = endColor+startColor
percent = 100 - percent
}
if(colorCache.hasOwnProperty(key)){
return colorCache[key][Math.round(percent)]
}
const re = /#(.{2})(.{2})(.{2})/;
let _start = re.exec(startColor)
let _end = re.exec(endColor)
let start = [
parseInt(_start[1], 16),
parseInt(_start[2], 16),
parseInt(_start[3], 16)
]
let diff = [
parseInt(_end[1], 16) - start[0],
parseInt(_end[2], 16) - start[1],
parseInt(_end[3], 16) - start[2]
]
let frames = Array(101).fill(1).map((i, percent) => {
return '#' + start.map((color, index) => {
let difference = Math.round(diff[index] / 100 * percent)
let newValue = (color + difference).toString(16)
return newValue.length === 1? '0' + newValue : newValue
}).join('')
}).reverse()
colorCache[key] = frames;
return frames[Math.round(percent)]
}