-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
49 lines (39 loc) · 1003 Bytes
/
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
44
45
46
47
48
49
'use strict'
/**
* @module color-interpolate
* Pick color from palette by index
*/
const parse = require('color-parse');
const hsl = require('color-space/hsl');
const lerp = require('lerp');
const clamp = require('clamp');
module.exports = interpolate;
function interpolate (palette) {
palette = palette.map(function(c) {
c = parse(c);
if (c.space != 'rgb') {
if (c.space != 'hsl') throw 'c.space' + 'space is not supported.';
c.values = hsl.rgb(c.values);
}
c.values.push(c.alpha);
return c.values;
});
return function(t, mix) {
mix = mix || lerp;
t = clamp(t, 0, 1);
var idx = ( palette.length - 1 ) * t,
lIdx = Math.floor( idx ),
rIdx = Math.ceil( idx );
t = idx - lIdx;
var lColor = palette[lIdx], rColor = palette[rIdx];
var result = lColor.map(function(v, i) {
v = mix(v, rColor[i], t);
if (i < 3) v = Math.round(v);
return v;
});
if (result[3] === 1) {
return 'rgb('+result.slice(0,3)+')';
}
return 'rgba('+result+')';
};
}