# Thread: Representing floats with color?

1. ## Representing floats with color?

Ok I've got a challenge for you, a nut to crack. We have a color class with values r, g, b and a.

r,g,b and a each go from 0 to 255 as integers.

How do you make the color value represent any float value? Find some logic for how you could combine the four values to represent a float.

..or perhaps a reasonable value like between 0.0f and 1000000.0f. 2-3 decimals would be enough to store.

Will try to solve this myself in some way too... Edit: Things got a bit easier... added a fourth "a" value since we have an alpha channel as well. 2. Why would you want to do this? 3. Why would I NOT want to do this? The thing is I want to find a way to store the surface thickness of a 3d object at each pixel. Perhaps there is a better way? Any ideas are welcome.

I figured I can use the alpha channel to store the decimals like this:

Code:
`int decimals = (int) (val - floor(val)) * 100.0f);`
Only two decimals, 0 to 99 but that should be enough. 4. This is EXACTLY what I need. What strange coincidence that I logged on to get the answer to this question... and there the question is.

I for one, have a huge 2D array of doubles that will represent some physical value. First, though, I need to read in the properties of those points from a BMP, indicated by color. Finding a simple bijection between double bit patterns and 24-bit colors would save me a lot of redesign, and memory.

Edit: but I don't like the proposed solution.

Edit: Actually, think I found a workaround. Still -- an interesting question. Would there be a portable way? 5. Ok cool. My second post was not a solution though just an initial idea. I may find that only 2 decimals is too limiting. I think I need at least 3. 6. Well take a look in float.h

It'll tell you the bit-space of a float's different components. You could build a fairly safe method that way. 7. Originally Posted by misterMatt Why would you want to do this?
Should I rephrase? In what context would representing an RGB+alpha value as a float be useful?

A memory saving device perhaps? 8. One way it's possible, assuming a float is 4 bytes long:
Code:
```union {
float fv;
char sv;
} e;
e.sv = color.red;
e.sv = color.green;
e.sv = color.blue;
e.sv = color.alpha;

/* e.fv is your unique float-type variable representing the color */```
Not to mention that such a thing would be utterly pointless and stupid, but it's a solution. Sort of.  9. It's really quite a poor solution. The float's actual value would be undefined.
It's better to just use 4 chars to do this. Perhaps even use std::tr1::array to wrap them in a single container with no overhead.
But I still fail to understand what exactly it is that must be achieved, so this is the best solution I know of. 10. Originally Posted by misterMatt Should I rephrase? In what context would representing an RGB+alpha value as a float be useful?

A memory saving device perhaps?
Memory is an issue yes. I want to calculate the thickness of a 3d object at each pixel and store the thickness value for each pixel in a bitmap. Only after that will I have the info I need about what is the minimum and maximum thickness so after that step I want to work with the float values I stored in the bitmap and convert between the color and the float value.

I don't want to store an array in memory with possibly several million floats. Working with such an array would also be slow. With a bitmap I have the benefit of being able to easially check neighbors of the values as well.

In the final step the float value at each pixel will be converted to the color that the bitmap will actually have. 11. Originally Posted by CodeMonkey Well take a look in float.h

It'll tell you the bit-space of a float's different components. You could build a fairly safe method that way.
Thanks, had a quick look but couldn't find anything I understand to use. Might need to read up on bit operations a bit. 12. Originally Posted by EVOEx One way it's possible, assuming a float is 4 bytes long:
Code:
```union {
float fv;
char sv;
} e;
e.sv = color.red;
e.sv = color.green;
e.sv = color.blue;
e.sv = color.alpha;

/* e.fv is your unique float-type variable representing the color */```
Not to mention that such a thing would be utterly pointless and stupid, but it's a solution. Sort of.  13. Ignore the problems for a moment; can you post some pseudo-source illustrating what you want to do?

So far, what you are saying just isn't adding up.

Soma 14. Originally Posted by phantomotap Ignore the problems for a moment; can you post some pseudo-source illustrating what you want to do?

So far, what you are saying just isn't adding up.

Soma
Well I explained what I'm doing... but a bit more...

I'm drawing a bitmap that shows the thickness of a 3d object. When the bitmap is applied to the object each pixel of the bitmap will be at a certain surface point on the object. I calculate the thickness at that point and want to assign that thickness(float) to the corresponding pixel on the bitmap I'm drawing. The thing is that the final bitmap will go from black at the thickest point to white at the thinnest so after all pixels has been assigned float values I need to clamp them between 0.0 and 1.0.

Instead of creating an additional huge array of values it would be better and faster to do the calculation directly in the bitmap since the bitmap will already exists in memory. The bitmap is also very memory efficient and has access to neighbors that I need so it is the perfect container for my values, that is if can make floats fit in as colors. That is the challenge here. 15. Originally Posted by Elysia It's really quite a poor solution. The float's actual value would be undefined.
It's better to just use 4 chars to do this. Perhaps even use std::tr1::array to wrap them in a single container with no overhead.
But I still fail to understand what exactly it is that must be achieved, so this is the best solution I know of.
Why would it be undefined, if it is a union the float will have the same place in memory as the chars right? So starting from a float value I would assign the float value and in the char that float value would be split up in 4 parts each part holding 0 - 255. Isn't this a really elegant solution?

So in my case I could do:

Code:
```union {
float fv;
char sv;
} e;

float thickness = 40.86f;
e.fv = thickness;
Color col;
col.r = e.sv;
col.g = e.sv;
col.b = e.sv;
col.a = e.sv;

bitmap->putPixels(x,y,col);```
and to convert back:

Code:
```Color col = Color(12,64,255,0);
char ch = {(char)col.r, (char)col.g, (char)col.b, (char)col.a};
e.sv = ch;
float thickness = e.fv;```
Or could I? Popular pages Recent additions 