# Help understanding code that converts floating point number to RBG values

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

## Recommended Posts

I wasn’t too sure if this should be in the maths section but here we go. I was wondering if someone could help explain this section of code:
 vec3 marble_color(float x)
{
vec3 clr;
// make x 0 to 2
// Square root it range now 0 to 1.414213562 times 0.7071
x = sqrt(x+1.0)*0.7071; // x = (0 to 0.99999041)
clr.g = 0.30 + 0.8*x; // g = (0.3 to 1.099992328)
x=sqrt(x); // x =  0 to 0.999995205
clr.r = 0.30 + 0.6*x; //r = (0.3 to 0.899997123)
clr.b = 0.60 + 0.4*x; //b = (0.6 to 0.999998082)
return (clr);
}
I have added comments for when x is between -1 and 1 although in real situations the number can be a lot bigger than that. What it does is convert a floating point number to 3 separate Red, Blue and Green values. An example of this would be this: Image 1 B&W http://aycu35.webshots.com/image/43874/2003229353465368027_rs.jpg Image 2 Colour http://aycu35.webshots.com/image/40914/2003238542614432066_rs.jpg Image 1 is a gray scale image using just one floating point number to fill the RGB values. Image 2 is what it looks like after that single number is converted into three separate RGB values. I was just wondering how this works and how I could change it to give me the colour I want. Thanks

##### Share on other sites
I'm not sure what they're doing in that code, my guess is that it is just something they cooked up to get the job done. If I were you and I wanted to map a float to random spectrum of color values, I would pick a fixed saturation and value, let the float control hue, and then map from HSV to RGB.

##### Share on other sites
Quote:
 Original post by yahastuI'm not sure what they're doing in that code, my guess is that it is just something they cooked up to get the job done.

That was my guess as well but just wanted to get another opinion on it.
Quote:
 Original post by yahastuIf I were you and I wanted to map a float to random spectrum of color values, I would pick a fixed saturation and value, let the float control hue, and then map from HSV to RGB.

Well in other code the conversion of a floating point number into RGB values has been done by splines.

I presume he avoided the use of splines for his own set function to help reduce the calculations involved.