tags:

views:

278

answers:

8

This is probably really easy, but I'm lost on how to "make sure" it is in this range..

So basically we have class Color and many functions to implement from it.

this function I need is:

Effects: corrects a color value to be within 0-255 inclusive. If value is outside this range, adjusts to either 0 or 255, whichever is closer.

This is what I have so far:

static int correctValue(int value)
{
    if(value<0)
        value=0;
    if(value>255)
        value=255;
}

Sorry for such a simple question ;/

+8  A: 

fair enough. but you missed the final return statement

static int correctValue(int value)
{
    if(value<0) return 0;
    if(value>255) return 255;
    return value;
}
sza
shoot, really was that simple. thanks!
codefail
+1 although for something this simple, I'd probably do "if (val < 0) return 0;" rather than setting a return value. It's small enough that the multiple returns don't affect readability.
paxdiablo
+1  A: 

Just return value at the bottom of your function after your ifs and you are good.

Michael Dorgan
+2  A: 

What you have will work fine. You just need to return value at the end of your function. If you want to make it more concise, you can use a ternary expression at the cost of a little bit of readability:

value = (value < MIN) ?
            MIN : (value > MAX) ?
                    MAX : value;

Where MIN is 0 and MAX is 255.

Vivin Paliath
Or use `min` and `max` to get it back. Even more readable is to wrap the functionality away. :]
GMan
Good point. I was commenting on ternary expressions overall. I use them but some people seem to dislike them.
Vivin Paliath
Ternary expressions used to be more efficient through saving an evaluation of a term, but in these days of optimizing compilers do they serve much more as a tool in the arsenal of obfuscated coding contestants? :) Although, having said that, I do use them in simple cases as I find it improves readability.
Duncan
@Duncan! Haha, I know, right? In some cases I find that they actually *improve* readability are better than having a bunch of if-else's.
Vivin Paliath
@Duncan: Ternaries have their use for initializing `const` objects with short expressions *(for some value of small)*.
Georg Fritzsche
+4  A: 

clamping is pretty easy using max and min:

value = std::min(255,std::max(0,value))

but your method should be correct also.

KillianDS
a) is std:: too much for homework, and b) why does that just _look_ wrong? (even though it's correct)
drachenstern
+1  A: 
static int correctValue(int value) {
 int correctedValue;
 if (value < 0) {
        correctedValue = 0;
 } else if (value > 255) {
        correctedValue = 255;
 } else {
        correctedValue = value;
 }

 return correctedValue;

}

Flash84x
+13  A: 

It's good. I'd recommend making a clamp function for re-usability:

template <typename T>
void clamp(T& pValue, const T& pMin, const T& pMax)
{
    pValue = std::min(std::max(pValue, pMin), pMax);
}

template <typename T>
T clamp_copy(T pValue, const T& pMin, const T& pMax)
{
    clamp(pValue, pMin, pMax);
    return pValue;
}

That gives you an in-place and copying clamp:

int i = 50;

j = clamp_copy(i, 0, 10);
k = clamp_copy(i, 100, 1000);
clamp(i, 0, 100);

// i = 50
// j = 10
// k = 100
GMan
i think we should go gently on him. hes just starting out, lets not blind him with generics quite yet :)
Aran Mulholland
@Aran: Meh, such a function is simple enough to be a good starting point. :)
GMan
+1 for not restricting the limits to 0 <= value <= 255
Duncan
Comment on down-vote, please? Is this going to be another inane case of "don't give noobs such good working code!"?
GMan
the downvote wasn't me - it is a good answer and will be useful to others intersted in a generic clamp.
Aran Mulholland
I suspect some may consider it a bit of an overkill for the level of the question. I'm sure it's very good code for general use (when you need to clamp any type) but, if all you want is to restrict the range of an integer, I consider the answer from ziang more readable. Having said that, I should stress that it's just my opinion. I went from C to Java via a *very* short C++ path so it's possible I may be wrong (just ask my wife).
paxdiablo
I do have one question. Can you overload based on the return type? I didn't think that was possible in C++. Or are templates a different breed of overloading?
paxdiablo
@pax: No, two functions cannot exist that differ only by return type. That's why one is suffixed by `_copy`. :) And, in my opinion, the function isn't terribly complex and should serve as a pretty basic template function. But damn those opinions, always different...
GMan
And now don't I feel like an idiot? :-)
paxdiablo
@paxdiable: Heh, that's okay. It's a bit hidden in there. :)
GMan
It would be great if `clamp_copy` had a second parameter template to allow to change the return type (you yourself remarked that `0..255` should be contained in an `unsigned char`). How to make it play nice with argument deduction is left as an exercise for the reader...
Matthieu M.
+1  A: 

I agree with the other answers, with one modification; this should be an else-if statement. There is no need to test if the value is over 255 if you already know it is less than 0

static unsigned char correctValue(int value)
{
    if(value<0) value=0;
    else if(value>255) value=255;
    return value;
}
Aran Mulholland
I would still return an `unsigned char` so that the return type makes it obvious that the value is now contained within `[0,255]`
Matthieu M.
fair enough, changed the answer.
Aran Mulholland
A: 

Instead of using an int, use an unsigned char, the value will then be unable to go lower than 0 or higher than 255.

gbrandt
An `unsigned char` could go higher than 255.
GMan