fill

  how to do a bucket fill function in lingo
fill




  fill
There was a diskussioon about ways to do a bucket fill  on lingo_l.

Joseph Bergevin wrote:

The things I miss out on when I drop off the list for a bit.
Here's my recursive solution. I wrote it quite a while ago. It's slow. Fun
to watch fill, though. Just draw a bitmap, drop this behavior on it, and
click somewhere.

_joe

---------------------------------

property spnum,mymem,pcolor,xoffset,yoffset

on new me
  spnum=me.spritenum
  mymem=sprite(spnum).member
end

on beginsprite me
  myloc=sprite(spnum).rect
  xoffset=getat(myloc,1)
  yoffset=getat(myloc,2)
end

on mousedown me
  x=(the mouseh)-xoffset
  y=(the mousev)-yoffset
  pcolor=getpixel(mymem,x,y)
  fill me,x,y
end

on fill me,x,y
  xcolor=getpixel(mymem,x,y)
  if xcolor=pcolor then
    --change the number (1677216) for a different color
    setpixel(mymem,x,y,1677216)
    --disable the updatestage for greater speed. still slow. bah.
    updatestage
    fill(me,x+1,y)
    fill(me,x,y-1)
    fill(me,x-1,y)
    fill(me,x,y+1)
  end if
end


Jakob Hede Madsen:

--------- flood fill color in bitmap to random color
--------- Jakob Hede Madsen
--------- hede@image.dk
--------- special thanks: Roy Pard & a whole host of others

property pPctRef
property pSprLeft
property pSprTop
property pSprScalarX
property pSprScalarY
property pPixelActorListX
property pPixelActorListY
property pSpreadListX
property pSpreadListY

property whichMember
---------------

on beginSprite me
  sprite(me.spriteNum).cursor = 259

  mySpr = sprite me.spriteNum
  whichMember = mySpr.member
  sWidth = member(whichMember).width
  sHeight = member(whichMember).height
  imageDepth =  member(whichMember).depth
  pPctRef = image(sWidth, sHeight,imageDepth)

  --  pPctRef = mySpr.member
  pSprLeft = mySpr.left
  pSprTop = mySpr.top
  pSprScalarX = float(sWidth) / mySpr.width
  pSprScalarY = float(sHeight) / mySpr.height
  pPixelActorListX = []
  pPixelActorListY = []
  pSpreadListX = [1, 0, -1, 0]
  pSpreadListY = [0, 1, 0, -1]
end

---------------

on mouseDown me
  sprite(me.spriteNum).cursor = 4
  if the doubleClick = FALSE then
    pPctRef.copyPixels(whichMember.image, pPctRef.rect, whichMember.rect)
    global fillColor
    sprX = the mouseH - pSprLeft
    sprY = the mouseV - pSprTop
    mbrX = integer(sprX * pSprScalarX) - 1
    mbrY = integer(sprY * pSprScalarY) - 1
    ----

    if pPctRef.getPixel(mbrX, mbrY, #integer) <> fillColor then
      me.mFlood(mbrX, mbrY, fillColor)

      whichMember.image.copyPixels(pPctRef, pPctRef.rect, whichMember.rect)
      updateStage
    end if

  end if
  sprite(me.spriteNum).cursor = 259
end

---------------

on mFlood me, aX, aY, aSourceColor
  targetColor = pPctRef.getPixel(aX, aY, #integer)
  pPctRef.setPixel(aX, aY, aSourceColor)

  append pPixelActorListX, aX
  append pPixelActorListY, aY
  repeat while count(pPixelActorListX)
    xCur = getAt(pPixelActorListX, 1)
    yCur = getAt(pPixelActorListY, 1)
    deleteAt pPixelActorListX, 1
    deleteAt pPixelActorListY, 1

    xTest = xCur + 1
    yTest = yCur
    if pPctRef.getPixel(xTest, yTest, #integer) = targetColor then
      pPctRef.setPixel(xTest, yTest, aSourceColor)
      -- updateStage
      append pPixelActorListX, xTest
      append pPixelActorListY, yTest
    end if

    xTest = xCur
    yTest = yCur + 1
    if pPctRef.getPixel(xTest, yTest, #integer) = targetColor then
      pPctRef.setPixel(xTest, yTest, aSourceColor)
      -- updateStage
      append pPixelActorListX, xTest
      append pPixelActorListY, yTest
    end if

    xTest = xCur - 1
    yTest = yCur
    if pPctRef.getPixel(xTest, yTest, #integer) = targetColor then
      pPctRef.setPixel(xTest, yTest, aSourceColor)
      -- updateStage
      append pPixelActorListX, xTest
      append pPixelActorListY, yTest
    end if

    xTest = xCur
    yTest = yCur - 1
    if pPctRef.getPixel(xTest, yTest, #integer) = targetColor then
      pPctRef.setPixel(xTest, yTest, aSourceColor)
      -- updateStage
      append pPixelActorListX, xTest
      append pPixelActorListY, yTest
    end if
  end repeat
end






Home shock + cgi Bits 'n pieces Director Lingo ShockLets Contact