[C + +] Converts a long-type color value to an RGB value __c++

Source: Internet
Author: User

Conversion principle:

The calculation is: (65536 * Blue) + (256 * Green) + (Red)
' Convert RGB to LONG:
LONG = B * 65536 + G * 256 + R

' Convert LONG to RGB:
B = LONG \ 65536
G = (long-b * 65536) \ 256
R = long-b * 65536-G * 256

Conversion function: (inside VS2010 debugging Pass)

BOOL Convertcolor_longtorgb (const __int64 L_COLOR_SCR, int& r_des, int& g_des,int&);

BOOL Mainapp::convertcolor_longtorgb (const __int64 L_COLOR_SCR, int& r_des, int& g_des,int&)
{
	int color_long_min = 65536;
	int color_rgb_max = 256;
	if (l_color_scr<color_long_min)
	{return
		false;
	}
	B_des = l_color_scr/color_long_min;
    G_des = (L_color_scr-b_des * color_long_min)/Color_rgb_max;
    R_des = l_color_scr-b_des * color_long_min-g_des * COLOR_RGB_MAX;
	return true;
}


Call Method:

	LONG color_long=0; 
	Color_long = 12632256;
	int r=0;
	int g=0;
	int b=0;
	Convertcolor_longtorgb (color_long,r,g,b);
	cout<< "R:" <<R<<endl;
	cout<< "G:" <<G<<endl;
	cout<< "B:" <<B<<endl;

The above mainly uses, when extracts the BackColor value from the webpage, will obtain is the long type color value, this time needs to convert to the RGB use.

For example:

VARIANT Pcmdvalue;
:: VariantInit (&pcmdvalue);
BSTR BSTR = SysAllocString (OleStr (""));
Range->querycommandvalue (SysAllocString (Command.wc_str ()), &pcmdvalue); Get Pcmdvalue

if (PCMDVALUE.VT!= vt_empty) and your code ...

............................

Http://www.pbdr.com/pbtips/ps/rgblong.htm

The above page describes the relationship between long and RGB.

My test example is: 192,192,192 Gray.


Add: Inside the control to get color RGB, how to convert to the Web page needs to #ff00ff it. Please see below

Wxstring fontcolor=wxt ("");
Wxcolor Wxcolor_font = Retdata.getcolour ();
FontColor = Fontcolor.format (Wxt ("#%02x%02x%02x"),
Wxcolor_font.red (),
Wxcolor_font.green (),
Wxcolor_font.blue ());

Finish.



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.