Click here to Skip to main content
12,997,042 members (128,553 online)
Click here to Skip to main content
Add your own
alternative version

Stats

50.4K views
8 bookmarked
Posted 30 Jan 2008

A Generic Clamp Function for C#

, 31 Jan 2008
Rate this:
Please Sign up or sign in to vote.
A function for 'clamping' values to within a given range

Introduction

This is my first C# generic function.

In computer terms, to clamp a value is to make sure that it lies between some maximum and minimum values. If it’s greater than the max value, then it’s replaced by the max, etc.

I first learned it in the context of computer graphics, for colors - it’s used, for instance, to make sure that your raytraced pixel isn't "blacker than black".

So:

public static T Clamp<T>(T value, T max, T min)
     where T : System.IComparable<T> {
    T result = value;
    if (value.CompareTo(max) > 0)
        result = max;
    if (value.CompareTo(min) < 0)
        result = min;
    return result;
}

Usage

int i = Clamp(12, 10, 0); -> i == 10
double d = Clamp(4.5, 10.0, 0.0); -> d == 4.5

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mike McCabe
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionFailed clamp Pin
ShaneYu13-Jul-13 20:52
memberShaneYu13-Jul-13 20:52 
AnswerRe: Failed clamp Pin
ShaneYu13-Jul-13 21:02
memberShaneYu13-Jul-13 21:02 
GeneralI like more this one Pin
Alberto Bencivenni6-Feb-08 0:31
memberAlberto Bencivenni6-Feb-08 0:31 
GeneralExcellence milk for babes Pin
JadeZhao2-Feb-08 1:47
memberJadeZhao2-Feb-08 1:47 
GeneralGreat! Pin
Jean-Paul Mikkers1-Feb-08 9:34
memberJean-Paul Mikkers1-Feb-08 9:34 
GeneralUsage examples Pin
J4amieC1-Feb-08 2:24
mvpJ4amieC1-Feb-08 2:24 
GeneralRe: Usage examples Pin
StockportJambo1-Feb-08 4:36
memberStockportJambo1-Feb-08 4:36 
GeneralRe: Usage examples Pin
J4amieC2-Feb-08 0:10
mvpJ4amieC2-Feb-08 0:10 
GeneralSome comments. Pin
Ennis Ray Lynch, Jr.31-Jan-08 17:03
memberEnnis Ray Lynch, Jr.31-Jan-08 17:03 
AnswerRe: Some comments. Pin
mikemccabe31-Jan-08 23:06
membermikemccabe31-Jan-08 23:06 
QuestionUsage? Pin
PCoffey31-Jan-08 3:00
memberPCoffey31-Jan-08 3:00 
AnswerRe: Usage? Pin
mikemccabe31-Jan-08 11:58
membermikemccabe31-Jan-08 11:58 
GeneralRe: Usage? Pin
Mladen Jankovic31-Jan-08 12:55
memberMladen Jankovic31-Jan-08 12:55 
GeneralRe: Usage? Pin
PCoffey1-Feb-08 3:32
memberPCoffey1-Feb-08 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170622.1 | Last Updated 1 Feb 2008
Article Copyright 2008 by Mike McCabe
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid