-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (122 loc) · 5.07 KB
/
Copy pathscript.js
File metadata and controls
142 lines (122 loc) · 5.07 KB
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
const imageInput = document.getElementById('imageInput');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const pixelSizeInput = document.getElementById('pixelSize');
const greyscaleInput = document.getElementById('greyscale');
const maxWidthInput = document.getElementById('maxWidth');
const maxHeightInput = document.getElementById('maxHeight');
const usePaletteInput = document.getElementById('usePalette');
const paletteSelection = document.getElementById('paletteSelection');
const downloadButton = document.getElementById('downloadButton');
let img = new Image();
// Predefined color palettes
const colorPalettes = {
gameboy: ['#0F380F', '#306230', '#8BAC0F', '#9BBC0F'],
atari2600: ['#000000', '#FFFFFF', '#F01800', '#F0CC00', '#0018F0', '#78F0CC'],
nes: ['#7C7C7C', '#0000FC', '#940084', '#F80000', '#F8B800', '#00FC00', '#3CBCFC', '#FFFFFF'],
commodore64: ['#000000', '#FFFFFF', '#68372B', '#70A4B2', '#6F3D86', '#588D43', '#352879', '#B8C76F'],
bbcmicro: ['#000000', '#FFFFFF', '#FF0000', '#00FF00', '#FFFF00', '#0000FF', '#FF00FF', '#00FFFF'],
sinclairql: ['#000000', '#A1A1A1', '#A6CEE3', '#1F78B4', '#B2DF8A', '#33A02C', '#FB9A99', '#E31A1C'],
ti994a: ['#000000', '#FFFFFF', '#FF6600', '#FFAA00', '#AA5500', '#550000', '#FFFFAA', '#AAFFFF'],
vic20: ['#000000', '#FFFFFF', '#FF0000', '#A9F5A9', '#0000FF', '#FF00FF', '#FFFF00', '#00FFFF'],
zxspectrum: ['#000000', '#0000D7', '#D70000', '#D700D7', '#00D700', '#00D7D7', '#D7D700', '#D7D7D7']
};
imageInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
});
img.onload = () => {
handleCanvasResize();
drawPixelatedImage();
};
pixelSizeInput.addEventListener('input', drawPixelatedImage);
greyscaleInput.addEventListener('change', drawPixelatedImage);
maxWidthInput.addEventListener('input', handleCanvasResize);
maxHeightInput.addEventListener('input', handleCanvasResize);
usePaletteInput.addEventListener('change', (event) => {
paletteSelection.disabled = !event.target.checked;
drawPixelatedImage();
});
paletteSelection.addEventListener('change', drawPixelatedImage);
function handleCanvasResize() {
const maxWidth = parseInt(maxWidthInput.value, 10) || img.width;
const maxHeight = parseInt(maxHeightInput.value, 10) || img.height;
const aspectRatio = img.width / img.height;
if (img.width > maxWidth) {
canvas.width = maxWidth;
canvas.height = maxWidth / aspectRatio;
} else if (img.height > maxHeight) {
canvas.height = maxHeight;
canvas.width = maxHeight * aspectRatio;
} else {
canvas.width = img.width;
canvas.height = img.height;
}
drawPixelatedImage();
}
function drawPixelatedImage() {
const pixelSize = parseInt(pixelSizeInput.value, 10);
const greyscale = greyscaleInput.checked;
const usePalette = usePaletteInput.checked;
const selectedPalette = colorPalettes[paletteSelection.value];
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = canvas.width / pixelSize;
tempCanvas.height = canvas.height / pixelSize;
tempCtx.drawImage(img, 0, 0, tempCanvas.width, tempCanvas.height);
let imageData = tempCtx.getImageData(0, 0, tempCanvas.width, tempCanvas.height);
if (greyscale) {
for (let i = 0; i < imageData.data.length; i += 4) {
const avg = (imageData.data[i] + imageData.data[i + 1] + imageData.data[i + 2]) / 3;
imageData.data[i] = avg; // Red
imageData.data[i + 1] = avg; // Green
imageData.data[i + 2] = avg; // Blue
}
}
if (usePalette) {
for (let i = 0; i < imageData.data.length; i += 4) {
const [r, g, b] = [imageData.data[i], imageData.data[i + 1], imageData.data[i + 2]];
const nearestColor = getNearestColor([r, g, b], selectedPalette);
imageData.data[i] = nearestColor[0]; // Red
imageData.data[i + 1] = nearestColor[1]; // Green
imageData.data[i + 2] = nearestColor[2]; // Blue
}
}
tempCtx.putImageData(imageData, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(tempCanvas, 0, 0, tempCanvas.width, tempCanvas.height, 0, 0, canvas.width, canvas.height);
}
function getNearestColor(color, palette) {
let nearest = palette[0];
let minDistance = Infinity;
for (const hex of palette) {
const [pr, pg, pb] = hexToRgb(hex);
const distance = Math.sqrt(
Math.pow(color[0] - pr, 2) +
Math.pow(color[1] - pg, 2) +
Math.pow(color[2] - pb, 2)
);
if (distance < minDistance) {
minDistance = distance;
nearest = hex;
}
}
return hexToRgb(nearest);
}
function hexToRgb(hex) {
const bigint = parseInt(hex.slice(1), 16);
return [(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255];
}
downloadButton.addEventListener('click', () => {
const link = document.createElement('a');
link.download = 'pixelated-image.png';
link.href = canvas.toDataURL();
link.click();
});