-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (32 loc) · 1016 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
/**
* A simple color interpolator.
* Parses a `from` and `to` HSL string in the format
* `hsl(200, 100%, 50%)` and a step value between 0 and 1
* and returns a new HSL string.
* An optional `precision` value may be provided for the
* new HSL string
*
* @param {string} from
* @param {string} to
* @param {number} step
* @param {precision} number
*/
var regex = /^hsl\(\s*([\-|\d|\.]*)\s*,\s*([\d|\.]*)%\s*,\s*([\d|\.]*)%/;
function interpolate (start, end, step, precision) {
precision = precision != null ? precision : 0;
start = start.match(regex);
end = end.match(regex);
var
startH = +start[1],
startS = +start[2],
startL = +start[3],
endH = +end[1],
endS = +end[2],
endL = +end[3];
var
h = (startH - (startH - endH) * step).toFixed(precision),
s = (startS - (startS - endS) * step).toFixed(precision),
l = (startL - (startL - endL) * step).toFixed(precision);
return 'hsl(' + h + ', ' + s + '%, ' + l + '%)';
}
module.exports = interpolate;