1234567891011121314151617181920212223242526272829303132333435363738 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Windows.UI;
- namespace KdanCommon.Helpers
- {
- public static class ColorCodeExtension
- {
- public static Color ToColor(this string hex)
- {
- byte a = 255;
- byte r = 0;
- byte g = 0;
- byte b = 0;
- if (hex.StartsWith("#"))
- {
- hex = hex.Replace("#", string.Empty);
- if (hex.Length == 8)
- {
- a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
- r = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
- g = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
- b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16));
- }
- else if (hex.Length == 6)
- {
- r = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
- g = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
- b = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
- }
- }
- return Color.FromArgb(a, r, g, b);
- }
- }
- }
|