COLOR RGB BGR以及字符串之间转换
来源:互联网 发布:thug life软件下载 编辑:程序博客网 时间:2024/06/11 02:41
一、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();
}
如何从 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并没有直接实现。那么就需要我们自己处理了。这里给出两个函数。
{
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);
}
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));
}
一、格式化字符串的功能很强大
int iRed = GetRValue(pMarkInfo->lfColor);
int iGreen = GetGValue(pMarkInfo->lfColor);
int iBlue = GetBValue(pMarkInfo->lfColor);
CString szColor;
szColor.Format(_T("#%02X%02X%02X"), iRed, iGreen, iBlue);
/////////////////////////////////////////////////////
CString m_BackColor.m_frame_color = "#0290D8";
DWORD r,g,b;
sscanf(m_BackColor.m_frame_color,"#%2X%2X%2X",&r,&g,&b);
COLORREF rgb = RGB(r,g,b);
brush.CreateSolidBrush (rgb)
//////////////////////////////////////////////////
//COLORREF转换为字符串
BOOL CDataManager::GetRGBText(std::string &strRGBText , COLORREF color)
{
//COLORREF col = RGB( 255 , 12 , 4);
BYTE Red = GetRValue(color); ///得到红颜色
BYTE Green = GetGValue(color); ///得到绿颜色
BYTE Blue = GetBValue(color); ///得到兰颜色
char chR[4];
itoa(Red ,chR , 10 );
char chG[4];
itoa(Green , chG , 10);
char chB[4];
itoa(Blue , chB , 10);
std::string strR , strG, strB;
strR = chR ;
strG = chG;
strB = chB;
strRGBText = strR + "," + strG + "," + strB;
return TRUE;
}
//字符串转换为COLORREF,如("32","34","21")
BOOL CDataManager::GetColorRGB(CString strColorText , COLORREF& color)
{
char chR[4] = "", chG[4] = "", chB[4] = "";
sscanf( strColorText, "%[^,],%[^,],%[^,]", chR, chG, chB);
color = RGB(atoi(chR), atoi(chG), atoi(chB));
return TRUE;
}
///////////////////////////////////////////////////////
- COLOR RGB BGR以及字符串之间转换
- RGB颜色与BGR颜色的转换问题
- OpenCV读取的图片从BGR转换为RGB
- BGR to RGB排列
- c#、WPF字符串,color,brush之间的转换
- c#、WPF字符串,color,brush之间的转换
- EmguCV中BGR与HSV之间的转换
- YUY2转RGB(或BGR)
- 各种字符串以及它们之间的转换
- RGB和YUV之间转换
- 【Computer vision】OPENCV RGB与BGR互换
- 【Compute Vision】【Color】图像RGB与YUV转换优化
- color工具类 hex、int与ARGB、RGB转换
- C#中将Color转换为RGB的样式
- 修改字符串以及string 与char* char[]之间的转换
- 字符串之间以及与int float double转换
- C#枚举和字符串以及整型数之间的转换
- Color和String之间的类型转换
- 三层架构及生活实例
- 简单动画
- 利用lucene对文件内容进行关键字检索
- JavaEE maven 综合实践
- POJ 3253 Fence Repair(贪心, Huffman编码)
- COLOR RGB BGR以及字符串之间转换
- NSFileManager—使用NSData缓存
- WebSphere问题集锦(未完待续)
- 什么导致了Context泄露:Handler&内部类
- 如何在Visual Studio项目中正确添加汇编代码
- Shiro haspermission 权限验证有关问题
- [Android开发]仿天天P图带气泡显示百分比进度的自定义SeekBar
- java启动spring容器
- 【WIN 32 报错】 Expression: ::IsWindow(m_hWnd)