Commit 78f9553b authored by Matija Obreza's avatar Matija Obreza
Browse files

Flood fill with color

parent 6b022f9f
......@@ -797,8 +797,9 @@ export function filterOutline(image) {
* @param {number} startX startAt
* @param {number} startY startAt
* @param {number} color Channel
* @param {number} value Color value
*/
export function floodFill(data, w, h, startX, startY, color) {
export function floodFill(data, w, h, startX, startY, color, value: number = 255) {
const P = (x, y, color) => {
if (x < 0 || x >= w || y < 0 || y >= h) {
return -1;
......@@ -832,18 +833,18 @@ export function floodFill(data, w, h, startX, startY, color) {
while ((point = stack.shift())) {
// console.log('Next start at', point);
const x = point[0], y = point[1];
if (data[P(x, y, color)] !== 0) {
if (data[P(x, y, color)] === value) {
continue;
}
data[P(x, y, color)] = 255;
data[P(x, y, color)] = value;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === j || i === -j) {
continue;
continue; // ignore diagonal pixels: X
}
const p = P(x - j, y - i, color);
if (p >= 0) {
if (data[p] === 0) {
if (data[p] !== value) {
stack.push([ x - j, y - i]);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment