-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex-multi-image-arranger.js
107 lines (81 loc) · 2.84 KB
/
hex-multi-image-arranger.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
"use strict";
import fs from 'fs-extra';
import path from 'path';
import colors from 'colors';
import * as Hex from './hex.js';
import * as color_convert from './color-convert.js';
import {writeImage, getOutputImage} from './image-loader.js';
export {
arrange,
}
async function arrange(settings, images, reverse_color_map, image_size) {
const max_image_size = Math.max(image_size.width, image_size.height);
const size = 0.52;
const max_hex_pos = {
x: size * Math.sqrt(3) * (image_size.width + 0.5 * (image_size.height & 1)),
y: size * 3/2 * image_size.height,
}
let centering_padding = {
x: (1.0 - max_hex_pos.x / max_image_size) / 2,
y: (1.0 - max_hex_pos.y / max_image_size) / 2,
}
const image_datas = images.map(image => {
const data = image.getContext('2d').getImageData(0, 0, image.width, image.height).data;
return {image, data}
});
var output = [];
let missing_color_sequences = [];
for (let x = 0; x < image_size.width; x++) {
for (let y = 0; y < image_size.height; y++) {
let colors = image_datas.map( image_data => {
let index = (x + y * image_data.image.width) * 4;
let rgb_obj = {
r: image_data.data[index+0],
g: image_data.data[index+1],
b: image_data.data[index+2],
a: image_data.data[index+3]
};
let argb = color_convert.objToARGB(rgb_obj);
return argb;
});
const color_keys = Object.keys(reverse_color_map)
.filter( key => {
let map_colors = reverse_color_map[key];
for (let i = 0; i < map_colors.length; i++){
if (colors[i] != map_colors[i]) return false;
}
return true;
});
if (color_keys.length == 0) {
// keep on going but fail later
missing_color_sequences.push(colors);
}
const color_key = color_keys[0] || 'NOT_FOUND';
const hex_pos = {
x: size * Math.sqrt(3) * (x + 0.5 * (y & 1)),
y: size * 3/2 * y,
}
const scaled_hex_pos = {
x: (0.5 + hex_pos.x) / max_image_size,
y: (0.5 + hex_pos.y) / max_image_size,
};
const scaled_hex_pos_with_padding = {
x: scaled_hex_pos.x + centering_padding.x,
y: scaled_hex_pos.y + centering_padding.y,
}
output.push({
x: scaled_hex_pos_with_padding.x,
y: scaled_hex_pos_with_padding.y,
pixel_colors: color_key,
})
}
}
if (missing_color_sequences.length !== 0) {
// make unique
const missing_color_sequences_string = missing_color_sequences.map( e => e.map(color_convert.toHexString).join(', '))
.filter( (e,i,a) => a.indexOf(e) === i)
.join('\n')
throw new Error(`Image contains color sequences not present in the color map:\n${missing_color_sequences_string}`);
}
return output;
}