Jump to content
• ### What is your GameDev Story?

• Advertisement

Public Group

#### Archived

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

# need help to get rid of this Bug

This topic is 5431 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

##### 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

##### 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

##### 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

##### Share on other sites
Oh shit, where where my eyes.... thank very very much guys

thanks again

kalash

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 15
• 14
• 46
• 22
• 27
• Advertisement
• ### Forum Statistics

• Total Topics
634053
• Total Posts
3015264
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!