optimal sensor?

I want to count moving colored objects with a sensor. Imagine 100 M&M´s falling through a tube and I'd like to know how many of which color fell. Do you have an idea with which sensor this could work? Has someone ever built something similar and can give me his instructions and the code?

I'm pretty sure I saw something like this on Instructables or maybe it was youtube, or both...

Have a dig around there.

The only experience I ever had with a colour sensor was a total waste of time.

try this link it might help

This projects sorts candy by color but you could leave out the servo and just count each color.