<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">var t=0;

// Flood fill
function fill(runtime)
{
	var c = runtime.objects.DrawingCanvas.getFirstInstance();
	
	var col = {r: runtime.globalVars.r, g: runtime.globalVars.g, b: runtime.globalVars.b, a: 0xff}

	// tolerance value
	t = runtime.globalVars.tolerance;

	c.getImagePixelData().then(d=&gt; {
		floodFill(d, col, runtime.globalVars.x, runtime.globalVars.y);

		// load changed image back into the canvas
		c.loadImagePixelData(d);
		console.log(d.data[0]);
	})
}


function getColorAtPixel(imageData, x, y) {
  const {width, data} = imageData

  return {
    r: data[4 * (width * y + x) + 0],
    g: data[4 * (width * y + x) + 1],
    b: data[4 * (width * y + x) + 2],
    a: data[4 * (width * y + x) + 3]
  }
}

function setColorAtPixel(imageData, color, x, y) {
  const {width, data} = imageData

  data[4 * (width * y + x) + 0] = color.r &amp; 0xff
  data[4 * (width * y + x) + 1] = color.g &amp; 0xff
  data[4 * (width * y + x) + 2] = color.b &amp; 0xff
  data[4 * (width * y + x) + 3] = color.a &amp; 0xff
}

function colorMatch(a, b) {
  return (a.r&gt;=b.r-t &amp;&amp; a.r&lt;=b.r+t) &amp;&amp; (a.g&gt;=b.g-t &amp;&amp; a.g&lt;=b.g+t) &amp;&amp; (a.b&gt;=b.b-t &amp;&amp; a.b&lt;=b.b+t) &amp;&amp; (a.a&gt;=b.a-t &amp;&amp; a.a&lt;=b.a+t);
}

function floodFill(imageData, newColor, x, y) {
  const {width, height, data} = imageData
  const stack = []
  const baseColor = getColorAtPixel(imageData, x, y)
  let operator = {x, y}

  // Check if base color and new color are the same
  if (colorMatch(baseColor, newColor)) {
    return
  }

  // Add the clicked location to stack
  stack.push({x: operator.x, y: operator.y})

  while (stack.length) {
    operator = stack.pop()
    let contiguousDown = true // Vertical is assumed to be true
    let contiguousUp = true // Vertical is assumed to be true
    let contiguousLeft = false
    let contiguousRight = false

    // Move to top most contiguousDown pixel
    while (contiguousUp &amp;&amp; operator.y &gt;= 0) {
      operator.y--
      contiguousUp = colorMatch(getColorAtPixel(imageData, operator.x, operator.y), baseColor)
    }

    // Move downward
    while (contiguousDown &amp;&amp; operator.y &lt; height) {
      setColorAtPixel(imageData, newColor, operator.x, operator.y)

      // Check left
      if (operator.x - 1 &gt;= 0 &amp;&amp; colorMatch(getColorAtPixel(imageData, operator.x - 1, operator.y), baseColor)) {
        if (!contiguousLeft) {
          contiguousLeft = true
          stack.push({x: operator.x - 1, y: operator.y})
        }
      } else {
        contiguousLeft = false
      }

      // Check right
      if (operator.x + 1 &lt; width &amp;&amp; colorMatch(getColorAtPixel(imageData, operator.x + 1, operator.y), baseColor)) {
        if (!contiguousRight) {
          stack.push({x: operator.x + 1, y: operator.y})
          contiguousRight = true
        }
      } else {
        contiguousRight = false
      }

      operator.y++
      contiguousDown = colorMatch(getColorAtPixel(imageData, operator.x, operator.y), baseColor)
    }
  }
}



const scriptsInEvents = {

	async ["Bucket-Paint_Event12_Act5"](runtime, localVars)
	{
		fill(runtime);
	},

	async ["Bucket-Paint_Event13_Act5"](runtime, localVars)
	{
		fill(runtime);
	},

	async ["Bucket-Paint_Event17_Act1"](runtime, localVars)
	{
		MyFileChooser.click();
	},

	async ["Brush-Paint_Event23_Act1"](runtime, localVars)
	{
		MyFileChooser.click();
	}

};

self.C3.ScriptsInEvents = scriptsInEvents;

</pre></body></html>