Silverlight中将字符串转换成Color类型

时间:2022-12-04 19:35:08

方法:

/// <summary>
        /// 将字符串转换成Color类型
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static Color ReturnColorFromString(string color)
        {
            color = color.Substring(1, color.Length-1);
            string alpha = color.Substring(0, 2);
            string red = color.Substring(2, 2);
            string green = color.Substring(4, 2);
            string blue = color.Substring(6, 2);

            byte alphaByte = Convert.ToByte(alpha, 16);
            byte redByte = Convert.ToByte(red, 16);
            byte greenByte = Convert.ToByte(green, 16);
            byte blueByte = Convert.ToByte(blue, 16);
            return Color.FromArgb(alphaByte, redByte, greenByte, blueByte);
        }