-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
64 lines (46 loc) · 1.13 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
var path = require('path');
var _ = require('lodash');
var color = require('onecolor');
var yazl = require('yazl');
var validateColors = function(colors) {
if (!_.isArray(colors) || !colors.length) {
throw new TypeError('Expected an array of colors with at least 1 valid color');
}
return colors;
};
var filterValidColors = function(colors) {
colors = validateColors(colors);
colors = colors.map(color).filter(_.isObject).slice(0, 30);
return validateColors(colors);
};
var createJSON = function(colors, name) {
var swatches = colors.map(
function(item) {
var hsv = item.hsv();
return {
hue: hsv.h(),
brightness: hsv.v(),
saturation: hsv.s()
};
}
);
return [
{
name: name,
swatches: swatches
}
];
};
var createZip = function(obj, name) {
var zip = new yazl.ZipFile();
var buffer = new Buffer(JSON.stringify(obj, null, 2));
zip.addBuffer(buffer, 'Swatches.json');
zip.end();
return zip.outputStream;
};
module.exports = function(colors, name) {
colors = filterValidColors(colors);
var obj = createJSON(colors, path.basename(name, '.swatches'));
return createZip(obj, name);
};