Algorithm for Color Balancing an Image
Photoshop's Auto Color is nothing more than Histogram Equalization.  

It has some improvements over the classic Histogram Equalization by adding a threshold of not touching the % at the extreme values.

Try it and see for yourself:  


Histogram Equalization.  
Histogram Equalization (Image Processing) Part 1.
Histogram.


Good Luck.