-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageOperations.js
132 lines (123 loc) · 4.08 KB
/
imageOperations.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var refPal = undefined, newPal = undefined;
var filename = undefined;
function updatePaletteImg(pal, btnName) {
let canvas = document.querySelector('#color-canvas');
let ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
let btn = document.querySelector('#' + btnName);
for (let i = 0; i < pal.length; i++) {
let c = hexToRgb(pal[i]);
Object.keys(c).forEach(function(key) {
c[key] = parseFloat(c[key]) * 0.5 | 0;
});
c = rgbToHex(c);
ctx.fillStyle = c;
ctx.fillRect(i * btn.offsetWidth / pal.length, 0, btn.offsetWidth / pal.length, btn.offsetHeight);
}
btn.style.background = "url(" + canvas.toDataURL() + ") no-repeat";
}
function applyPalette() {
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext("2d");
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imgData.data;
newColors = []
for(let i = 0; i < data.length; i += 4) {
if (data[i + 3] == 0) continue;
let c = {
r: data[i],
g: data[i + 1],
b: data[i + 2]
};
let index = refPal.indexOf(rgbToHex(c));
console.log(index, refPal[index], newPal[index]);
let newColor = newPal[index];
if (newColor == undefined) continue;
newColors.push(
{
color: newColor,
pos: {
x: i / 4 % canvas.width | 0,
y: i / 4 / canvas.width | 0
}
}
);
}
newColors.forEach(color => {
ctx.fillStyle = color.color;
ctx.fillRect(color.pos.x, color.pos.y, 1, 1);
})
loadImg(document.querySelector('#image').getContext('2d'), canvas.toDataURL(), true);
}
function extractPalette() {
var colors = new Set();
var canvas = document.querySelector('#canvas');
ctx = canvas.getContext("2d");
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imgData.data;
for(let i = 0; i < data.length; i += 4) {
if (data[i + 3] == 0) continue;
var c = {
r: data[i],
g: data[i + 1],
b: data[i + 2]
};
colors.add(rgbToHex(c));
}
colors = Array.from(colors);
colors.sort((a, b) => {
let A = hexToRgb(a);
let B = hexToRgb(b);
A = (A.r + A.g + A.b) / 3;
B = (B.r + B.g + B.b) / 3;
return A - B;
});
var pals = (colors.length / 10 | 0) + (colors.length % 10 == 0 ? 1 : 0);
if (Object.keys(p_dict).length + pals >= 5) return;
for (var i = 0; i < colors.length; i++) {
if (i == 0 && (Object.keys(p_dict).length == 0 || p_dict[palName].length != 0)) createPalette();
if (i % 10 == 0 && i != 0) {
drawPalette();
createPalette();
}
p_dict[palName].push(colors[i]);
}
drawPalette();
}
async function loadImage(e) {
context ??= document.querySelector('#image').getContext('2d');
opContext ??= document.querySelector('#canvas').getContext('2d');
let input = document.createElement('input');
input.type = 'file';
input.onchange = _ => {
let file = Array.from(input.files)[0];
if (!isFileImage(file)) return;
filename = file['name'];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function() {
image = reader.result;
loadImg(opContext, image, false);
loadImg(context, image, true);
}
};
input.click();
}
async function saveImage(e) {
if (filename == undefined) return;
const handle = await showSaveFilePicker({
suggestedName: filename,
types: [{
description: 'PNG',
accept: {'image/png': ['.png']},
}],
});
const blob = await fetch(
canvas.toDataURL("image/png")
).then((response) =>
response.blob()
);
const writableStream = await handle.createWritable();
await writableStream.write(blob);
await writableStream.close();
}