-
Notifications
You must be signed in to change notification settings - Fork 0
/
createMediaData.mjs
executable file
·63 lines (54 loc) · 1.75 KB
/
createMediaData.mjs
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
import Vibrant from 'node-vibrant';
import fs from 'node:fs/promises';
const imageDirectory = 'public/images';
async function getColors() {
try {
const startImage = `${imageDirectory}/start.png`;
const vibrantData = await Vibrant.from(startImage).quality(1).getPalette();
const vibrantYiq =
(vibrantData.Vibrant.rgb[0] * 299 + vibrantData.Vibrant.rgb[1] * 587 + vibrantData.Vibrant.rgb[2] * 114) / 1000;
return {
primaryColor: vibrantData.Vibrant.hex,
secondaryColor: vibrantData.Muted.hex,
backgroundColor: vibrantData.LightVibrant.hex,
textColor: vibrantYiq < 150 ? '#fff' : '#000',
errorColor: vibrantData.DarkVibrant.hex,
infoColor: vibrantData.LightMuted.hex,
successColor: vibrantData.DarkMuted.hex,
};
} catch (err) {
console.log(err);
}
}
async function getImages() {
const files = await fs.readdir(imageDirectory);
try {
const images = files
.filter((file) => file.endsWith('.png'))
.map(async (file) => {
const fileName = file.substr(0, file.lastIndexOf('.'));
return {
name: fileName,
shortName: fileName,
source: fileName,
description: '',
link: '',
wiki: '',
};
});
return await Promise.all(images);
} catch (err) {
console.log(err);
}
}
export async function createData() {
const mediaData = {};
mediaData.isMaaS = true;
mediaData.elementSuffix = '.png';
mediaData.colors = await getColors();
mediaData.images = await getImages();
const mediaDataFile = `export const mediaData = ${JSON.stringify(mediaData, null, 2)};`;
const mediaDataFilePath = 'public/mediaData.js';
await fs.writeFile(mediaDataFilePath, mediaDataFile, 'utf-8');
}
createData();