This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
170 lines (151 loc) · 5.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const ase = require('ase-utils'),
clean = require('gulp-clean'),
fs = require('fs'),
gulp = require('gulp'),
handlebars = require('handlebars'),
jeditor = require("gulp-json-editor"),
map = require('through2-map'),
rename = require('gulp-rename'),
shell = require('gulp-shell'),
spy = require('through2-spy');
module.exports = function(options) {
/*--- paths and files ------------------------------------------------------*/
const filename = module.parent.filename,
rootPath = filename.substring(0, Math.max(filename.lastIndexOf("/"), filename.lastIndexOf("\\"))),
basePath = rootPath.includes('color-bee')
? ''
: '/node_modules/color-bee';
const config = {
templates: `${rootPath}${basePath}/source/templates/*.hbs`,
output: options.output
? `${rootPath}/${options.output}`
: `${rootPath}/`,
partials: `${rootPath}${basePath}/source/templates/partials/*`,
source: options.source
? `${rootPath}/${options.source}`
: './source/colors.js',
templates: rootPath.includes('color-bee')
? './source/templates/*.hbs'
: `${rootPath}/node_modules/color-bee/source/templates/*.hbs`,
};
const colors = require(config.source);
console.log(`Color Bee is buzzing the ${colors.name} palette from ${config.source} to ${config.output}!`);
if (!fs.existsSync(config.output)){
fs.mkdirSync(config.output);
}
/*--- functions -------------------------------------------------------------*/
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
red: parseInt(result[1], 16) / 255,
green: parseInt(result[2], 16) / 255,
blue: parseInt(result[3], 16) / 255,
alpha: 1
} : null;
};
/*--- build tasks ------------------------------------------------------------*/
gulp.task('ase', function() {
let aseObj = {
"version": colors.version,
"groups": [],
"colors": []
};
aseObj.groups = colors.palettes.map(function(obj){
const color = obj.name;
const aseGroup = {
name: color
};
const formArray = obj.values.map(function(colors){
const hex = function(hex) {
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [
parseInt(result[1], 16) / 255,
parseInt(result[2], 16) / 255,
parseInt(result[3], 16) / 255
] : null;
}
const formColor = {
"name": color + ' ' + colors.grade,
"model": 'RGB',
"color": hex(colors.value),
"type": "global"
};
return formColor;
});
aseGroup.colors = formArray;
return aseGroup;
});
aseObj["colors"] = [].concat.apply([], aseObj.colors);
fs.writeFileSync(`${config.output}/${colors.name}.ase`, ase.encode(aseObj));
});
gulp.task('clr', [ 'sketchpalette' ], function () {
gulp.src(`${config.output}${colors.name}.scl`)
.pipe(shell([
`chmod u+x ${rootPath}${basePath}/lib/color-tool/color-tool`,
`${rootPath}${basePath}/lib/color-tool/color-tool create-clr ${config.output}${colors.name}.scl ${config.output}${colors.name}.clr`,
]))
.pipe(clean({force: true}))
});
gulp.task('eyeglass', () =>
gulp.src('./eyeglass-exports.js')
.pipe(gulp.dest(config.output))
);
gulp.task('partials', () =>
gulp.src(config.partials)
.pipe(spy.obj(chunk =>
// register each file in the partials folder as a handlebars partial,
// using its own path name, to be compiled on demand when referenced
handlebars.registerPartial(chunk.relative, chunk.contents.toString())
))
);
gulp.task('sketchpalette', [ 'templates' ], function () {
gulp.src(`${config.output}/${colors.name}.json`)
.pipe(jeditor(function(json) {
let sketchPalette = {
compatibleVersion: "1.4",
pluginVersion: "1.5"
};
sketchPalette.colors = Object.keys(json).map(function (color) { return json[color]; });
sketchPalette.colors = sketchPalette.colors.map(function(color) {
var arr = Object.keys(color).map(function (grade) { return color[grade]; });
return arr
});
sketchPalette.colors = [].concat.apply([], sketchPalette.colors);
sketchPalette.colors = sketchPalette.colors.map(hex => (hexToRgb(hex)));
return sketchPalette;
}))
.pipe(rename(`${colors.name}.sketchpalette`))
.pipe(gulp.dest(config.output))
})
gulp.task('templates', ['partials'], () => {
gulp.src(config.templates)
.pipe(map.obj(chunk => {
// compile each handlebars file in the templates folder, then evaluate
// the compiled template with the color definitions as context data,
// and replace the pipeline item with the output buffer
var template = handlebars.compile(chunk.contents.toString());
chunk.contents = new Buffer(template({ colors: colors }));
return chunk;
}))
.pipe(rename(path => {
// a template file of the form AAA.hbs produces output file name.AAA
const dot = path.basename.lastIndexOf('.');
path.extname = path.basename.substring(dot);
path.basename = path.extname === 'scss' ? `_${colors.name}.` : `${colors.name}.`;
}))
.pipe(gulp.dest(config.output))
});
gulp.task('default', [ 'ase', 'clr', 'eyeglass'], () => {
console.log('Color Bee is done buzzing!')
});
gulp.start('default');
};