# Converting RGB to a single numeric value

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

## Recommended Posts

Does anyone know what the formula would be to convert the three color channels (RGB) of an image to a single color value? I want to use this as a heightmap, but I don't want to be limitted to 256 values of a grayscale image. Since an RGB image can many thousands of colors, I thought this would make for a more detailed heightmap, but I'm not sure of the formula to convert it? Thanks, QUAD

##### Share on other sites
Value = (R<<16) | (G<<8) | B;

##### Share on other sites
yeah, seems almost too simple, but yeah, I can see it. Let me check it out. Thanks

##### Share on other sites
FYI there's a happy prebuilt function to do just this:

//Read r, g, and b from your heightmapunsigned long Color = D3DCOLOR_XRGB(r, g, b);

If you wanted even more precision you could use an alpha channel and do this:
//Read a, r, g, and b from your heightmapunsigned long Color = D3DXCOLOR_ARGB(a, r, g, b);

The first example will produce numbers from 0 to 16,777,215 and the second will range from 0 to 4,294,967,295. Hope a little over four billion possible values is satisfactory ;-).

##### Share on other sites
I knew of those functions but didn’t put two and two together when I was thinking of what I wanted, argh. Thanks

1. 1
2. 2
JoeJ
20
3. 3
4. 4
5. 5
frob
12

• 10
• 13
• 21
• 13
• 20
• ### Forum Statistics

• Total Topics
632195
• Total Posts
3004702

×