# clamping values

This topic is 4520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm looking at dot3 bumpmapping and at one point during the info I'm looking at it says that an integer between [0,255] must be clamped to [-1.0,1.0].
float clamp(unsigned char i)
{
float temp;

temp = (i / 255.0f);	// first, clamp the value to [0,1]
temp *= 2;		// multiply by 2
temp -= 1.0f;		// subtract 1

return temp;
}

This is what I did, and it initially looks to be fine (works with values 0,65,127,191,255). I was wondering if there was a generalised forumla for clamping a value between [a,b] to [c,d]. I took a look on mathworld, but I'm not really sure what to search for.

##### Share on other sites
[a,b] ->f [c,d]
f(x) = (x-a)       // move [a,b] to [0,b-a] so we may scale by means of multiplication     * (d-c)/(b-a) // scale [0,b-a] to [0,d-c]     + c           // move from [0,d-c] to [c,d]

I'm not sure word "clamp" fits here. It's more like "remap".
Unless you're changing data type in the meantime, so it's clamped to some min/max automatically.

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011473
• ### Who's Online (See full list)

There are no registered users currently online

×