Sign in to follow this  

RGBA (1 byte each) to 32bit Integer

Recommended Posts

You simply need to bit-shift the bytes and OR them together as appropriate. Note that 'int' is synonymous with 'signed int'. The signing makes this a little awkward. While an int is perfectly capable of storing a 32-bit colour value on an x86 machine, you're better off using something a little more explicit. The Direct3D API uses 32-bit D3DCOLORs, so you should too. If you truly need to use a signed int, you could always reinterpret_cast<signed int>, but I'd advise against it.

The Direct3D header d3d9types.h offers a macro 'D3DCOLOR_ARGB':
inline D3DCOLOR ToARGB(unsigned char a, unsigned char r, unsigned char g, unsigned char b) {
return D3DCOLOR_ARGB(a, r, g, b);
Note that this is ARGB, not RGBA. I recommend wrapping the macro in a function like this, so as to avoid most of the headaches associated with preprocessor macros. For completeness, a macroless RGBA alternative would go like:
inline D3DCOLOR ToRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
return (reinterpret_cast<D3DCOLOR> (r << 24) | (g << 16) | (b << 8) | a);


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this