diff --git a/PROMS/VEPROMS.CSLA.Library/Config/ColorConfig.cs b/PROMS/VEPROMS.CSLA.Library/Config/ColorConfig.cs new file mode 100644 index 00000000..f23fb4c4 --- /dev/null +++ b/PROMS/VEPROMS.CSLA.Library/Config/ColorConfig.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; +using System.Drawing; + +namespace VEPROMS.CSLA.Library +{ + public static class ColorConfig + { + private static Regex byARGB = new Regex(@"Color \[A=([0-9]*), R=([0-9]*), G=([0-9]*), B=([0-9]*)\]"); + private static Regex byName = new Regex(@"Color \[(.*)\]"); + private static Regex byRGB = new Regex(@"[0-9]*,[0-9]*,[0-9]*"); + public static Color ColorFromString(string sColor) + { + Match myMatch = byARGB.Match(sColor); // New Format by ARGB + if (myMatch.Groups.Count == 5) + return FindKnownColor(Color.FromArgb(int.Parse(myMatch.Groups[1].Value), int.Parse(myMatch.Groups[2].Value), int.Parse(myMatch.Groups[3].Value), int.Parse(myMatch.Groups[4].Value))); + myMatch = byName.Match(sColor); // New Format by Name + if (myMatch.Groups.Count == 2) + return Color.FromName(myMatch.Groups[1].Value); + myMatch = byRGB.Match(sColor); // Old Format by RGB + if (sColor[0] == '[') // Old Format by ARGB + { + string[] parts = sColor.Substring(1, sColor.Length - 2).Split(",".ToCharArray()); + return FindKnownColor(Color.FromArgb(Int32.Parse(parts[0]), Int32.Parse(parts[1]), Int32.Parse(parts[2]), Int32.Parse(parts[3]))); + } + if (myMatch.Groups.Count == 4) + return FindKnownColor(Color.FromArgb(int.Parse(myMatch.Groups[1].Value), int.Parse(myMatch.Groups[2].Value), int.Parse(myMatch.Groups[3].Value))); + return Color.FromName(sColor); // Old Format by Name + } + private static Dictionary _ConvertRGBToName; + public static Dictionary ConvertRGBToName + { + get + { + if (_ConvertRGBToName == null) + { + _ConvertRGBToName = new Dictionary(); + foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor))) + { + Color tmp1 = Color.FromKnownColor(knownColor); + if (!tmp1.IsSystemColor) + { + Color tmp2 = Color.FromArgb(tmp1.ToArgb()); + if (!_ConvertRGBToName.ContainsKey(tmp2.Name)) // Use first name + _ConvertRGBToName.Add(tmp2.Name, tmp1); + } + } + } + return _ConvertRGBToName; + } + } + public static Color FindKnownColor(int r, int g, int b) + { + Color tmp = Color.FromArgb(r, g, b); + if (ConvertRGBToName.ContainsKey(tmp.Name)) + return ConvertRGBToName[tmp.Name]; + return tmp; + } + public static Color FindKnownColor(Color tmp) + { + if (ConvertRGBToName.ContainsKey(tmp.Name)) + return ConvertRGBToName[tmp.Name]; + return tmp; + } + + } +}