C # RGB vs. 16 binary Color Conversion method

Source: Internet
Author: User

Http://www.cnblogs.com/goldarch/archive/2010/08/16/1801053.html

#region [color: 16 binary to RGB]
<summary>
[Color: 16 binary to RGB]
</summary>
<param name= "Strcolor" > Set 16 binary color [return rgb]</param>
<returns></returns>
public static System.Drawing.Color Colorhx16torgb (String strhxcolor)
{
Try
{
if (Strhxcolor.length = = 0)
{//If empty
Return System.Drawing.Color.FromArgb (0, 0, 0);//Set to Black
}
Else
{//Convert color
Return System.Drawing.Color.FromArgb (System.Int32.Parse (strhxcolor.substring (1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse (Strhxcolor.substring (3, 2), System.glob Alization. Numberstyles.allowhexspecifier), System.Int32.Parse (Strhxcolor.substring (5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
}
}
Catch
{//Set to Black
Return System.Drawing.Color.FromArgb (0, 0, 0);
}
}
#endregion


#region [color: RGB to 16 binary]
<summary>
[Color: RGB to 16 binary]
</summary>
<param name= "R" > Red int</param>
<param name= "G" > Green int</param>
<param name= "B" > Blue int</param>
<returns></returns>
public static string colorRGBtoHx16 (int R, int G, int B)
{
Return System.Drawing.ColorTranslator.ToHtml (System.Drawing.Color.FromArgb (R, G, B));
}
#endregion

and

private string Tohexcolor (color color)
{
String R = convert.tostring (color. R, 16);
if (R = = "0")
R = "00";
String G = convert.tostring (color. G, 16);
if (G = = "0")
G = "00";
String B = convert.tostring (color. B, 16);
if (B = = "0")
B = "00";
String hexcolor = "#" + R + G + B;
return hexcolor;
}

public string ForeColor

{

Set

{

Value = #ab364f

int r = Convert.ToInt32 ("0x" + value. Substring (1, 2), 16);

int g = Convert.ToInt32 ("0x" + value. Substring (3, 2), 16);

int B = Convert.ToInt32 ("0x" + value. Substring (5, 2), 16);

Txturl.forecolor = System.Drawing.Color.FromArgb (r,g,b);

}

}

C # RGB vs. 16 binary Color Conversion method

Related Article

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.