-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (26 loc) · 800 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
'use strict';
var tinycolor = require('tinycolor2');
var map = require('lodash.mapvalues');
module.exports = function(base, target, decimals) {
if (!base || !tinycolor(base).isValid()) {
throw new Error('Argument `base` "' + base + '" is not a valid color.');
}
if (!target || !tinycolor(target).isValid()) {
throw new Error('Argument `target` "' + target + '" is not a valid color.');
}
// Handle equal colors
// tinycolor.equals(base, target)
base = tinycolor(base).toHsl();
target = new tinycolor(target).toHsl();
var diff = {
hue: -(base.h - target.h),
saturation: base.s - target.s,
lightness: base.l - target.l
};
return map(diff, function(value) {
if (decimals > 0) {
return value.toFixed(decimals);
}
return value;
});
};