While tinkering with image editing, I thought I might implement a contrast algorithm; something that would take a range of values and, based on the value that the user provides (from 0 to 255; the range of a byte) the contrast will be decreased or increased. I've looked online for "contrast algorithm" and I can't seem to find anything.
As far as personal effort, I've given it a shot.
To increase the contrast, a margin is removed from the low and high end of the range. The low end is shifted down by that margin to become 0. The high end is scaled up by the margin * 2.
To decrease the contrast, a margin is removed from the center of the range. The low and high ends of the range are "stretched" to fit the lost area.
But I have no idea if this formula is mathematically sound. When I implemented it, I got strange results:

Can you identify what I did wrong here or at least provide links to contrast formulas or that which I need to acquire them?