Fill Irregular Objects with Color

Hi all, I tried my all getting a solution for this one, but I’d end up with an extremely complicated and unfeasible solution so I decided to come here. How would I got about using codea to fill in an irregular object (which is an image) with a color? Like, I want to fill in, for example, the image posted with this post.

I’m pretty sure what I’m looking for is pretty complicated and not a one step solution. Is there any tutorial on this or secret function I don’t know of (lol).

Idk why but my image isn’t posting. The image is just basically an irregular object with a black stroke, no fill, and on a white background (it is also drawn in a pixelated style).

Try this

function setup()
    --create an image
    img=image(600,600)
    setContext(img)
    stroke(255,255,0)
    strokeWidth(2)
    fill(0,0,0,0)
    ellipse(300,300,300)
    ellipse(375,325,300)
    setContext()
    --fill the three parts of the image
    Flood(img,color(255,0,0),color(0),300,300)
    Flood(img,color(0,0,255),color(0),200,300)
    Flood(img,color(0,255,0),color(0),500,300)
end

function draw()
    background(0)
    sprite(img,WIDTH/2,HEIGHT/2)
end

--p is the image
--c is the new colour 
--r is the colour to replace
--x,y are the x,y values of the pixel to start from 
--(any pixel inside the area you want filled)
function Flood(p,c,r,x,y,tbl)
    if tbl==nil then 
        tbl={}
        for i=1,p.width do tbl[i]={} end
    end
    if tbl[x][y]==nil then 
        local r,g,b=p:get(x,y)
        tbl[x][y]=color(r,g,b)
    end
    if tbl[x][y]==r then 
        p:set(x,y,c)
        tbl[x][y]=c
        --now check the neighbours around
        local w,h=p.width,p.height
        if x>1 then Flood(p,c,r,x-1,y,tbl) end
        if x<w then Flood(p,c,r,x+1,y,tbl) end
        if y>1 then Flood(p,c,r,x,y-1,tbl) end
        if y<h then Flood(p,c,r,x,y+1,tbl) end
        if x>1 and y>1 then Flood(p,c,r,x-1,y-1,tbl) end
        if x<w and y<h then Flood(p,c,r,x+1,y+1,tbl) end
        if y>1 and x<w then Flood(p,c,r,x+1,y-1,tbl) end
        if x>1 and y<h then Flood(p,c,r,x-1,y+1,tbl) end
    end
end