Sign in to follow this  

Storing Color in a DWORD

This topic is 4837 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

Quick question about optimization: Currently, I am storing data for points in a structure: struct POINTS { unsigned int x, y; // For the position of the point unsigned long color; // For storing the color } Now, the way I planned on dealing with colors was storing them in hexadecimal in the long, (i.e. red would = 0xFFFF0000 ), and then shifting/masking to retrieve the specific parts of the color. I guess the only reason I looked into doing this was to become more familiar with bit operations, but now that I am, is there any reason to do this rather than create four unsigned ints, one for each red, green, blue, and alpha channels? Thanks!

Share this post


Link to post
Share on other sites
When using 32bit modes it is natural to store the color in a int (dword). Why waste the space?

If you dont like bit operations you can do something like this:


struct color_t
{
union
{
int argb
struct bits
{
char a, r, g ,b;
};
};
};



Now you can access each channel easily without bit operations. And the structure is no larger than 32bits which modern processors are good at handeling.

Share this post


Link to post
Share on other sites
For the most part, it's easier to work with colors when you have them in float-tuple form. Dropping things down to a DWORD saves space - might be good when saving/loading your stuff - but aside from that you get minimal benefit. Consider also that most pixel/vertex shaders take parameters in float form as well these days... you want DWORDs for vertex colors under D3D, but that's about all.

Share this post


Link to post
Share on other sites

This topic is 4837 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.

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