need help to get rid of this Bug

Started by
3 comments, last by kalash 20 years, 1 month ago
Being new to C++ this bug in the code below may be something very simple but i just don''t know what the hell it is.... I can''t understand why the values i print out are all zeroes at a particular point in this short code below, by the way all the code is doing is converting an RGB colour value to a H in HSV colour system space, the algorithm is not important to look at just if u can help me and tell me why it''s zero there (i marked it below in the code) #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <cmath> #include <conio.h> struct Colour { int r, g, b; }; double r,g,b,delta,maximum,minimum,h; int RGBtoH(int r1, int g1, int b1); #define max(a, b) (((a) > (b)) ? (a) : (b)) #define min(a, b) (((a) < (b)) ? (a) : (b)) int main() { Colour v; v.r=200; v.g=233; v.b=102; printf("\nvalue of H in RGB is %i\n\n\n", RGBtoH(200,233,102) ); return 1; } int RGBtoH (int r1, int g1, int b1) { //in range from 0 to 1. r = r1/255; g = g1/255; b = b1/255; //**** WHY ARE THERE R G B values all 0 here ??? printf("\n r g b are: %i %d %u ", r, g, b); maximum = max(max(r1,g1),b1); printf("\n max is: %i ", maximum); minimum = min(min(r1,g1),b1); //// *** REMEMBER IF S=0, Hue is NOT DEFINED !! ****** ////// //working out the hue delta = maximum-minimum; if (delta!=0) { double r_new = (maximum-r)/delta; double g_new = (maximum-g)/delta; double b_new = (maximum-b)/delta; if (r==maximum && g==minimum) h=5+b_new; else if (r==maximum && g!=maximum) h=1-g_new; else if (g==maximum && b==maximum) h=r_new+1; else if(g==maximum && b !=minimum) h=3-b_new; else if (r==maximum) h=5-r_new; } h=h*60; if(h<0) h=h+360; return h; // } Many thanks in advance kalash
Advertisement
Why are all zeroes there (here) -

//**** WHY ARE THERE R G B values all 0 here ???
printf("\n r g b are: %i %d %u ", r, g, b);
Because with integer arithmetic 200/255 = 0, 233/255 = 0 and 102/255 = 0. If you want to store the actual floating point value in the r, g and b variables you need to cast the numerator and/or denominator to doubles before division. Also in the printf() statement, you''re using integer format specifiers on floating point values. Change the format specifiers to display floating point.
SiCrane''s exactly right....

change
int r,g,b;
to
float r,g,b;

and

r = r1/255;
g = g1/255;
b = b1/255;
printf("\n r g b are: %i %d %u ", r, g, b);

to

r = r1/255.0;
g = g1/255.0;
b = b1/255.0;
printf("\n r g b are: %f %f %f ", r, g, b);
Oh shit, where where my eyes.... thank very very much guys

thanks again

kalash

This topic is closed to new replies.

Advertisement