-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (97 loc) · 4.03 KB
/
script.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
document.getElementById('uploadButton').addEventListener('click', async () => {
const fileInput = document.getElementById('imageUpload');
const pixelSize = document.getElementById('pixelSize').value;
const file = fileInput.files[0];
const formData = new FormData();
formData.append('image', file);
formData.append('pixel_size', pixelSize);
// Show the loader
document.getElementById('loader').style.display = 'block';
try {
const response = await fetch('https://bunny-backend-ce104ef992fd.herokuapp.com/process_image', {
method: 'POST',
body: formData,
});
const result = await response.json();
// const colors = result.colors;
const imgBase64 = result.img_base64;
document.getElementById('pixelatedImage').src =
'data:image/png;base64,' + imgBase64;
const colorGrid = document.getElementById('colorGrid');
const rowLabels = document.getElementById('rowLabels');
const colorList = document.getElementById('colorList');
colorGrid.innerHTML = ''; // Clear previous colors
rowLabels.innerHTML = ''; // Clear previous row labels
colorList.innerHTML = ''; // Clear previous color list
// Determine grid dimensions
const cellSize = 10; // Default size
const pixelData = result.pixel_data;
const numColumns = pixelData[0].length;
const numRows = pixelData.length;
document.getElementById(
'summary'
).innerHTML = `<h3>Rows: ${numRows} <br/> Columns: ${numColumns} </h3>`;
// Set grid size
colorGrid.style.gridTemplateColumns = `repeat(${numColumns}, ${cellSize}px)`;
colorGrid.style.gridTemplateRows = `repeat(${numRows}, ${cellSize}px)`;
// Set row labels
for (let i = 0; i < numRows; i++) {
const rowLabel = document.createElement('span');
rowLabel.textContent = i;
rowLabels.appendChild(rowLabel);
}
// Populate the color grid and sidebar list
function rgbToHex(r, g, b) {
const componentToHex = (c) => c.toString(16).padStart(2, '0');
return `#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`;
}
pixelData.forEach((row, rowIndex) => {
row.forEach((color, colIndex) => {
const [r, g, b] = color;
const colorsHex = rgbToHex(r, g, b);
const colorName = ntc.name(colorsHex)[1];
// Create color box in grid with tooltip
const colorBox = document.createElement('div');
colorBox.className = 'color-box';
colorBox.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
colorBox.style.width = `${cellSize}px`;
colorBox.style.height = `${cellSize}px`;
const tooltip = document.createElement('span');
tooltip.className = 'tooltiptext';
tooltip.textContent = `${colorName} (Row: ${rowIndex}, Col: ${colIndex})`;
colorBox.appendChild(tooltip);
colorGrid.appendChild(colorBox);
// Create color list item
const listItem = document.createElement('li');
listItem.textContent = colorName;
listItem.dataset.row = rowIndex;
listItem.dataset.column = colIndex;
listItem.classList.add('color-list-item');
listItem.addEventListener('mouseover', () => {
listItem.textContent = `${colorName} (Row: ${rowIndex}, Col: ${colIndex})`;
document.getElementById(
'patternOutput'
).innerHTML = `<h3>${tooltip.textContent}</h3>`;
});
listItem.addEventListener('mouseout', () => {
listItem.textContent = colorName;
});
listItem.addEventListener('click', () => {
document
.querySelectorAll('.color-list-item')
.forEach((item) => item.classList.remove('selected'));
listItem.classList.add('selected');
document.getElementById(
'patternOutput'
).innerHTML = `<h2>${tooltip.textContent}</h2>`;
});
colorList.appendChild(listItem);
});
});
} catch (error) {
console.error('Error:', error);
} finally {
// Hide the loader
document.getElementById('loader').style.display = 'none';
}
});