COLORREF和COLOR和RGB的总结

一、COLORREF与RGB的相互转化
RGB(r,g,b)是一个宏
实际上它做得事是((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))

 rgb(r,g,b)   =   一个整型值   =   r   +   g   *   256   + b*255*256   


COLORREF 是 一 个 32-bit 整 型 数 值,它 代 表 了 一 种 颜 色。你 可以 使 用 RGB 函 数 来 初 始 化 COLORREF
它的定义
typedef DWORD   COLORREF;


COLORREF变量有两种赋值方法
第一种
COLORREF cf = RGB(,,);
第二种
 CColorDialog colorDialog;
 COLORREF color;


    if( colorDialog.DoModal() == IDOK )
    {
        color = colorDialog.GetColor();
    }
这 段 代 码 使 用 了 MFC 中 的 颜 色 对 话 框


如何从 COLORREF中取出RGB分量值?
可以使用宏GetRValue
 GetGValue
 GetBValue


他们的定义如下
#define GetRValue(rgb)      ((BYTE)(rgb))
#define GetGValue(rgb)      ((BYTE)(((WORD)(rgb)) >> 8))

#define GetBValue(rgb)      ((BYTE)((rgb)>>16))

二 、Color与ColorRef(int类型表示的颜色)的相互转换
实际的软件开发过程中,常需要用到非.net平台的代码。这时候就可能碰到ColorRef(也就是以int类型代表的颜色值
或是以DWORD值表示的颜色)。这跟.net平台下的颜色的相互转换MS并没有直接实现。那么就需要我们自己处理
了。这里给出两个函数。                   
uint GetCustomColor(Color color)
{            


     int nColor = color.ToArgb();            


     int blue = nColor & 255;            


     int green = nColor >> 8 & 255;            


     int red = nColor >> 16 & 255;            


     return Convert.ToUInt32(blue << 16 | green << 8 | red);        


}


Color GetArgbColor(int color)        


 {            


     int blue = color & 255;            


     int green = color >> 8 & 255;            


     int red = color >> 16 & 255 ;            


     return Color.FromArgb(blue, green, red);        


 } 


注:COLORREF中颜色的排列是BGR,而通过Color.ToArgb()得到的数值中颜色排列是AARRGGBB

三、 注意

CColorDialog dlg;
if(dlg.DoModal() == IDOK)
{
//m_FillColor = dlg.GetColor();
COLORREF ctmpColor = dlg.GetColor();
Color tmpColor;
tmpColor.SetFromCOLORREF(ctmpColor);
ARGB argb = Color::MakeARGB(m_nFillTran, tmpColor.GetR(),tmpColor.GetG(),tmpColor.GetB());
//Graphics graphics(pDC->m_hDC);
//HatchBrush hBrush(HatchStyleDashedHorizontal,Color(100,255,0,0), Color(argb));
tmpColor = argb;
m_FillColor = tmpColor.ToCOLORREF();
//m_FillColor = RGB(Color(argb));

}

这是用api函数写的Colorref和color直间的转换。

原文地址:https://www.cnblogs.com/riskyer/p/3285524.html