Picking Colors Optimally: An Optimization Problem in Python
usrsb.in
usrsb.in
Also, there's the issue of size. Medium-sized adjacent objects are perceived as having more contrast than large or small ones:
http://en.wikipedia.org/wiki/Contrast_(vision)#Contrast_sens...
So this is a more complex problem than simply choosing colors by number.
Here is a web-app that I use often: