Just curious about something. While converting HTML colors (whose individual color components are represented via 2-digit hexadecimal values) to floats between 0.0 and 1.0 so we can use them with OpenGL, I became curious about something. We have to divide the hex values by 255 to give us their OpenGL counterparts, but we can’t simply use the hex values as-is because that produces integer division.
Now I know these all work around this issue (of course)…
float r = 0xFD / (float)0xFF; // Works because of the cast to float
float r = 0xFD / 255.0f; // Works because of the explicit float
float d = 0xFF;
float r = 0xFD / d; // Works because 'd' is a float
…but I was wondering if there’s any way to just decorate a hex value so it’s interpreted as a float (like you do with the ‘f’ in 1.0f) without having to do casting, calculations or interim variables.
These of course don’t work…
float r = 0xFD / 0xFF; // Integer division yields an integer, not float
float r = 0xFD / 0xFFf; // Interprets 'f' as part of the hex value
Again, not trying to find out how to achieve my needed results as my code works just fine. I’m just wondering if I can make the code cleaner via decorating the hex value with something similar to how ‘f’ works with decimal values instead of using the above-three methods that do work.
I’m afraid you have to use preprocessor definition in order to achieve the best look. And you need also casting to perform your computations.
The best solution I can think is
Floating point values cannot be specified as hexadecimal (integer) values.