![]() ![]() Throw new FormatException("Invalid HTML Color") ![]() Throw new ArgumentException($"Provided parameter '' is not valid") If (!htmlColor.StartsWith("#") & requireHexSpecified) Throw new ArgumentNullException(nameof(htmlColor)) Public static int HtmlColorToArgb(string htmlColor, bool requireHexSpecified = false, int defaultAlpha = 0xFF) So this is a solution that does #RGB and #RGBA shorthand - and extended color definition public static Color ParseHtmlColor(string htmlColor) => Color.FromArgb(HtmlColorToArgb(htmlColor)) So, if for some reason you want to avoid and create your own implementation, you should at least make it respect the specifications So in short: Use the Framework if you can. I get that some people want to avoid so there is the other solution, and since you want to have a you should have a reference to System.Drawing already in your project. In short I support those that propose to use. This was inspired by Hans Kesting's answer. Hex must be either an ARGB (8 digits) or RGB (6 digits)") Throw new ArgumentException("Invalid Hex value. If method hasn't returned a color yet, then there's a problem If (Int32.TryParse(cleanHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out argb)) Affix fully opaque alpha hex value of FF (225) String cleanHex = hex.Replace("0x", "").TrimStart('#') public static GetColorFromHexValue(string hex) Therefore, a simple method can be created that handles both ARGB and RGB hex's and converts them to the appropriate Color struct. This means by adding FF to a 6 digit (RGB) hex color code it becomes an 8 digit ARGB hex color code. The alpha value is implicitly 255 (fully opaque). Therefore, I thought I'd add a comprehensive solution that deals with both 6 digit (RGB) and 8 digit (ARGB) hex values.īy default, according to Microsoft, when converting from an RGB to ARGB value This post has become the goto for anyone trying to convert from a hex color code to a system color. Note 2: please provide your own error checking (colorcode should be a hexadecimal value of either 6 or 8 characters) Note 1: NumberStyles is in System.Globalization. Int.Parse(colorcode.Substring(6, 2), NumberStyles.HexNumber)) Int.Parse(colorcode.Substring(4, 2), NumberStyles.HexNumber), Int.Parse(colorcode.Substring(2, 2), NumberStyles.HexNumber), Int.Parse(colorcode.Substring(0, 2), NumberStyles.HexNumber), Int.Parse(colorcode.Substring(4,2), NumberStyles.HexNumber)) Int.Parse(colorcode.Substring(2,2), NumberStyles.HexNumber), Int.Parse(colorcode.Substring(0,2), NumberStyles.HexNumber), If you need to use 4 values instead of a single integer, you can use this (combining several comments): string colorcode = "#FFFFFF00" Ĭolor col // from System.Drawing or Ĭol = Color.FromArgb(255, // hardcoded opaque The colorcode is just the hexadecimal representation of the ARGB value. Int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber) If you don't want to use the ColorTranslator, you can do it in easily: string colorcode = "#FFFFFF00" ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |