Jump to content
  • Advertisement
Sign in to follow this  
REspawn

RGBA (1 byte each) to 32bit Integer

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im using c++ and dx and I have my R G B A color values and im wondering how I can combine these into an int color value? Thanks for any help, -Dave

Share this post


Link to post
Share on other sites
Advertisement
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);
}

Admiral

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!