I'm not sure exactly how it works, but contrast basically changes adjacent pixels to be further apart in their respective RGB values, or closer together.
I did an experiment in Photoshop, with a graphic that had Red, Orange, Yellow, Green, Blue, Purple, and Red -- with shades in between. Changing the contrast didn't change the core colors at all, but the mixes between did change. When contrast was at +100, all you had were the 6 colors in bars. When at -100, it was an almost perfectly uniform shade of gray all the way across, #808080.
I think the algorithm is complicated. A google search did little to turn up anything useful. You may want to simply use ImageMagick.