2021-07-22 20:26:40 +03:00
|
|
|
// REFACTOR: fill tool onMouseDown
|
2019-03-27 02:20:54 +03:00
|
|
|
function fill(cursorLocation) {
|
2020-04-04 10:41:56 +03:00
|
|
|
|
|
|
|
//changes a pixels color
|
|
|
|
function colorPixel(tempImage, pixelPos, fillColor) {
|
|
|
|
//console.log('colorPixel:',pixelPos);
|
|
|
|
tempImage.data[pixelPos] = fillColor.r;
|
|
|
|
tempImage.data[pixelPos + 1] = fillColor.g;
|
|
|
|
tempImage.data[pixelPos + 2] = fillColor.b;
|
|
|
|
tempImage.data[pixelPos + 3] = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
//change x y to color value passed from the function and use that as the original color
|
|
|
|
function matchStartColor(tempImage, pixelPos, color) {
|
|
|
|
//console.log('matchPixel:',x,y)
|
|
|
|
|
|
|
|
var r = tempImage.data[pixelPos];
|
|
|
|
var g = tempImage.data[pixelPos + 1];
|
|
|
|
var b = tempImage.data[pixelPos + 2];
|
2021-06-28 23:03:57 +03:00
|
|
|
var a = tempImage.data[pixelPos + 3];
|
2020-04-04 10:41:56 +03:00
|
|
|
//console.log(r == color[0] && g == color[1] && b == color[2]);
|
2021-06-28 23:03:57 +03:00
|
|
|
return (r == color[0] && g == color[1] && b == color[2] && a == color[3]);
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//save history state
|
2021-07-15 18:10:07 +03:00
|
|
|
new HistoryState().EditCanvas();
|
2020-04-04 10:41:56 +03:00
|
|
|
//saveHistoryState({type: 'canvas', canvas: context.getImageData(0, 0, canvasSize[0], canvasSize[1])});
|
|
|
|
//console.log('filling at '+ Math.floor(cursorLocation[0]/zoom) + ','+ Math.floor(cursorLocation[1]/zoom));
|
|
|
|
|
|
|
|
//temporary image holds the data while we change it
|
|
|
|
var tempImage = currentLayer.context.getImageData(0, 0, canvasSize[0], canvasSize[1]);
|
|
|
|
|
|
|
|
//this is an array that holds all of the pixels at the top of the cluster
|
|
|
|
var topmostPixelsArray = [[Math.floor(cursorLocation[0]/zoom), Math.floor(cursorLocation[1]/zoom)]];
|
|
|
|
//console.log('topmostPixelsArray:',topmostPixelsArray)
|
|
|
|
|
|
|
|
//the offset of the pixel in the temp image data to start with
|
|
|
|
var startingPosition = (topmostPixelsArray[0][1] * canvasSize[0] + topmostPixelsArray[0][0]) * 4;
|
|
|
|
|
|
|
|
//the color of the cluster that is being filled
|
2021-06-28 23:03:57 +03:00
|
|
|
var clusterColor = [tempImage.data[startingPosition],tempImage.data[startingPosition+1],tempImage.data[startingPosition+2], tempImage.data[startingPosition+3]];
|
2020-04-04 10:41:56 +03:00
|
|
|
|
|
|
|
//the new color to fill with
|
2021-07-19 03:52:43 +03:00
|
|
|
var fillColor = Color.hexToRgb(currentLayer.context.fillStyle);
|
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
//if you try to fill with the same color that's already there, exit the function
|
|
|
|
if (clusterColor[0] == fillColor.r &&
|
|
|
|
clusterColor[1] == fillColor.g &&
|
2021-06-28 23:03:57 +03:00
|
|
|
clusterColor[2] == fillColor.b &&
|
2021-06-30 23:35:46 +03:00
|
|
|
clusterColor[3] != 0) {
|
|
|
|
console.log("Returned");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
|
|
|
|
//loop until there are no more values left in this array
|
|
|
|
while (topmostPixelsArray.length) {
|
|
|
|
var reachLeft, reachRight;
|
|
|
|
|
|
|
|
//move the most recent pixel from the array and set it as our current working pixels
|
|
|
|
var currentPixel = topmostPixelsArray.pop();
|
|
|
|
|
|
|
|
//set the values of this pixel to x/y variables just for readability
|
|
|
|
var x = currentPixel[0];
|
|
|
|
var y = currentPixel[1];
|
|
|
|
|
|
|
|
//this variable holds the index of where the starting values for the current pixel are in the data array
|
|
|
|
//we multiply the number of rows down (y) times the width of each row, then add x. at the end we multiply by 4 because
|
|
|
|
//each pixel has 4 values, rgba
|
|
|
|
var pixelPos = (y * canvasSize[0] + x) * 4;
|
|
|
|
|
|
|
|
//move up in the image until you reach the top or the pixel you hit was not the right color
|
|
|
|
while (y-- >= 0 && matchStartColor(tempImage, pixelPos, clusterColor)) {
|
|
|
|
pixelPos -= canvasSize[0] * 4;
|
|
|
|
}
|
|
|
|
pixelPos += canvasSize[0] * 4;
|
|
|
|
++y;
|
|
|
|
reachLeft = false;
|
|
|
|
reachRight = false;
|
|
|
|
while (y++ < canvasSize[1] - 1 && matchStartColor(tempImage, pixelPos, clusterColor)) {
|
|
|
|
colorPixel(tempImage, pixelPos, fillColor);
|
|
|
|
if (x > 0) {
|
|
|
|
if (matchStartColor(tempImage, pixelPos - 4, clusterColor)) {
|
|
|
|
if (!reachLeft) {
|
|
|
|
topmostPixelsArray.push([x - 1, y]);
|
|
|
|
reachLeft = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (reachLeft) {
|
|
|
|
reachLeft = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x < canvasSize[0] - 1) {
|
|
|
|
if (matchStartColor(tempImage, pixelPos + 4, clusterColor)) {
|
|
|
|
if (!reachRight) {
|
|
|
|
topmostPixelsArray.push([x + 1, y]);
|
|
|
|
reachRight = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (reachRight) {
|
|
|
|
reachRight = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pixelPos += canvasSize[0] * 4;
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 23:35:10 +03:00
|
|
|
currentLayer.context.putImageData(tempImage, 0, 0);
|
2020-04-04 10:41:56 +03:00
|
|
|
//console.log('done filling')
|
|
|
|
}
|