• 12
• 9
• 9
• 13
• 10

# getting the R, G, of B from a D3DCOLOR

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

## Recommended Posts

G'day, Finally back from an internetless vacation. I've had this question in my head for the complete 3 weeks (yeah, I encountered the problem right at the first day), and didn't know how to handle. Does anyone kow how to get the R, G and B values (seperate) from a D3DCOLOR and put them in an int? I hope someone can help. -Stenny

##### Share on other sites
Well, a D3DCOLOR is just a packed ARGB value.
So:
D3DCOLOR col = D3DCOLOR_ARGB(1, 2, 3, 4);int a = (col&0xff000000) >> 24;int r = (col&0x00ff0000) >> 16;int g = (col&0x0000ff00) >> 8;int b = (col&0x000000ff) >> 0;

##### Share on other sites
Alternately, shift first, then and with '0xff' always (for example, g = (col >> 8) & 0xff). I find it simpler.

Another way is to define a union with a bit field, which could result in clearer code, and give the compiler more leeway for optimisation.