in my c#-application, I use an RGB to HSV (and back) converter for changing a specified color-spectrum to another. In the actual case:

rgb1=5,122,255 to rgb2=22,165,220

similar colors to rgb1 will be changed to the related color, by saving the HSV-difference between hsv1 and hsv2 and aplying these to every similar color.

doing this in HSV was verry helpfull, since in RGB I wasn't able to archive this, but now i get errors, because sometimes an HSV-Value gets negative.

So my question, if an H(0>n>360) S,V(0>n>1) gets beneath or above its value-area, how do i need to change it (and do I also need to change the other two values) for getting the right value?

Calculation Example:

//Value Definition
hsvMainColor = 250/0.5/0.2
hsvSimilarColor = 260/0.6/0.4
hsvDestinationColor = 280/0.8/0.1
hsvMemory = null
hsvOutput = null

//Memorizing Differences
hsvMemory.H = hsvSimilarColor.H - hsvMainColor.H
            = 260 - 250
            = 10
hsvMemory.S = hsvSimilarColor.S - hsvMainColor.S
            = 0.5 - 0.6
            = -0.1
hsvMemory.V = hsvSimilarColor.V - hsvMainColor.V
            = 0.2 - 0.4
            = -0.2

//Creating hsvOutput by applying Differences to hsvDestination
hsvOutput.H = hsvDestination.H + thsvMemory.H
            = 280 + 10
            = 290
hsvOutput.S = hsvDestination.S + hsvMemory.S
            = 0.8 + (-0.1)
            = 0.7
hsvOutput.V = hsvDestination.V + hsvMemory.V
            = 0.1 + (-0.2)
            = (-0.1)

//Now I have a negative Value!
hsvOutput == 30/0.7/(-0.1)

1 Answer 1


There are many possible ways to deal with the situation at hand. Here, I'll try to present one of them.

Let's try to think one component at a time and what are the consequences of crossing its allowed range.

H denotes hue. Its value is between 0 and 360 degrees (containing 0, excluding 360, because 360 degrees can be understood as "0 degrees again"). H values are cyclical (like values of sin or cos functions). What does it mean if H value is less than 0? It means that we "rotated our hue past 0 degrees". So if we've got, say, value of "-25", then it's the same as if we've had H equal to "360-25", that is "335". Generally, add modulo 360 before storing the output value and forget about it ;}.

S means "saturation", by the given V. What if we've had saturation less than 0? "0" means the lowest saturation perceivable—effectively, colour is one of the shades of gray (regardless of the hue). So, if our colour can't be any less saturated, then values less than 0 are meaningless—we hit our bottom with 0, period. In other words, I'd suggest clipping all negative values to 0.

Similarly we can't have any colour saturated more than for the S value of 1. According to the same resoning as above, we'd have to clip any value greater than 1 to 1. It doesn't matter how hard we try, we can't get more saturated colour.

Now for the V. Value describes the "the lightness of the colour, by the given S". For S equal 1 and V equal 1 it gives the most saturated and brightest colours possible. V equal to 0 gives the darkest colours possible (actually one colour—black). Let's think once again about negative and greater than 1 values. We can't have colour darker than for V equal 0, so any negative V woudn't give any darker colour. Once again clipping value to 0 seems reasonable. Analogically, V greater than 1 can't give colours brighter than V equal to 1. This leads us to conclusion that clipping is once more the way to go.

Mind you, clipping means that our mapping is not bijective in general case, hence, it's not always reversible!

  • Very happy to hear that :}
    – thebodzio
    May 19, 2014 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.