Color extension class-ColorExtensions, devcolorextensions

Source: Internet
Author: User
Tags hex code

Color extension class-ColorExtensions, devcolorextensions

/// <Summary> /// color extension class /// </summary> public static class ColorExtensions {// <summary> ///. NET pre-defined system Color cache list /// </summary> private static readonly Dictionary <int, Color> ColorCache; /// <summary> /// list of system color names with the same color code value // </summary> private static readonly Dictionary <int, string []> DuplicateColorNameCache; /// <summary> /// static constructor /// </summary> static ColorExtensions () {ColorCache = new Dictiona Ry <int, Color> (); DuplicateColorNameCache = new Dictionary <int, string []> (); Type type = typeof (Color); var properties = type. getProperties (BindingFlags. public | BindingFlags. static | BindingFlags. ignoreCase); foreach (var propertyInfo in properties) {var value = propertyInfo. getValue (null, null); if (value is Color) {var color = (Color) value; var intCode = color. toIntCode (); if (! ColorCache. containsKey (intCode) {ColorCache. add (intCode, color);} else {if (DuplicateColorNameCache. containsKey (intCode) {var values = DuplicateColorNameCache [intCode]. toList (); values. add (color. name); DuplicateColorNameCache [intCode] = values. toArray ();} else {string [] values = {ColorCache [intCode]. name, color. name}; DuplicateColorNameCache [intCode] = values ;}}}/// <summary> /// Returns the color value in decimal format. The value ranges from 0 ~ 16777215. /// <Para> for example, 0 indicates black and 16777215 indicates white. </Para> /// </summary> /// <param name = "color"> Color object </param> /// <returns> color value in decimal format </returns> public static int ToIntCode (this Color color) {int c = color. r; c = c | color. G <8; c = c | color. B <16; return c; // the following code is equivalent. // convert the code to a hexadecimal value and then a hexadecimal value. // var hexCode = color. toHexCode (false); // obtain the color code value in hexadecimal format // int intCode = Convert. toInt32 (hexCode, 16); // obtain the color code value in decimal format} // <summary> // return the color value in hexadecimal format. /// <Para> for example, Color. Gray (Gray) returns #808080. Color. fromArgb (192,192,192) returns # C0C0C0 </para> /// </summary> /// <param name = "color"> Color object </param> /// <param name = "isAddPrefix "> whether to add a prefix "#", the default value is true. </Param> // <returns> hexadecimal Color value </returns> public static string ToHexCode (this color, bool isAddPrefix = true) {return string. format ("{0} {1: X2} {2: X2} {3: X2}", isAddPrefix? "#": String. empty, color. r, color. g, color. b); // The following method is encountered. NET pre-defined color object, will return the color name, instead of the hexadecimal value. // Return ColorTranslator. toHtml (color) ;}/// <summary> // return the color code value supported by HTML. If the color is already predefined in HTML, the color name is returned, otherwise, the code value in hexadecimal format is returned. /// <Para> for example, Color. Gray (Gray) returns Gray. Color. fromArgb (192,192,192) returns # C0C0C0 (silver) </para> // <para> for example, this is because the color code value has been predefined in HTML, therefore, HTML supports the name value corresponding to the color code value. </Para> /// <para>. NET has three pairs of pre-defined color code values are the same, so when the input color object in the ARGB representation is in the pre-defined color range of the three pairs, only one color name in a pair is returned. </Para> /// <para> When You Need To Know another color name, you can </para> /// </summary> /// <param name = "color"> Color object </param> /// <returns> If the color is already predefined in HTML, otherwise, the code value in hexadecimal format </returns> public static string ToHtmlCode (this Color color) {int intCode = color. toIntCode (); if (ColorCache. containsKey (intCode) {return ColorTranslator. toHtml (ColorCache [intCode]);}/** the reason for this is that when the user uploads a Color object represented by ARGB. * After the above processing, the color name can be returned successfully. * Example: Color color1 = Color. gray; // Gray * Color color2 = Color. fromArgb (color1.A, color1.R, color1.G, color1. B); * color1.ToHtmlCode () returns "Gray" * color2.ToHtmlCode () returns "#808080 ". * In fact, the above two Color objects are equal. It is reasonable for these two objects to call the ToHtmlCode () Extension Method to return the same result. */Return ColorTranslator. ToHtml (color) ;}/// <summary> // gets an array of color names with the same code value. If no, null is returned. /// </Summary> /// <param name = "color"> Color object </param> /// <returns> returns an array of color names with the same code value, if no, null is returned. </Returns> public static string [] GetDuplicateColorNames (this Color color) {int intCode = color. toIntCode (); if (DuplicateColorNameCache. containsKey (intCode) {return DuplicateColorNameCache [intCode];} return null ;} /// <summary> /// convert the Color code value of the decimal format to the Color object // </summary> /// <param name = "intCode"> Color in decimal format code value </param> /// <returns> returns the Color object </returns> public static Color IntToColor (int intCode) {I F (ColorCache. containsKey (intCode) {return ColorCache [intCode];}/** the preceding processing is the same as the ToHtmlCode Method * first checks whether it is consistent.. NET pre-defined color match. If yes, return.. NET pre-defined color object (this object contains a color-friendly name that can be understood directly ). * If it does not match, the Color object represented by ARGB is returned. */String hexCode = Convert. toString (intCode, 16); return ColorTranslator. fromHtml ("#" + hexCode);} // <summary> // convert the color code value of the hexadecimal format starting with "#" or.. NET pre-defined Color name: Color object // </summary> /// <param name = "hexCodeOrColorName">. NET predefined Color name or hexadecimal Color code value starting with "#" </param> // <returns> </returns> public static Color HexOrNameToColor (string hexCodeOrColorName) {if (string. isNullOrEmpty (hexCodeOrColorName) {return Color. empty;} try {// This is hex code. if (hexCodeOrColorName [0] = '#') {string hexCode = hexCodeOrColorName. substring (1); int intCode = Convert. toInt32 (hexCode, 16); if (ColorCache. containsKey (intCode) {// return the Color object return ColorCache [intCode] with a friendly name;} // return the Color object return ColorTranslator in the argb format. fromHtml (hexCodeOrColorName);} // This is color name. return ColorTranslator. fromHtml (hexCodeOrColorName);} catch (Exception) {return Color. empty ;}}}

 

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.