-
Notifications
You must be signed in to change notification settings - Fork 0
/
palette2oco.js
executable file
·538 lines (527 loc) · 13.7 KB
/
palette2oco.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#! /usr/bin/env node
import { relative, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import _ from 'lodash';
import { truwrap } from 'truwrap';
import { TemplateTag, replaceSubstitutionTransformer, stripIndent } from 'common-tags';
import { box } from '@thebespokepixel/string';
import meta from '@thebespokepixel/meta';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { globby } from 'globby';
import updateNotifier from 'update-notifier';
import { simple } from 'trucolor';
import { createConsole } from 'verbosity';
import fs from 'node:fs';
import { promisify } from 'es6-promisify';
import { OCOValueEX, fromBytes, fromPrecise } from '@thebespokepixel/oco-colorvalue-ex';
import oco from 'opencolor';
import ase from 'ase-util';
const name = "@thebespokepixel/palette2oco";
const version = "3.0.2";
const description = "Convert directories of Sip, JSON and ASE palette files to Open Color";
const main = "index.js";
const types = "index.d.ts";
const type = "module";
const files = [
"index.js",
"index.d.ts"
];
const bin = {
palette2oco: "./palette2oco.js"
};
const scripts = {
build: "rollup -c && chmod 755 palette2oco.js && npm run readme",
test: "xo && c8 --reporter=text ava",
"doc-serve": "documentation serve --watch --theme node_modules/documentation-theme-bespoke --github --config src/docs/documentation.yml --project-name $npm_package_name --project-version $npm_package_version src/index.js",
"doc-build": "documentation build --format html --output docs --theme node_modules/documentation-theme-bespoke --github --config src/docs/documentation.yml --project-name $npm_package_name --project-version $npm_package_version src/index.js",
readme: "compile-readme -u src/docs/example.md src/docs/readme.md > readme.md",
coverage: "c8 --reporter=lcov ava; open coverage/lcov-report/index.html",
prepublishOnly: "npx -p typescript tsc index.js --declaration --allowJs --emitDeclarationOnly"
};
const repository = {
type: "git",
url: "git+https://github.com/thebespokepixel/palette2oco.git"
};
const keywords = [
"Open",
"Color",
"oco",
"sip",
"ase",
"json",
"color",
"colour",
"converter"
];
const author = "Mark Griffiths <mark@thebespokepixel.com> (http://thebespokepixel.com/)";
const copyright = {
year: "2021",
owner: "The Bespoke Pixel"
};
const license = "MIT";
const bugs = {
url: "https://github.com/thebespokepixel/palette2oco/issues"
};
const homepage = "https://github.com/thebespokepixel/palette2oco#readme";
const dependencies = {
"@thebespokepixel/meta": "^3.0.5",
"@thebespokepixel/oco-colorvalue-ex": "^6.0.1",
"@thebespokepixel/string": "^2.0.1",
"ase-util": "^1.0.3",
"common-tags": "^1.8.0",
"es6-promisify": "^7.0.0",
globby: "^12.0.2",
lodash: "^4.17.21",
opencolor: "^0.2.0",
trucolor: "^4.0.4",
truwrap: "^4.0.4",
"update-notifier": "^5.1.0",
verbosity: "^3.0.2",
yargs: "^17.2.1"
};
const devDependencies = {
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.6",
"@types/estree": "^0.0.50",
ava: "^4.0.0-rc.1",
c8: "^7.10.0",
"documentation-theme-bespoke": "^2.0.14",
rollup: "^2.59.0",
"rollup-plugin-cleanup": "^3.2.1",
xo: "^0.46.4"
};
const xo = {
semicolon: false,
ignores: [
"index.js",
"palette2oco.js",
"index.d.ts",
"docs/**",
"coverage/**"
]
};
const badges = {
github: "thebespokepixel",
npm: "thebespokepixel",
"libraries-io": "TheBespokePixel",
name: "palette2oco",
codeclimate: "d58dd3cdc1459632afa5",
providers: {
aux1: {
title: "github",
text: "source",
color: "4E73B6",
link: "https://github.com/thebespokepixel/palette2oco"
}
},
readme: {
"Publishing Status": [
[
"npm",
"libraries-io-npm"
],
[
"travis-com",
"rollup"
]
],
"Development Status": [
[
"travis-com-dev",
"libraries-io-github"
],
[
"snyk",
"code-climate",
"code-climate-coverage"
]
],
"Documentation/Help": [
"inch",
"twitter"
]
},
docs: [
[
"aux1",
"travis"
],
[
"code-climate",
"code-climate-coverage"
],
[
"snyk",
"libraries-io-npm"
]
]
};
const engines = {
node: ">=10.0"
};
var pkg = {
name: name,
version: version,
description: description,
main: main,
types: types,
type: type,
files: files,
bin: bin,
scripts: scripts,
repository: repository,
keywords: keywords,
author: author,
copyright: copyright,
license: license,
bugs: bugs,
homepage: homepage,
dependencies: dependencies,
devDependencies: devDependencies,
xo: xo,
badges: badges,
engines: engines
};
const loader = promisify(fs.readFile);
const supportedTypes = [
'oco',
'json',
'sippalette',
'ase',
];
const fileFilter = new RegExp(`.(${supportedTypes.join('|')})$`);
const fileMatch = new RegExp(`(.*/)*(.+?).(${supportedTypes.join('|')})$`);
function createIdentity(rootPath) {
return function (path) {
const address = path
.replace(rootPath, '')
.match(fileMatch);
return {
source: path,
name: address[2],
path: address[1].replace(/^\//, '').replace(/\//g, '.'),
type: address[3],
}
}
}
function testPaletteJSONColor(datum, min, max) {
return {
name: typeof datum.name === 'string' && datum.name,
red: (datum.red >= min && datum.red <= max) && datum.red,
green: (datum.green >= min && datum.green <= max) && datum.green,
blue: (datum.blue >= min && datum.blue <= max) && datum.blue,
alpha: (datum.alpha >= min && datum.alpha <= max) && datum.alpha,
}
}
function isPaletteJSON(datum) {
const tests = {
palette: {
name: (typeof datum.name === 'string') && datum.name,
colors: (Array.isArray(datum.colors)) && datum.colors,
},
rgba: testPaletteJSONColor(datum, 0, 1),
rgbaInteger: testPaletteJSONColor(datum, 0, 255),
};
return {
isPalette: _.isEqual(datum, tests.palette),
isRGBA: _.isEqual(datum, tests.rgba),
isIntegerRGBA: _.isEqual(datum, tests.rgbaInteger),
}
}
async function loadOCO(identity) {
const ocoSource = await loader(identity.source, 'utf8');
return oco.parse(ocoSource)
}
async function loadJSON(identity) {
const json = await loader(identity.source, 'utf8');
const palette = JSON.parse(json);
if (isPaletteJSON(palette).isPalette) {
console.debug(`JSON Palette: ${palette.name}`);
return new oco.Entry(
identity.name,
[OCOValueEX.generateOCO(
palette.name,
palette.colors.map(color => {
const paletteColor = isPaletteJSON(color);
switch (true) {
case paletteColor.isRGBA:
console.debug(`JSON Color (RGBA): ${color.name}`);
return fromPrecise(color)
case paletteColor.isIntegerRGBA:
console.debug(`JSON Color (Integer RGBA): ${color.name}`);
return fromBytes(color)
default:
throw new Error(`${color.name}.json is not a valid JSON color object`)
}
}),
)],
)
}
throw new Error(`${identity.name}.json is not a valid palette`)
}
async function loadASE(identity) {
function scan(node) {
return node.map(datum => {
switch (datum.type) {
case 'color':
switch (datum.color.model) {
case 'Gray':
case 'RGB':
console.debug(`ASE Color (RGB/Grayscale): ${datum.name}`);
return new OCOValueEX(datum.color.hex, datum.name)
case 'CMYK':
console.debug(`ASE Color (CMYK): ${datum.name}`);
return new OCOValueEX({
cyan: datum.color.c,
magenta: datum.color.m,
yellow: datum.color.y,
black: datum.color.k,
}, datum.name)
case 'LAB':
console.debug(`ASE Color (Lab): ${datum.name}`);
return new OCOValueEX({
L: datum.color.lightness,
a: datum.color.a,
b: datum.color.b,
}, datum.name)
default:
throw new Error(`${datum.color.model} is not a valid ASE color model`)
}
case 'group':
console.debug(`ASE Group: ${datum.name}`);
return OCOValueEX.generateOCO(
datum.name,
scan(datum.entries),
)
default:
throw new Error(`${datum.type} is not a valid ASE data type`)
}
})
}
const aseSource = await loader(identity.source);
const palette = await ase.read(aseSource);
if (Array.isArray(palette)) {
return palette.length === 1 ? new oco.Entry(
identity.name,
scan(palette),
) : OCOValueEX.generateOCO(
identity.name,
scan(palette),
)
}
throw new Error(`${identity.name}.ase is not a valid palette`)
}
function selectLoaderByIndentity(type) {
switch (type) {
case 'sippalette':
return loadJSON
case 'json':
return loadJSON
case 'ase':
return loadASE
case 'oco':
return loadOCO
default:
throw new Error(`${type} is not recognised`)
}
}
class Reader {
constructor(source_) {
this.sourcePath = source_;
this.tree = new oco.Entry();
}
pick(key_) {
return key_ ? this.tree.get(key_) : this.tree.root()
}
transform(formats) {
this.tree.traverseTree('Color', color_ => {
const original = color_.get(0).identifiedValue.getOriginalInput();
color_.children = [];
for (const [index_, format] of formats.entries()) {
const newFormat = new OCOValueEX(original, color_.name);
newFormat._format = format;
color_.addChild(new oco.ColorValue(
format,
newFormat.toString(format),
newFormat,
), true, index_);
}
});
return this
}
async load(pathArray) {
await Promise.all(pathArray
.filter(file => file.match(fileFilter))
.map(file => createIdentity(this.sourcePath)(file))
.map(async identity => {
const entry = await selectLoaderByIndentity(identity.type)(identity);
entry.addMetadata({
'import/file/source': relative(process.cwd(), identity.source),
'import/file/type': identity.type,
});
this.tree.set(`${identity.path}${identity.name}`, entry);
}),
);
return this
}
render(path) {
return oco.render(this.pick(path))
}
}
const writeFile = promisify(fs.writeFile);
function writer(destination, contents) {
console.debug(`Writing file to ${destination}`);
return writeFile(destination, contents)
}
const console = createConsole({outStream: process.stderr});
function paletteReader(pathArray) {
return new Reader(pathArray)
}
function paletteWriter(destination, palette) {
return writer(destination, palette)
}
const clr = simple({format: 'sgr'});
const metadata = meta(dirname(fileURLToPath(import.meta.url)));
const renderer = truwrap({
outStream: process.stderr,
});
const colorReplacer = new TemplateTag(
replaceSubstitutionTransformer(
/([a-zA-Z]+?)[:/|](.+)/,
(match, colorName, content) => `${clr[colorName]}${content}${clr[colorName].out}`,
),
);
const title = box(colorReplacer`${'title|palette2oco'}${`dim| │ ${metadata.version(3)}`}`, {
borderColor: 'red',
margin: {
top: 1,
},
padding: {
bottom: 0,
top: 0,
left: 2,
right: 2,
},
});
const usage = stripIndent(colorReplacer)`
Convert palette data from a variety of sources into Open Color .oco format.
Allows structured directories of pallette data to be converted into nested oco palette data.
Formats supported:
Sip (http://sipapp.io): Supports .sippalette and .json exports.
Abobe Swatch Exchange (ASE): Full support of RGB, CMYK and Lab colorspaces.
Vanilla JSON: File signature must match the following...
{
"name" : "Palette name",
"colors" : [
{
name: "Color name",
red: (0.0 - 1.0 | 0 - 255)
green: (0.0 - 1.0 | 0 - 255)
blue: (0.0 - 1.0 | 0 - 255)
alpha: (0.0 - 1.0 | 0 - 255)
}
...
]
}
Usage:
${'command|palette2oco'} ${'option|[options]'} ${'argument|sourceGlob'} ${'argument|outputFile'}`;
const epilogue = colorReplacer`${'brightGreen|' + metadata.copyright} ${'grey|Released under the MIT License.'}`;
const yargsInstance = yargs(hideBin(process.argv))
.strictOptions()
.help(false)
.version(false)
.options({
h: {
alias: 'help',
describe: 'Display help.',
},
v: {
alias: 'version',
count: true,
describe: 'Print version to stdout. -vv Print name & version.',
},
V: {
alias: 'verbose',
count: true,
describe: 'Be verbose. -VV Be loquacious.',
},
o: {
alias: 'stdout',
type: 'boolean',
describe: 'Print output to stdout.',
},
color: {
describe: 'Force color output. Disable with --no-color',
},
});
const {argv} = yargsInstance;
if (!(process.env.USER === 'root' && process.env.SUDO_USER !== process.env.USER)) {
updateNotifier({pkg}).notify();
}
if (argv.help) {
(async () => {
const usageContent = await yargsInstance.wrap(renderer.getWidth()).getHelp();
renderer.write(title).break(2);
renderer.write(usage);
renderer.break(2);
renderer.write(usageContent);
renderer.break();
renderer.write(epilogue);
renderer.break(1);
process.exit(0);
})();
}
if (argv.version) {
process.stdout.write(metadata.version(argv.version));
process.exit(0);
}
if (argv.verbose) {
switch (argv.verbose) {
case 1:
console.verbosity(4);
console.log(`${clr.title}Verbose mode${clr.title.out}:`);
break
case 2:
console.verbosity(5);
console.log(`${clr.title}Extra-Verbose mode${clr.title.out}:`);
console.yargs(argv);
break
default:
console.verbosity(3);
}
}
async function processor(paths) {
const root = resolve();
const pathArray = await globby(paths);
const pal = await paletteReader(root).load(pathArray);
return pal.render()
}
(async () => {
console.log(argv._.length);
if (argv.stdout) {
if (argv._.length > 0) {
try {
process.stdout.write(await processor(argv._));
} catch (error) {
console.error(error);
process.exit(1);
}
} else {
console.error('palette2oco needs at least a source.');
process.exit(1);
}
} else if (argv._.length > 1) {
try {
const dest = resolve(_.tail(argv._)[0]);
paletteWriter(dest, await processor(_.initial(argv._)));
} catch (error) {
console.error(error);
process.exit(1);
}
} else {
console.error('palette2oco needs at least a source and a destination.');
process.exit(1);
}
})();