首页 > 代码库 > 颜色转换

颜色转换

Color --->string(十六进制)

        public string GetHexByArgb(Color color)        {            string rtn = string.Empty;            Color tmpColor = Color.FromArgb(color.A, color.R, color.G, color.B);            rtn = "#" + Convert.ToString(tmpColor.ToArgb(), 16);            rtn = rtn.PadRight(9, '0');            return rtn;        }


string ---> Color

        public Color GetColor(string strArgb)        {            int argb;            Color color = Color.Empty;            if (string.IsNullOrEmpty(strArgb) == false)            {                if (int.TryParse(strArgb.Remove(0, 1), NumberStyles.HexNumber, null, out argb) == true)                {                    color = Color.FromArgb(argb);                }            }            return color;        }


Silverlight中颜色转换

        public SolidColorBrush ConvertColor(string color)        {            if (!string.IsNullOrEmpty(color) && color.Contains("#"))            {                if (color.StartsWith("#"))                {                    color = color.Replace("#", string.Empty);                }                int v = int.Parse(color, System.Globalization.NumberStyles.HexNumber);                return new SolidColorBrush(new Color()                {                    A = Convert.ToByte((v >> 24) & 255),                    R = Convert.ToByte((v >> 16) & 255),                    G = Convert.ToByte((v >> 8) & 255),                    B = Convert.ToByte((v >> 0) & 255)                });            }            return null;        }