-2

I'm trying to use this function to improve a HSV to RGB conversion but there is a little problem without using float...

HsvColor hsv;
unsigned char rgbMin, rgbMax;

rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);

hsv.v = rgbMax;
if (hsv.v == 0)
{
    hsv.h = 0;
    hsv.s = 0;
    return hsv;
}

hsv.s = 255 * (long int)(rgbMax - rgbMin) / hsv.v;
if (hsv.s == 0)
{
    hsv.h = 0;
    return hsv;
}

if (rgbMax == rgb.r)
    hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
else if (rgbMax == rgb.g)
    hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
else
    hsv.h = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);

return hsv;

My problem is that I have (rgb.b-rgb.r)<1, without using float I have the truncated value (so 0) and hsv.h stay always at 85...

hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);

Does anyone have an idea for that?

O'Neil
  • 3,790
  • 4
  • 16
  • 30
B. Delbart
  • 41
  • 5
  • 2
    You don't need the language in the title, that's what tags are for. Especially silly when title doesn't match the tags you've chosen. I'd guess you've seen this question, or your title is quite a coincidence... https://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both Why can't you use float? Have you considered fixed point? – Retired Ninja Sep 28 '17 at 13:39
  • 1
    Given that `.b` and `.r` are, presumably, integers, converting the result of the subtraction to a `float` should not have any difference, whatsoever; so your question seems unclear. – Sam Varshavchik Sep 28 '17 at 13:41
  • I can't use float... .b and .r are unsigned char that i've configure in a structure. – B. Delbart Sep 28 '17 at 13:45
  • So, what exactly is your question? – Sam Varshavchik Sep 28 '17 at 14:17
  • "_.b and .r are unsigned char_" so if their difference is `<1` they must have the same value, so the difference _is_ zero -- it's not that it's been truncated. So if you think `85` is the wrong answer for a particular rgb value, have you double-checked the formulae? – TripeHound Sep 28 '17 at 14:39
  • How does `.b and .r are unsigned char` implies `I can't use float`? Even if it is uchar you can have calculations on float and then cast it into uchar. – R2RT Sep 28 '17 at 14:40
  • Why can't you use float?? I have never seen HSV represented as only integers.. In any case, I wrote some code: https://pastebin.com/PqJfbSeA – Brandon Sep 28 '17 at 14:44

1 Answers1

0

For a precision of 3 digits after the decimal point, multiply the operands by 1000 before the operation, this way you'll have 1000*(rgb.b-rgb.r) < 1000 instead of (rgb.b-rgb.r) <1 and as a result you don't truncate the difference to 0. make sure the type of r and b are wide enough to contain 255*1000.

  • But `rgb.b` and `rgb.r` are `unsigned char` and therefore whole numbers. For `(rgb.b-rgb.r) <1` to be true, you must actually have `(rgb.b-rgb.r) =0` and so the two must be equal. Multiplying by 1,000 won't make any difference. – TripeHound Sep 29 '17 at 06:45