Bitmap/Flood fill: Difference between revisions

Line 2,023:
<lang Python>from collections import deque
 
tolerance = 15
fill_color = color(250, 0, 0)
tolerance = 15
allowed = False
 
Line 2,042:
if allowed:
image(img, 0, 0, width, height)
text("Tolerance = {} (Use mouse wheel to change)".format(tolerance), 100, height - 30)
tolerance), 100, height - 30)
text("Right click to reset", 100, height - 10)
allowed = False
 
def mousePressed():
global imgallowed, allowedimg
img.loadPixels()
flood(mouseX, mouseY)
img.updatePixels()
allowed = True
if mouseButton == RIGHT:
img = loadImage("image.png")
else:
img.loadPixels()
flood(mouseX, mouseY)
img.updatePixels()
allowed = True
 
def mouseWheel(event):
Line 2,059 ⟶ 2,061:
e = event.getCount()
tolerance += 2 * e
if tolerance > 256128:
tolerance = 256128
if tolerance < 0:
tolerance = 0
Line 2,067 ⟶ 2,069:
def flood(x, y):
target_color = img.pixels[pixel_position(mouseX, mouseY)]
if target_color != fill_color:
queue = deque()
queue.append((x, y)= deque()
queue.append((x, y))
while len(queue) > 0:
p_x,while p_y = queue.popleftlen(queue) > 0:
if (check( p_x, p_y, target_color)= queue.popleft():
queue.appendif (check(p_x, p_y, - 1target_color)):
queue.append((p_x, p_y +- 1))
queue.append((p_x - 1, p_y + 1))
queue.append((p_x +- 1, p_y))
queue.append((p_x + 1, p_y))
 
def pixel_position(x, y):
Anonymous user