(adding unit tests to Typescript led to looking into some C# code, which led to the discovery that ColorTranslator is completely ignorant of alpha values - don't assume anything, test everything!) public static ColorUtils You may want to handle errors differently, I prefer to just return Color.Empty if the input cannot be parsed. I have expanded on Thomas's answer to cover the #RGBA/#RRGGBBAA formats. Throw new FormatException("Not rgb, rgba or hexa color string") Ĭolor c = ColorHelper.ParseColor("rgb(110,120,130)") Ĭolor c = ColorHelper.ParseColor("rgba(110,120,130,0.5)") Ĭolor c = ColorHelper.ParseColor("#192856") Return Color.FromArgb((int)(a * 255), r, g, b) Int b = int.Parse(parts, CultureInfo.InvariantCulture) įloat a = float.Parse(parts, CultureInfo.InvariantCulture) Int g = int.Parse(parts, CultureInfo.InvariantCulture) Int r = int.Parse(parts, CultureInfo.InvariantCulture) String noBrackets = cssColor.Substring(left + 1, right - left - 1) Throw new FormatException("rgba format error") Return ColorTranslator.FromHtml(cssColor) Įlse if (cssColor.StartsWith("rgb")) //rgb or argb Public static Color ParseColor(string cssColor) I'm more looking towards the solution of getting Hex value directly or if not possible then convert rgba to Hex.įollowing code cover hexadecimal format, rgb format and rgba format. Like my code gives me 0 for "a" but cssvalue is 1. String x = string.Format("rgba() ", r, g, b,a) īut this one is giving me value like, "rgba(25, 40, 86, 0) ". Int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber) I've also tried below code string colorcode = menuColor Is there any way i can convert it back to Hex ? or i can get value in Hex itself? How can i do that? quickLinkstab.GetCssValue("background-color")Ībove is givingm e value of "rgba(25, 40, 86, 1)" which is rgba value. Now i need to get values in hex values itself. but when i get the CSS property of that element it is giving me color in rgba format. In my selenium code i need to verify that color code is #192856 for background.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |