Jump to content
  • Advertisement
Sign in to follow this  
Barrow Boy

Quick question about Direct3D colors

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

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
Advertisement
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
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
If you want you can use D3DXCOLOR instead. It let's you manipulate individual components as floating-point values, and also supports casting to a DWORD (it also has a DWORD constructor).

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!