Sign in to follow this  
Barrow Boy

Quick question about Direct3D colors

Recommended Posts

Barrow Boy    138
Lets say I want to create the color blue. I declare it as such:
DWORD color = D3DCOLOR_XRGB( 0, 0, 255 );
But what if, later in the program, I want to change the color to red? Do I have to redeclare 'color', like this?
color = D3DCOLOR_XRGB( 255, 0, 0 );
What I'm asking is, are there ways of accessing the individual r, g, and b values of a Direct3D color? For example, is there any way to do something like this:
color.r = 255; color.b = 0;
Or would I have to manually set up 3 separate variables to hold the rgb values, like this:
short r = 0, g = 0, b = 255;
DWORD color = D3DCOLOR( r, g, b );

Share this post

Link to post
Share on other sites
jpetrie    13098
The 'color' is just a DWORD there, with semantic value as a color. D3DCOLOR_XRGB is a macro that swizzles and packs the three components into the DWORD appropriately. It calls D3DCOLOR_ARGB, which itself is:

#define D3DCOLOR_ARGB(a,r,g,b) ((D3DCOLOR)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))

So with appropriate use of the shift and bitwise AND operators, you can extract the individual color components or manipulate them as needed. Chances are it will be more readable for you to "redeclare" the color as you did in your second example (which is not really redeclaration, just reassignment).

If you want to use a color object with individually accessible components, like R, G and B, you need to use something like the D3DXCOLOR struct and not DWORDs and D3DCOLOR_RGB macros.

Share this post

Link to post
Share on other sites
beun    160
Well, color = DWORD = 32-bits integer. So that's no struct. You can't do something like color.r. You can set the values yourself by shifting (32-bits for 4 components (RGBA) -> 8 bits per component. That macro does that for you. If you want to set only 1 color, you can do bitshifting (0, 8, 16 or 24 bits).

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