Convert hex color value to color

Source: Internet
Author: User

When assigning a value to a background, other colors can be given by the following methods, in addition to their own red,blue,black.

The main is to convert hex to ARGB (A:Alpha, which represents transparency, r:red, G:green, B:blue), where ARGB values are between 0--255

The string parameter passed in by this method can be similar to #21459A or #FF21459A

PublicStatic Color?Getcolorfromhex (String colorstr)//? Indicates that the return value can be null{if (colorstr! =Null && (Colorstr.length = =7 | | Colorstr.length = =9)) {byte A =255;int posi =1;if (colorstr.length = =9) {a = Byte.parse (Colorstr.substring (Posi,2), Numberstyles.hexnumber); Posi + =2; }byte R = Byte.parse (colorstr.substring (Posi, 2), numberstyles.hexnumber); Posi + = 2; byte g = Byte.parse (Colorstr.substring (Posi, 2), numberstyles.hexnumber); Posi + = 2; byte B = Byte.parse (Colorstr.substring (Posi, 2), numberstyles.hexnumber); return Color.FromArgb (A, R, G, b);} return null;}                

Then assign a value to the background color

Grid New SolidColorBrush (Getcolorfromhex ("#21459A"). Value);

Of course, when you know the RGB can also take the following ways:

Grid New SolidColorBrush () {Color = Color.FromArgb (255,154)};              // This . Foreground = new SolidColorBrush (Color.FromArgb (255,33,, 154));    // Object not created  this way. 


PS: During the programming process, copy the color value string from Skype on Win8.1. The length should be more than normal.

(The reason is that at the end there will be something for "" ...) It's really empty, but with the keyboard arrow keys you can see that there's something inside.

and is able to extract this empty character. But also because of this little detail troubled for a long time.

Convert hex color value to color

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.