DX中的颜色表示

 DX中有很多种方式可以表示颜色:

1 DWORD:

定义:typedef unsigned long       DWORD;

即为一个无符号32位(32机器)长整数,有四个字节,我们从左到右叫他1,2,3,4字节,每一个字节的范围是0~255。第一个字节表示alpha值,即透明度。如果是255,表示不透明,0表示完全透明(看不到),其他分别是R,G,B值。

获取:

可通过下列方法获得每个字节的值:

  1. int   A   =   (int)((DWORD   &   0xFF000000)   >>   24);   
  2. int   R   =   (int)((DWORD   &   0x00FF0000)   >>   16);   
  3. int   G   =   (int)((DWORD   &   0x0000FF00)   >>   8);   
  4. int   B   =   (int)(DWORD   &   0x000000FF);  

使用:

  1. struct CUSTOMVERTEX 
  2. {
  3.     D3DXVECTOR3 position;
  4.     float       rhw;
  5.     DWORD    color;
  6. };
  7. vertices[0].position = D3DXVECTOR3(150.0f,  50.0f, 1.0f);//四个点,注意三角形的构造顺序
  8. vertices[0].rhw      = 1.0f;
  9. vertices[0].color    = 0xffff0000;

2 D3DCOLOR

这个就不需要讲了,看了他的定义就知道了:

typedef DWORD D3DCOLOR;

 

3 D3DCOLOR_ARGB

定义:

  1. // maps unsigned 8 bits/channel to D3DCOLOR
  2. #define D3DCOLOR_ARGB(a,r,g,b) /
  3.     ((D3DCOLOR)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))

 

定义这个宏的目的是使用方便,一看就知道A,R,G,B的值,这个宏也比较简单,就是位移,然后合并。其实最后还是D3DCOLOR

使用:

  1. struct CUSTOMVERTEX 
  2. {
  3.     D3DXVECTOR3 position;
  4.     float       rhw;
  5.     D3DCOLOR    color;
  6. };
  7. vertices[0].color    = D3DCOLOR_ARGB(255,0,155,155);

还有D3DCOLOR_RGBA和D3DCOLOR_XRGB与上面差不多,看定义:

  1. #define D3DCOLOR_RGBA(r,g,b,a) D3DCOLOR_ARGB(a,r,g,b)
  2. #define D3DCOLOR_XRGB(r,g,b)   D3DCOLOR_ARGB(0xff,r,g,b)

 

3 D3DCOLORVALUE

定义:

  1. typedef struct _D3DCOLORVALUE {
  2.     float r;
  3.     float g;
  4.     float b;
  5.     float a;
  6. } D3DCOLORVALUE;

与上面说的最大的区别就是他的值为0~1

使用:

  1. struct CUSTOMVERTEX 
  2. {
  3.     D3DXVECTOR3 position;
  4.     float       rhw;
  5.     D3DCOLORVALUE    color;
  6. }; 
  7. vertices[0].position = D3DXVECTOR3(150.0f,  50.0f, 1.0f);//四个点,注意三角形的构造顺序
  8. vertices[0].rhw      = 1.0f;
  9. D3DCOLORVALUE d={1,0,0.5,0.2};
  10. vertices[0].color    = d;

 

4 D3DXCOLOR

这个封装了下,看定义就知道用法了:

  1. typedef struct D3DXCOLOR
  2. {
  3. #ifdef __cplusplus
  4. public:
  5.     D3DXCOLOR() {}
  6.     D3DXCOLOR( DWORD argb );
  7.     D3DXCOLOR( CONST FLOAT * );
  8.     D3DXCOLOR( CONST D3DXFLOAT16 * );
  9.     D3DXCOLOR( CONST D3DCOLORVALUE& );
  10.     D3DXCOLOR( FLOAT r, FLOAT g, FLOAT b, FLOAT a );
  11.     // casting
  12.     operator DWORD () const;
  13.     operator FLOAT* ();
  14.     operator CONST FLOAT* () const;
  15.     operator D3DCOLORVALUE* ();
  16.     operator CONST D3DCOLORVALUE* () const;
  17.     operator D3DCOLORVALUE& ();
  18.     operator CONST D3DCOLORVALUE& () const;
  19.     // assignment operators
  20.     D3DXCOLOR& operator += ( CONST D3DXCOLOR& );
  21.     D3DXCOLOR& operator -= ( CONST D3DXCOLOR& );
  22.     D3DXCOLOR& operator *= ( FLOAT );
  23.     D3DXCOLOR& operator /= ( FLOAT );
  24.     // unary operators
  25.     D3DXCOLOR operator + () const;
  26.     D3DXCOLOR operator - () const;
  27.     // binary operators
  28.     D3DXCOLOR operator + ( CONST D3DXCOLOR& ) const;
  29.     D3DXCOLOR operator - ( CONST D3DXCOLOR& ) const;
  30.     D3DXCOLOR operator * ( FLOAT ) const;
  31.     D3DXCOLOR operator / ( FLOAT ) const;
  32.     friend D3DXCOLOR operator * ( FLOAT, CONST D3DXCOLOR& );
  33.     BOOL operator == ( CONST D3DXCOLOR& ) const;
  34.     BOOL operator != ( CONST D3DXCOLOR& ) const;
  35. #endif //__cplusplus
  36.     FLOAT r, g, b, a;
  37. } D3DXCOLOR, *LPD3DXCOLOR;

可以对颜色加减了,使用起来有时候很方便,取值也是0~1,当然也可以取这个范围之外的值。

使用:

  1. struct CUSTOMVERTEX 
  2. {
  3.     D3DXVECTOR3 position;
  4.     float       rhw;
  5.     D3DCOLOR    color;
  6. };
  7. vertices[0].position = D3DXVECTOR3(150.0f,  50.0f, 1.0f);//四个点,注意三角形的构造顺序
  8. vertices[0].rhw      = 1.0f;
  9. vertices[0].color    = D3DXCOLOR(1,1,1,0);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值