-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-info.js
111 lines (91 loc) · 3.34 KB
/
extract-info.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
import fg from 'fast-glob';
import fs from 'fs';
import { resolve } from 'path';
import Jimp from 'jimp';
const cwd = new URL('.', import.meta.url).pathname;
const getPatternInfo = pattern => {
const fullPath = resolve(pattern, 'info.json');
try {
const source = fs.readFileSync(fullPath).toString();
const {name, designer, colors, thumbnail} = JSON.parse(source)
return {
name,
designer,
thumbnail,
folder: pattern.replace('public/patterns/',''),
colorAmount:colors.length
}
} catch (error) {
console.error(`😳 Error: File ${fullPath} not found`);
}
}
const buildPatterns = async () => {
const patterns = await fg('public/patterns/*', { cwd, onlyDirectories: true });
const extracted = patterns.map(pattern => getPatternInfo(pattern)).filter(p => !!p);
console.log(`Extracted info for ${extracted.length} patterns`);
fs.writeFileSync('public/patterns.json', `${JSON.stringify({ patterns:extracted}, null, 2)}\n`);
};
const getYarnInfo = async yarn => {
const fullPath = resolve(yarn, 'info.json');
const images = await fg(resolve(`${yarn}/images/*`), { cwd });
const colors = await Promise.all(images.map(img => readColor(img, yarn.replace('public/yarns/',''))));
fs.writeFileSync(`${yarn}/colors.json`, `${JSON.stringify({ colors}, null, 2)}\n`);
try {
const source = fs.readFileSync(fullPath).toString();
const {company, name, weight, palette} = JSON.parse(source)
return {
company,
name: name || yarn.replace('public/yarns/',''),
weight,
folder: yarn.replace('public/yarns/',''),
color:palette.length
}
} catch (error) {
console.error(`😳 Error: File ${fullPath} not found`);
}
}
const readColor = (image, yarn) => {
return Jimp.read(image).then(img => {
const pxs = 4
const w = img.bitmap.width;
const h = img.bitmap.height;
img.pixelate(w/pxs);
const nrOfPixels = pxs*(h/(w/pxs));
const values = [];
for (let index = 1; index < nrOfPixels; index++) {
const x = Math.floor(index/pxs)*(w/pxs);
const y = index%pxs*(w/pxs);
values.push(Jimp.intToRGBA(img.getPixelColor(x, y)));
}
const r = Math.floor(values.reduce((n, {r}) => n + r, 0)/nrOfPixels);
const g = Math.floor(values.reduce((n, {g}) => n + g, 0)/nrOfPixels);
const b = Math.floor(values.reduce((n, {b}) => n + b, 0)/nrOfPixels);
// console.log(yarn);
return {
image: getFilePath(image),
name: getPrettyName(image),
color: `rgb(${r},${g},${b})`,
yarn: yarn
};
});
}
const getFilePath = path => {
const parts = path.split('/');
const start = parts.indexOf('yarns');
return '/'+parts.slice(start).join('/');
}
const getPrettyName = path => {
const parts = path.split('/');
return parts[parts.length-1].split('.')[0].replace('-',' ').replace('_',' ');
}
const buildYarns = async () => {
const yarns = await fg('public/yarns/*', { cwd, onlyDirectories: true });
const extracted = await Promise.all(yarns.map(yarn => getYarnInfo(yarn)).filter(p => !!p));
console.log(`Extracted info for ${extracted.length} yarns`);
fs.writeFileSync('public/yarns.json', `${JSON.stringify({ yarns:extracted}, null, 2)}\n`);
};
buildPatterns();
buildYarns();
// await getYarnInfo('public/yarns/rosarios4-terra');
// to read colors from images:
// https://www.npmjs.com/package/jimp