-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
584 lines (500 loc) · 17.5 KB
/
index.html
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image to rms</title>
<link rel="stylesheet" href="base.min.css"/>
<link rel="stylesheet" href="choices.min.css"/>
<style>
html {
background-color: #333;
color: #eee;
font-family: sans-serif;
}
body {
padding-top: 1rem;
max-width: 1080px;
margin: 0 auto !important;
float: none !important;
}
.choices {
color: #333;
display: inline-block;
min-width: 500px;
}
ul {
list-style: none;
}
.row {
margin-bottom: 1rem;
}
#myCanvas {
margin: 40px 100px 40px 140px;
}
button {
padding: .5rem 1rem;
border: none;
outline: none;
color: #fff;
background: #111;
cursor: pointer;
position: relative;
z-index: 0;
border-radius: 10px;
}
button:before {
content: '';
background: linear-gradient(45deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000);
position: absolute;
top: -2px;
left: -2px;
background-size: 400%;
z-index: -1;
filter: blur(5px);
width: calc(100% + 4px);
height: calc(100% + 4px);
animation: glowing 20s linear infinite;
opacity: 0;
transition: opacity .3s ease-in-out;
border-radius: 10px;
}
button:active {
color: #000
}
button:active:after {
background: transparent;
}
button:hover:before {
opacity: 1;
}
button:after {
z-index: -1;
content: '';
position: absolute;
width: 100%;
height: 100%;
background: #111;
left: 0;
top: 0;
border-radius: 10px;
}
@keyframes glowing {
0% {
background-position: 0 0;
}
50% {
background-position: 400% 0;
}
100% {
background-position: 0 0;
}
}
/* https://loading.io/css/ */
.lds-roller {
display: inline-block;
position: relative;
width: 80px;
height: 80px;
}
.lds-roller div {
animation: lds-roller 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
transform-origin: 40px 40px;
}
.lds-roller div:after {
content: " ";
display: block;
position: absolute;
width: 7px;
height: 7px;
border-radius: 50%;
background: #fff;
margin: -4px 0 0 -4px;
}
.lds-roller div:nth-child(1) {
animation-delay: -0.036s;
}
.lds-roller div:nth-child(1):after {
top: 63px;
left: 63px;
}
.lds-roller div:nth-child(2) {
animation-delay: -0.072s;
}
.lds-roller div:nth-child(2):after {
top: 68px;
left: 56px;
}
.lds-roller div:nth-child(3) {
animation-delay: -0.108s;
}
.lds-roller div:nth-child(3):after {
top: 71px;
left: 48px;
}
.lds-roller div:nth-child(4) {
animation-delay: -0.144s;
}
.lds-roller div:nth-child(4):after {
top: 72px;
left: 40px;
}
.lds-roller div:nth-child(5) {
animation-delay: -0.18s;
}
.lds-roller div:nth-child(5):after {
top: 71px;
left: 32px;
}
.lds-roller div:nth-child(6) {
animation-delay: -0.216s;
}
.lds-roller div:nth-child(6):after {
top: 68px;
left: 24px;
}
.lds-roller div:nth-child(7) {
animation-delay: -0.252s;
}
.lds-roller div:nth-child(7):after {
top: 63px;
left: 17px;
}
.lds-roller div:nth-child(8) {
animation-delay: -0.288s;
}
.lds-roller div:nth-child(8):after {
top: 56px;
left: 12px;
}
@keyframes lds-roller {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.hidden {
display: none;
}
</style>
</head>
<body>
<h1><img src="SiegeEngineers.png" alt="[SE]"/> Image to rms</h1>
<div class="row">
<ol>
<li>Select the default terrain that shall be assigned to all colours of your image initially.</li>
<li>Load an image of your choice. Images with sizes other than 101x101 pixels might cause inconsistent colors due to
resizing.
</li>
<li>Select below which terrain shall be used for which colour. Choose <code>NONE</code> to exclude tiles from the
generated rms code.
</li>
<li>Use the download buttons to get a standalone script or just the snippet for the terrains.</li>
</ol>
</div>
<div class="row">
<label for="default-terrain">Default terrain</label>
<select id="default-terrain"></select>
</div>
<div class="row">
<input type="checkbox" id="use-default-terrain-for-all">
<label for="use-default-terrain-for-all" style="display: inline">Use default terrain for all colours
initially</label>
</div>
<div class="row">
<label for="image-input">Load image</label>
<input type="file" id="image-input"/>
</div>
<div class="row">
<canvas id="myCanvas" width="101" height="101"
style="border:1px solid #000000;transform: matrix(1.8,-1,1.8,1,0,0);">
</canvas>
</div>
<div class="row">
Download script as
<button onclick="downloadSnippet()">snippet</button>
or
<button onclick="downloadStandalone()">standalone</button>
</div>
<div class="lds-roller hidden" id="spinner">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div id="options"></div>
<hr>
<p>Made by <a href="https://aoe2.se" rel="author">Siege Engineers</a>. You can contribute
to this project on <a href="https://github.com/SiegeEngineers/image-to-rms" rel="nofollow">GitHub</a>.
</p>
<script src="choices.min.js"></script>
<script src="colour.js"></script>
<script type="text/javascript">
const defaultTerrainChoice = new Choices('#default-terrain', {items: []});
const colourChoices = [];
const colours = [];
let terrains = [];
let terraincolours = {};
const gatherTerrains = (terraincolours) => {
const terrains = []
for (let key in terraincolours) {
if (!terraincolours.hasOwnProperty(key)) {
return;
}
const rgbcolour1 = `rgb(${terraincolours[key][0][0]},${terraincolours[key][0][1]},${terraincolours[key][0][2]})`;
const rgbcolour2 = `rgb(${terraincolours[key][1][0]},${terraincolours[key][1][1]},${terraincolours[key][1][2]})`;
const rgbcolour3 = `rgb(${terraincolours[key][2][0]},${terraincolours[key][2][1]},${terraincolours[key][2][2]})`;
const colourstring = `<span style="color:${rgbcolour1};">⬤</span>\
<span style="color:${rgbcolour2};">⬤</span>\
<span style="color:${rgbcolour3};">⬤</span> ${key}`;
terrains.push({value: key, label: colourstring});
}
return terrains;
}
const TOP = (baseTerrain, playerLandTerrain) => `/* ****************************************************** */
<PLAYER_SETUP>
random_placement
/* ****************************************************** */
<LAND_GENERATION>
base_terrain ${baseTerrain}
create_player_lands
{
terrain_type ${playerLandTerrain}
border_fuzziness 1
land_percent 1
base_size 1
}
/* ****************************************************** */
`;
const BOTTOM = `
/* ****************************************************** */
<OBJECTS_GENERATION>
create_object TOWN_CENTER
{
set_place_for_every_player
group_placement_radius 1
min_distance_to_players 0
max_distance_to_players 0
}
create_object VILLAGER
{
set_place_for_every_player
group_placement_radius 3
min_distance_to_players 3
max_distance_to_players 5
}
if REGICIDE
create_object KING
{
set_place_for_every_player
min_distance_to_players 6
max_distance_to_players 7
}
endif
/* ****************************************************** */
`;
const TEMPLATE = (terrain, x, y) => `create_land
{
terrain_type ${terrain}
land_position ${x} ${y}
base_size 1
number_of_tiles 0
}
`;
const downloadSnippet = () => {
const snippet = getSnippet();
download('image-to-rms-snippet.rms', snippet);
}
const downloadStandalone = () => {
const defaultTerrain = defaultTerrainChoice.getValue(true);
const top = TOP(defaultTerrain, defaultTerrain);
const snippet = getSnippet();
download('image-to-rms-standalone.rms', top + snippet + BOTTOM);
}
const download = (filename, text) => {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
const getSnippet = () => {
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
const imgd = ctx.getImageData(0, 0, 101, 101);
const pix = imgd.data;
const dict = {};
let rms = '';
const rbgtoterrain = {};
for (let i = 0; i < colours.length; i++) {
const rgbcolour = colours[i];
rbgtoterrain[rgbcolour] = document.getElementById('colour-select-' + i).value;
}
for (let x = 0; x < 101; x++) {
for (let y = 0; y < 101; y++) {
const i = (y * 101 + x) * 4;
const r = pix[i];
const g = pix[i + 1];
const b = pix[i + 2];
const a = pix[i + 3];
const str = `${r}/${g}/${b}`;
if (!dict.hasOwnProperty(str)) {
dict[str] = 0;
}
dict[str]++;
const index = `${r}/${g}/${b}`;
const terrain = rbgtoterrain[index];
if (terrain !== 'NONE') {
rms += TEMPLATE(terrain, x, y);
}
}
}
return rms;
}
const getTerrainsWithClosestColour = (rgb) => {
const closestTerrains = [];
let currentDelta = 9999999;
for (let terrain in terraincolours) {
const terrainColour = terraincolours[terrain][1];
const imageColour = fromSlashString(rgb);
const delta = deltaErgb(terrainColour, imageColour);
if (delta === currentDelta) {
closestTerrains.push(terrain);
} else if (delta < currentDelta) {
currentDelta = delta;
closestTerrains.length = 0;
closestTerrains.push(terrain);
}
}
return closestTerrains;
}
fetch('terraincolours.json')
.then((result) => result.json())
.then((tcs) => {
terraincolours = tcs;
terrains = gatherTerrains(tcs);
defaultTerrainChoice.setChoices(terrains,
'value',
'label',
false,
);
defaultTerrainChoice.setChoiceByValue(['GRASS']);
});
const toSlashString = (r, g, b) => {
return `${r}/${g}/${b}`;
}
const fromSlashString = (str) => {
const items = str.split('/');
return [parseInt(items[0]), parseInt(items[1]), parseInt(items[2])];
}
const autocolour = (i) => {
const closestColours = getTerrainsWithClosestColour(colours[i]);
const choicesItem = colourChoices[i];
choicesItem.setChoiceByValue([closestColours[Math.floor(Math.random() * closestColours.length)]]);
}
const quantize = value => (value / 32 >> 0) * 32;
document.getElementById('image-input').onchange = ev => {
const file = ev.target.files[0];
if (file.type !== 'image/png') {
alert('Please select a png file!');
return;
}
document.getElementById('spinner').classList.remove('hidden');
const reader = new FileReader();
reader.onload = event => {
const img = new Image();
img.src = event.target.result;
img.onload = () => {
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.drawImage(img, 0, 0, 101, 101);
const imageData = ctx.getImageData(0, 0, 101, 101);
const pix = imageData.data;
let dict = {};
for (let x = 0; x < 101; x++) {
for (let y = 0; y < 101; y++) {
const i = (y * 101 + x) * 4;
const r = pix[i];
const g = pix[i + 1];
const b = pix[i + 2];
const a = pix[i + 3];
const str = toSlashString(r, g, b);
if (!dict.hasOwnProperty(str)) {
dict[str] = 0;
}
dict[str]++;
}
}
let colourCount = Object.keys(dict).length;
if (colourCount > 200) {
const result = confirm('You are trying to load an image with ' + colourCount + ' distinct colours. That\'s a lot. There are only ' + terrains.length + ' terrains available.\nWe will apply a naive colour reduction algorithm first.\nDo you want to continue with that?');
if (!result) {
document.getElementById('spinner').classList.add('hidden');
return;
}
dict = {};
for (let x = 0; x < 101; x++) {
for (let y = 0; y < 101; y++) {
const i = (y * 101 + x) * 4;
const r = imageData.data[i] = quantize(pix[i]);
const g = imageData.data[i + 1] = quantize(pix[i + 1]);
const b = imageData.data[i + 2] = quantize(pix[i + 2]);
const a = imageData.data[i + 3] = Math.ceil(pix[i + 3]);
const str = toSlashString(r, g, b);
if (!dict.hasOwnProperty(str)) {
dict[str] = 0;
}
dict[str]++;
}
}
ctx.putImageData(imageData, 0, 0, 0, 0, 101, 101);
alert(`${Object.keys(dict).length} unique colour values remaining`);
} else if (colourCount > terrains.length) {
alert('You are trying to load an image with ' + colourCount + ' distinct colours.\nThere are only ' + terrains.length + 'terrains available.\nSurely, you know what you are doing. GLHF!');
}
const options = document.getElementById('options');
options.innerHTML = '';
const sorted = Object.entries(dict).sort(([, a], [, b]) => b - a);
const ul = document.createElement('ul');
options.appendChild(ul);
colours.length = 0;
let index = 0;
for (let item of sorted) {
const colour = item[0];
colours.push(colour);
const rgbcolour = 'rgb(' + colour.replaceAll('/', ',') + ')';
const li = document.createElement('li');
li.innerHTML = `<span style="color:${rgbcolour};">⬤</span> ${colour}: ${item[1]} <select id="colour-select-${index}"></select> <button onclick="autocolour(${index})">Auto-Colour</button>`;
ul.appendChild(li);
index++;
}
const defaultTerrain = defaultTerrainChoice.getValue(true);
let choices = [{value: 'NONE', label: 'NONE'}, ...terrains];
colourChoices.length = 0;
for (let i = 0; i < colours.length; i++) {
const choicesItem = new Choices('#colour-select-' + i, {choices: choices});
choicesItem.setChoiceByValue([defaultTerrain]);
colourChoices.push(choicesItem);
}
if (!document.getElementById('use-default-terrain-for-all').checked) {
for (let i = 0; i < colours.length; i++) {
autocolour(i);
}
}
document.getElementById('spinner').classList.add('hidden');
}
}
reader.readAsDataURL(file);
}
</script>
</body>
</html>