From 516dfa48adbcbec0b2dc55dd33e1f96b89e2672a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Wed, 1 Nov 2017 11:30:21 +0100 Subject: [PATCH] Use Dictionary to store color names --- Tests/BasicTests.cs | 4 ++-- Tests/Showcase.cs | 4 ++-- src/Colors.cs | 24 ++++++++---------------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/Tests/BasicTests.cs b/Tests/BasicTests.cs index 9340fe88..8d0a8026 100644 --- a/Tests/BasicTests.cs +++ b/Tests/BasicTests.cs @@ -98,7 +98,7 @@ namespace Tests } get { return List2; } } - IList testList = Color.ColorDic.ToList(); + IList testList = Color.ColorDic.Values.ToList(); public IList TestList { set{ testList = value; @@ -131,7 +131,7 @@ namespace Tests void OnClear (object sender, MouseButtonEventArgs e) => TestList = null; - void OnLoadList (object sender, MouseButtonEventArgs e) => TestList = Color.ColorDic.ToList(); + void OnLoadList (object sender, MouseButtonEventArgs e) => TestList = Color.ColorDic.Values.ToList(); protected override void OnLoad (EventArgs e) { diff --git a/Tests/Showcase.cs b/Tests/Showcase.cs index 0d68d383..2639b0f7 100644 --- a/Tests/Showcase.cs +++ b/Tests/Showcase.cs @@ -160,7 +160,7 @@ namespace Tests } get { return List2; } } - IList testList = Color.ColorDic.ToList(); + IList testList = Color.ColorDic.Values.ToList(); public IList TestList { set{ testList = value; @@ -193,7 +193,7 @@ namespace Tests void OnClear (object sender, MouseButtonEventArgs e) => TestList = null; - void OnLoadList (object sender, MouseButtonEventArgs e) => TestList = Color.ColorDic.ToList(); + void OnLoadList (object sender, MouseButtonEventArgs e) => TestList = Color.ColorDic.Values.ToList(); } diff --git a/src/Colors.cs b/src/Colors.cs index 3f200829..7317516d 100644 --- a/src/Colors.cs +++ b/src/Colors.cs @@ -59,11 +59,11 @@ namespace Crow G = _G; B = _B; Name = _name; - ColorDic.Add(this); + ColorDic.Add(_name,this); } #endregion - public static List ColorDic = new List(); + public static Dictionary ColorDic = new Dictionary(); internal string Name; @@ -88,11 +88,9 @@ namespace Crow if (c.Length == 1) { - foreach (Color cr in ColorDic) - { - if (string.Equals(cr.Name,s,StringComparison.Ordinal)) - return cr; - } + if (ColorDic.ContainsKey (s)) + return ColorDic[s]; + throw new Exception ("Unknown color name: " + s); } return new Color( double.Parse(c[0]), @@ -1126,15 +1124,9 @@ namespace Crow { if (!string.IsNullOrEmpty(Name)) return Name; - - foreach (Color cr in ColorDic) - { - if (cr == this) - { - Name = cr.Name; - return cr.Name; - } - } + Color tc = this; + if (ColorDic.ContainsValue (this)) + return ColorDic.FirstOrDefault (c => c.Value == tc).Key; return string.Format("{0},{1},{2},{3}", R, G, B, A); } -- 2.47.3