Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kalash

need help to get rid of this Bug

This topic is 5279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!