Convert value to clamped 0.0-1.0 float

What would be a reliable way to do this? I need to convert RGB components to float values between 0.0 and 1.0 so 0 is 0.0 and 255 is 1.0.
Last edited on
if you know the component is going to be between 0 and 255:

 
float v = component / 255.0f;



If you're not sure on the actual range and you want to clamp it:

1
2
     if(v > 1)  v = 1;
else if(v < 0)  v = 0;
Last edited on
Topic archived. No new replies allowed.