2
$\begingroup$

I'm interpolating RGB values by transforming them into HSV space and then back into RGB. I have trouble figuring out how to achieve interpolate(green, black, 0.5) = darkgreen.

Black has multiple representations in HSV, for example (0, 0, 0) and (30, 0, 0) are both black. When I convert the RGB black #000000, I turn it into (0, 0, 0). When I then interpolate it with green (120, 1, 1), I get (60, 0.5, 0.5) which is dark semi-saturated yellow. A similar issue exist for white.

Am I supposed to choose a different color space? I don't want to use LAB color space, because it's computationally expensive. How can I make this correct?

$\endgroup$

1 Answer 1

2
$\begingroup$

If you are interpolating in HSV you will need to handle a few special cases.

If the value of a colour is 0, you should calculate as though the hue and saturation of that colour is the same as the other colour. This means you should use (120, 1, 0) for black in your example.

If a colour has a saturation of 0, you should calculate as though the hue was the same as the hue of the other colour. (If both colours have a saturation of 0 you can use the hue from either.)

If the difference between the two hue values is greater than 180 degrees, you should add 360 to the lower hue value before interpolating. If the resulting interpolated hue is greater than 360, then you should subtract 360 from the interpolated hue.

Alternatively, you could interpolate directly in RGB, which will not require any special cases. This will not give the same interpolated value as HSV, so it depends on how you want the interpolation to work.

$\endgroup$

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.