Skip to content

Instantly share code, notes, and snippets.

@Larry57
Created October 18, 2012 13:32
Show Gist options
  • Save Larry57/3911823 to your computer and use it in GitHub Desktop.
Save Larry57/3911823 to your computer and use it in GitHub Desktop.
Clamp function
// Credits: http://stackoverflow.com/questions/2683442/where-can-i-find-the-clamp-function-in-net
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if(val.CompareTo(max) > 0) return max;
else return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment