Conversion of color values (colorref) in Win32 and. net

Source: Internet
Author: User

If you use MFC and. Net mixed programming, you will encounter this problem. If you use the control compiled by MFC, called by. net, the background color setting in the control requires color conversion.

Colorref type color value colorref Cr = RGB (123,200, 12 );

The order of the R, G, and B components is BGR.

In. net, the color is represented by the Data Type Color. This class has a function fromargb (INT, Int, INT). You can input three RGB values to a color type. There is also a toargb () function to get a 32-bit integer,

The 32-bit argb value is in bytes rggbb. 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 are respectively red, green, and blue color components.

After learning about the above content, color conversion is easy.

1. From color to colorref

Int ncolor = crcolor. toargb ();
Int Blue = ncolor & 255;
Int Green = ncolor> 8 & 255;
Int Red = ncolor> 16 & 255;

// Note that the color arrangement in colorref is BGR, and the color arrangement in the value obtained by color. toargb () is aarrggbb.
Int ncolorref = blue <16 | green <8 | red;

2. From colorref to color (note that the color arrangement in colorref is BGR, and the red weight is at the end)

Int Red = ncolorref & 255;

Int Green = ncolorref> 8 & 255;

Int Blue = ncolor ref> 16 & 255;

Color crcolor = color. fromargb (red, green, blue );

Or directly use the followingCode:

Color. fromargb (ncolorref & 255, ncolorref> 8 & 255, ncolor ref >>16 & 255 );

Note: The above code is written in 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: 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.