Summary and analysis of the transformation of colorref,color,rgb,cstring _c language

Source: Internet
Author: User
Tags chr getcolor

One, the COLORREF and the RGB transform mutually
RGB (R,G,B) is a macro
Actually what it does is ((COLORREF) ((BYTE) (R) | ( (WORD) ((BYTE) (g)) <<8) | (((DWORD) (BYTE) (b)) <<16))

RGB (R,G,B) = an integer value = r + G * 256 + b*255*256

COLORREF is a 32-bit integer value that represents a color. You can use the RGB function to initialize the COLORREF
It's defined
typedef DWORD COLORREF;

COLORREF variables have two methods of assignment
First Kind
Colorref CF = RGB (,,);
Second Kind

Copy Code code as follows:

CColorDialog ColorDialog;
COLORREF color;


if (colordialog.domodal () = = Idok)
{
color = Colordialog.getcolor ();
}


This code uses the Color dialog box in MFC

How do I remove the RGB component value from the COLORREF?
You can use the macro Getrvalue
Getgvalue
Getbvalue

Their definition is as follows
#define GETRVALUE (RGB) ((BYTE) (RGB))
#define GETGVALUE (RGB) ((WORD) (RGB)) >> 8)

#define GETBVALUE (RGB) ((BYTE) (RGB) >>16)

Two, color and colorref (type int expression) of the reciprocal conversion
In the actual software development process, it is often necessary to use code that is not. NET platform. You may encounter COLORREF (that is, a color value represented by an int type or a color represented by a DWORD value). This is not directly implemented with the color conversion MS under the. NET Platform. Then we need to deal with it ourselves. Here are two functions.

Copy Code code as follows:

UINT Getcustomcolor (color color)
{
int ncolor = color. ToArgb ();
int blue = ncolor & 255;
int green = Ncolor >> 8 & 255;
int red = Ncolor >> & 255;
Return Convert.touint32 (Blue << | Green << 8 | red);
}


Color getargbcolor (int color)

{
int blue = color & 255;
int green = Color >> 8 & 255;
int red = color >> & 255;
Return Color.FromArgb (blue, green, red);
}


Note: The color arrangement in the COLORREF is BGR, and the color arrangement of the values obtained by Color.toargb () is AARRGGBB

Third, attention

Copy Code code as follows:

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));


}


This is the conversion between the COLORREF and the color straight, written using API functions.

////////////////////////////////////////////////////////////////////

Conversion between COLORREF and characters
One, the format of the string is very powerful

Copy Code code as follows:

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 Convert to String

BOOL Cdatamanager::getrgbtext (std::string &strrgbtext, colorref color)
{
Colorref col = RGB (255, 12, 4);
BYTE Red = getrvalue (color); Get the red color
BYTE Green = getgvalue (color); Get the green color
BYTE Blue = getbvalue (color); Get blue 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;
}

Strings are converted to COLORREF, such as ("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;
}
///////////////////////////////////////////////////////

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.