clamp function is wrong

you have to pick the maximum value between "min" and the minimum between "max" and "value"

refer here: http://stackoverflow.com/a/16659144/2008214