Conversion of color values in WIN32 and color values (color) in. Net

Source: Internet
Author: User
Tags requires win32

If you are using MFC with. NET programming, you will encounter this problem, the control written through MFC, by. NET invocation, the setting of the background color in the control requires a color conversion.

If you are using MFC with. NET programming, you will encounter this problem, the control written through MFC, by. NET invocation, the setting of the background color in the control requires a color conversion.
The value of the COLORREF type color COLORREF cr=rgb (123,200,12);
The order of three components of R, G and B is BGR.
. NET color is represented by the data type color, which has a function FromArgb (int,int,int), which can be worth to a color type by entering RGB three. There's also a ToArgb () function that gets a 32-bit integer value,
The byte order of the 32-bit ARGB value is AARRGGBB. The highest valid byte (MSB) represented by AA is the alpha component value. The second, third, and fourth bytes represented by RR, GG, and BB, respectively, are red, green, and blue color components, and the color conversion is simple.
1, from color to COLORREF
int ncolor = Crcolor.toargb ();
int blue = ncolor & 255;
int green = Ncolor >> 8 & 255;
int red = Ncolor >> & 255;

Note that the color arrangement in the COLORREF is BGR, and the color arrangement of the values obtained by Color.toargb () is AARRGGBB
int ncolorref = Blue << 16 | Green << 8 | Red
2, from COLORREF to color (note that the COLORREF color arrangement is BGR, red component in the last side)
int Red=ncolorref & 255;
int green= ncolorref >> 8 & 255;
int blue= ncolor ref>> & 255;
Color Crcolor=color.fromargb (Red,green,blue);
or directly through the following code:
Color.FromArgb (Ncolorref & 255, Ncolorref >> 8 & 255, Ncolor ref>> & 255);
Note: The above code is written using C #.

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.