var draw = document.getElementById("canvas").getContext("2d");
var data = document.getElementById("data").getContext("2d");
colors = [
[0, 0, 0],
[255, 255, 255],
[192, 192, 192],
[128, 128, 128],
[126, 3, 8],
[252, 13, 27],
[255, 253, 56],
[128, 127, 23],
[15, 127, 18],
[41, 253, 46],
[45, 255, 254],
[17, 128, 127],
[2, 12, 126],
[11, 36, 251],
[252, 40, 252],
[127, 15, 126],
[128, 127, 68],
[255, 253, 136],
[42, 253, 133],
[4, 64, 64],
[23, 131, 251],
[133, 255, 254],
[129, 132, 252],
[6, 66, 126],
[127, 37, 251],
[127, 64, 13],
[253, 128, 73],
[252, 22, 129]
];
iteration = 0;
fails = 0;
success = 0;
x = 0;
y = 0;
//Init when the Go! button is pressed
document.getElementById("file").onchange = function (event) {
document.getElementById("img").src = URL.createObjectURL(event.target.files[0]);
filename = document.getElementById("file").value;
/*if (getCookie("orginal") == filename) {
console.log("Loading from Cookie");
reload = true;
document.getElementById("reload").src = getCookie("picture");
}*/
};
/*function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}*/
//Run when the image has been loaded into memory
document.getElementById("img").onload = function () {
document.getElementById("file").disable = "true";
document.getElementById("canvas").hidden = "";
document.getElementById("canvas").height = document.getElementById("img").height;
document.getElementById("data").height = document.getElementById("img").height;
document.getElementById("canvas").width = document.getElementById("img").width;
document.getElementById("data").width = document.getElementById("img").width;
var imgData = draw.createImageData(document.getElementById("img").width, document.getElementById("img").height);
for (var i = 0; i < imgData.data.length; i += 4) {
imgData.data[i + 0] = 0;
imgData.data[i + 1] = 0;
imgData.data[i + 2] = 0;
imgData.data[i + 3] = 255;
}
draw.putImageData(imgData, 0, 0);
data.putImageData(imgData, 0, 0);
if (reload == true) {
draw.drawImage(document.getElementById("reload"), 0, 0);
}
data.drawImage(document.getElementById("img"), 0, 0);
setInterval(function () {
for (var u = 0; u < document.getElementById("selectColor").value; u++) {
doThing();
}
}, 0);
};
//The core function. Every time this function is called, is checks/adds a dot.
function doThing() {
getCoords();
paintBucket();
console.count("Iteration");
if (compare(x, y)) {
draw.putImageData(imgData, x, y);
}
}
function getCoords() {
switch (document.getElementById("selectCord").value) {
case "1":
x = Math.floor(Math.random() * (document.getElementById("img").width + 4));
y = Math.floor(Math.random() * (document.getElementById("img").height + 4));
break;
case "2":
x = Math.floor(Math.random() * ((document.getElementById("img").width + 4) / 4)) * 4;
console.log(x);
x += parseInt(document.getElementById("allignX").value);
console.log(x);
y = Math.floor(Math.random() * ((document.getElementById("img").height + 4) / 4)) * 4;
y += parseInt(document.getElementById("allignY").value);
break;
case "3":
x += parseInt(document.getElementById("loopX").value);
if (x > document.getElementById("img").width + 5) {
x = parseInt(document.getElementById("allignX").value);
y += parseInt(document.getElementById("loopY").value);
}
if (y > document.getElementById("img").height + 5) {
y = parseInt(document.getElementById("allignY").value);
}
}
}
function compare(arg1, arg2) {
var arg3 = arg1 + 4;
var arg4 = arg2 + 4;
imgData2 = data.getImageData(arg1, arg2, 4, 4);
imgData3 = draw.getImageData(arg1, arg2, 4, 4);
N = 0;
O = 0;
i = 4;
addCompare();
addCompare();
i += 4;
for (l = 0; l < 8; l++) {
addCompare();
}
i += 4;
addCompare();
addCompare();
i += 4;
//console.log("New Score: " + N + " Old Score: " + O);
iteration++;
/*if(iteration>=1000){
document.cookie="orginal="+filename;
document.cookie="picture length="+document.getElementById("canvas").toDataURL().length;
document.cookie="picture="+document.getElementById("canvas").toDataURL();
}*/
if (N < O) {
return true;
} else {
return false;
}
}
function addCompare() {
if (document.getElementById("colorDif").value == "HSL") {
HSLCompare();
i += 4;
return;
}
if (document.getElementById("colorDif").value == "HSV") {
HSVCompare();
i += 4;
return;
}
N += Math.abs(imgData.data[i] - imgData2.data[i]);
N += Math.abs(imgData.data[i + 1] - imgData2.data[i + 1]);
N += Math.abs(imgData.data[i + 2] - imgData2.data[i + 2]);
O += Math.abs(imgData3.data[i] - imgData2.data[i]);
O += Math.abs(imgData3.data[i + 1] - imgData2.data[i + 1]);
O += Math.abs(imgData3.data[i + 2] - imgData2.data[i + 2]);
i += 4;
}
function HSVCompare() {
var NewHue = rgbToHsv(imgData.data[i], imgData.data[i + 1], imgData.data[i + 2])[0];
var PicHue = rgbToHsv(imgData2.data[i], imgData2.data[i + 1], imgData2.data[i + 2])[0];
var OldHue = rgbToHsv(imgData3.data[i], imgData3.data[i + 1], imgData3.data[i + 2])[0];
var NScore = [Math.abs(NewHue - PicHue), ((NewHue < PicHue) ? NewHue + (1 - PicHue) : PicHue + (1 - NewHue))];
var OScore = [Math.abs(OldHue - PicHue), ((OldHue < PicHue) ? OldHue + (1 - PicHue) : PicHue + (1 - OldHue))];
NScore = Math.min(NScore[0], NScore[1]);
OScore = Math.min(OScore[0], OScore[1]);
NewHue = rgbToHsv(imgData.data[i], imgData.data[i + 1], imgData.data[i + 2])[1];
PicHue = rgbToHsv(imgData2.data[i], imgData2.data[i + 1], imgData2.data[i + 2])[1];
OldHue = rgbToHsv(imgData3.data[i], imgData3.data[i + 1], imgData3.data[i + 2])[1];
NScore += Math.abs(NewHue-PicHue);
OScore += Math.abs(OldHue-PicHue);
NewHue = rgbToHsv(imgData.data[i], imgData.data[i + 1], imgData.data[i + 2])[2];
PicHue = rgbToHsv(imgData2.data[i], imgData2.data[i + 1], imgData2.data[i + 2])[2];
OldHue = rgbToHsv(imgData3.data[i], imgData3.data[i + 1], imgData3.data[i + 2])[2];
N += Math.abs(NewHue-PicHue) + NScore;
O += Math.abs(OldHue-PicHue) + OScore;
}
function rgbToHsv(r, g, b){
r = r/255, g = g/255, b = b/255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, v = max;
var d = max - min;
s = max == 0 ? 0 : d / max;
if(max == min){
h = 0; // achromatic
}else{
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, v];
}
function HSLCompare() {
var result = 0;
rgb = false;
var NewHue = rgbToHue(imgData.data[i], imgData.data[i + 1], imgData.data[i + 2])[0];
var PicHue = rgbToHue(imgData2.data[i], imgData2.data[i + 1], imgData2.data[i + 2])[0];
var OldHue = rgbToHue(imgData3.data[i], imgData3.data[i + 1], imgData3.data[i + 2])[0];
if (rgb == true) {
N += Math.abs(imgData.data[i] - imgData2.data[i]);
N += Math.abs(imgData.data[i + 1] - imgData2.data[i + 1]);
N += Math.abs(imgData.data[i + 2] - imgData2.data[i + 2]);
O += Math.abs(imgData3.data[i] - imgData2.data[i]);
O += Math.abs(imgData3.data[i + 1] - imgData2.data[i + 1]);
O += Math.abs(imgData3.data[i + 2] - imgData2.data[i + 2]);
return;
}
var NScore = [Math.abs(NewHue - PicHue), ((NewHue < PicHue) ? NewHue + (1 - PicHue) : PicHue + (1 - NewHue))];
var OScore = [Math.abs(OldHue - PicHue), ((OldHue < PicHue) ? OldHue + (1 - PicHue) : PicHue + (1 - OldHue))];
NScore = Math.min(NScore[0], NScore[1]);
OScore = Math.min(OScore[0], OScore[1]);
NewHue = rgbToHue(imgData.data[i], imgData.data[i + 1], imgData.data[i + 2])[1];
PicHue = rgbToHue(imgData2.data[i], imgData2.data[i + 1], imgData2.data[i + 2])[1];
OldHue = rgbToHue(imgData3.data[i], imgData3.data[i + 1], imgData3.data[i + 2])[1];
NScore += Math.abs(NewHue-PicHue);
OScore += Math.abs(OldHue-PicHue);
NewHue = rgbToHue(imgData.data[i], imgData.data[i + 1], imgData.data[i + 2])[2];
PicHue = rgbToHue(imgData2.data[i], imgData2.data[i + 1], imgData2.data[i + 2])[2];
OldHue = rgbToHue(imgData3.data[i], imgData3.data[i + 1], imgData3.data[i + 2])[2];
N += Math.abs(NewHue-PicHue) + NScore;
O += Math.abs(OldHue-PicHue) + OScore;
}
function rgbToHue(r, g, b) {
if (Math.max(r, g, b) - Math.min(r, g, b) < 50) {
rgb = true
}
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b),
min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0; // achromatic
} else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
}
h /= 6;
}
return [h,s,l];
}
//Create a 4x4 ImageData object, random color selected from the colors var, transparent corners.
function paintBucket() {
color = Math.floor(Math.random() * 28);
imgData = draw.createImageData(4, 4);
imgData2 = draw.getImageData(x, y, 4, 4);
i = 0;
createCorn();
createColor();
createColor();
createCorn();
for (l = 0; l < 8; l++) {
createColor();
}
createCorn();
createColor();
createColor();
createCorn();
}
function createCorn() {
imgData.data[i] = imgData2.data[i];
imgData.data[i + 1] = imgData2.data[i + 1];
imgData.data[i + 2] = imgData2.data[i + 2];
imgData.data[i + 3] = 255;
i += 4;
}
function createColor() {
imgData.data[i] = colors[color][0];
imgData.data[i + 1] = colors[color][1];
imgData.data[i + 2] = colors[color][2];
imgData.data[i + 3] = 255;
i += 4;
}
<canvas id="canvas" hidden></canvas>
<br>
<canvas id="data" hidden></canvas>
<br>
<input type="file" id="file"></input>
<br>
<img id="img">
<img id="reload" hidden>
<p>Algorithms:</p>
<select id="selectCord">
<option value="1">Random</option>
<option value="2">Random Alligned</option>
<option value="3" selected>Loop</option>
</select>
<select id="selectColor">
<option value="2000">Super Speedy</option>
<option value="1000">Very Speedy</option>
<option value="500" selected>Speedy</option>
<option value="1">Visual</option>
</select>
<select id="colorDif">
<option value="RGB" selected>RGB</option>
<option value="HSL">HSL</option>
<option value="HSV">HSV</option>
</select>
<p>Algorithm Options:
<br>
</p>
<p>X Offset:
<input id="allignX" type="range" min="0" max="3" value="0"></input>
</p>
<p>Y Offset:
<input id="allignY" type="range" min="0" max="3" value="0"></input>
</p>
<p>Spacing X:
<input id="loopX" type="range" min="1" max="4" value="2"></input>
</p>
<p>Spacing Y:
<input id="loopY" type="range" min="1" max="4" value="2"></input>
</p>