AltitudeAngelWings.DrawingExtensions.ToARGB C# (CSharp) Method

ToARGB() public static method

public static ToARGB ( string color, string opacity = null ) : uint
color string
opacity string
return uint
        public static uint ToARGB(string color, string opacity = null)
        {
            byte opacityByte;
            uint colorARGB = 0x00;

            if (color.StartsWith("#"))
            {
                color = color.Substring(1);
            }
            else if (color.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
            {
                color = color.Substring(2);
            }

            if (color.Length == 6)
            {
                float opacityAmount;
                if (!float.TryParse(opacity, out opacityAmount))
                {
                    opacityAmount = 1F;
                }

                opacityByte = (byte)(opacityAmount * 100);
            }
            else if (color.Length == 8)
            {
                if (opacity != null)
                {
                    throw new InvalidOperationException("Color cannot contain an ARGB value if the opacity is specified.");
                }

                if (!byte.TryParse(color.Substring(0, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out opacityByte))
                {
                    opacityByte = 0xFF;
                }

                color = color.Substring(2);
            }
            else
            {
                throw new ArgumentException(nameof(color), "Color must be either #AARRGGBB, #RRGGBB, 0xAARRGGBB or 0xRRGGBB");
            }

            colorARGB = (uint)opacityByte << 24;

            for (int i = 0; i < 6; i += 2)
            {
                byte colorByte;
                byte.TryParse(color.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out colorByte);
                colorARGB |= (uint)colorByte << (((4 - i) / 2) * 8);
            }

            return colorARGB;
        }
    }
DrawingExtensions