Paint on top of each color

hi guys , i was trying to help a friend with his project but we could not get the way to do it .

the aim is to draw everytime the color change , on top of the last color , the shape will be taken from a fabric sheet and as you move your hands you draw the shape , the problem is that the color is adding to last color so it alwyas become white , how could this be achived ?

the little video is taking from it

paint on top of each color.zip (84.5 kB)

you shoulda have checka da depthbuffa.

but thats not all. check this approach, perhaps you get closer.

paint on top of each color.zip (88.2 kB)

hi kalle thanks for your aproach , i was using depthbuffer in other testing patch ,still no way to do that , i tried this 2 ways but i think may be this is not the way to go for this task , as the goal is to paint on top when color change or in other words when the fabric is touched again , always keeping the last draw backwards ,
any ideas ?

paint on top of each color.zip (176.4 kB)

a simulation pic , is this posible ?

paint_on_top.jpg (41.5 kB)