type v2
        x as integer
        y as integer
     endtype
     dim node(0) as v2
sync on
sync rate 0
Do
  if mouseclick()=1
    ox=mousex()
    oy=mousey()
    while mouseclick()=1
    line ox,oy,mousex(),mousey()
    ox=mousex()
    oy=mousey()
    sync
    endwhile
  endif
  if mouseclick()=2
     `this makes a memblock from the bitmap on the screen:
     make memblock from bitmap 1,0
     fx=mousex()
     fy=mousey()
     `this finds the position of the colors for the pixel at (fx,fy) in the memblock
     pos=12+fy*(screen width())*4+fx*4 `once you learn this, editing image and bitmap memblocks will be easier
     ocb=memblock byte(1,pos)
     ocg=memblock byte(1,pos+1)
     ocr=memblock byte(1,pos+2)
     `ocb = old color blue, etc.
 
     `we'll make the new color a random one.
     ncb=rnd(255)
     ncg=rnd(255)
     ncr=rnd(255)
     empty array node(0)
     array insert at top node(0)
     node(0).x=fx
     node(0).y=fy
     `go through all the nodes
     while array count(node(0))>=0
        tot=array count(node(0))
        For n=0 to tot
            `look around each open node
           for x=node(n).x-1 to node(n).x+1
              for y=node(n).y-1 to node(n).y+1
               `this checks to see if the node is in the screen. It also disables diagonal search
                 if x>=0 and y>=0 and x<screen width() and y<screen height() and (x=node(n).x or y=node(n).y)
                    `get the position of point x,y in the memblock
                    pos=12+y*(screen width())*4+x*4
                    `see if the color at this point in the memblock matches the old color:
                    if memblock byte(1,pos)=ocb and memblock byte(1,pos+1)=ocg and memblock byte(1,pos+2)=ocr
                       `if so add a node, and update the point in the memblock with the new colors
                       array insert at bottom node(0)
                       node(array count(node(0))).x=x
                       node(array count(node(0))).y=y
                       `new colors are written to memblock
                       write memblock byte 1,pos,ncb
                       write memblock byte 1,pos+1,ncg
                       write memblock byte 1,pos+2,ncr
                       `this makes sure it is opaque (not transparent).
                       write memblock byte 1,pos+3,255
                    endif
                 endif
              next y
           next x
           `delete the current element from the array
           array delete element node(0),0
           `while it looks like n is increasing to tot,
           `we really keep n at zero for every iteration and decrease
           `tot to 0.
           dec n
           dec tot
 
        next n
        `this is what shows the fill in real time:
        make bitmap from memblock 0,1
        sync
     endwhile
     `convert the memblock back to an image:
     make bitmap from memblock 0,1
  endif
  sync
Loop