-
Notifications
You must be signed in to change notification settings - Fork 22
/
prepare.js
71 lines (62 loc) · 1.97 KB
/
prepare.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
const path = require('path')
const Jimp = require("jimp")
const fs = require('fs')
const blocksDb = require('./static/blocks_12.json')
const arr = []
const colors = []
function rgbToHsl(r, g, b){
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return {
h: Math.round(h * 360),
s: Math.round(s * 100),
l: Math.round(l * 100)
}
}
blocksDb.forEach((item, i) => {
item.id = i + 1
arr.push(Jimp.read('./static/textures/' + item.texture_image).then(function (image) {
let redSum = 0
let greenSum = 0
let blueSum = 0
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) {
redSum += this.bitmap.data[ idx + 0 ]
greenSum += this.bitmap.data[ idx + 1 ]
blueSum += this.bitmap.data[ idx + 2 ]
})
let red = Math.round(redSum / (image.bitmap.width * image.bitmap.height))
let green = Math.round(greenSum / (image.bitmap.width * image.bitmap.height))
let blue = Math.round(blueSum / (image.bitmap.width * image.bitmap.height))
const hsl = rgbToHsl(red, green, blue)
item.red = red
item.green = green
item.blue = blue
item.h = hsl.h
item.s = hsl.s
item.l = hsl.l
}))
})
Promise.all(arr).then(() => {
fs.writeFile(path.join(__dirname, './static/baked_blocks.json'), JSON.stringify(blocksDb), function(err) {
if(err) {
return console.log(err);
} else {
console.log('Completed!')
}
})
}, reject => {
console.log(reject)
})