[longomatch] Add new Parse function to parse hex colors



commit b370762e4b9628531b1cd0dc49e2cda2765deae8
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Fri Oct 17 17:08:31 2014 +0200

    Add new Parse function to parse hex colors

 LongoMatch.Core/Common/Color.cs      |   20 ++++++++++++++++++++
 LongoMatch.Core/Common/Serializer.cs |    5 +----
 2 files changed, 21 insertions(+), 4 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Color.cs b/LongoMatch.Core/Common/Color.cs
index d2fd0b3..d2af3e7 100644
--- a/LongoMatch.Core/Common/Color.cs
+++ b/LongoMatch.Core/Common/Color.cs
@@ -17,6 +17,7 @@
 // 
 using System;
 using Newtonsoft.Json;
+using System.Globalization;
 
 namespace LongoMatch.Core.Common
 {
@@ -82,6 +83,25 @@ namespace LongoMatch.Core.Common
                        return new Color (UShortToByte (r), UShortToByte (g),
                                          UShortToByte (b), UShortToByte (a));
                }
+
+               static public Color Parse (string colorHex)
+               {
+                       byte r, g, b, a=Byte.MaxValue;
+                       
+                       if (!colorHex.StartsWith ("#")) {
+                               return null;
+                       }
+                       if (colorHex.Length != 7 && colorHex.Length != 9) {
+                               return null;
+                       }
+                       r = Byte.Parse (colorHex.Substring (1, 2), NumberStyles.HexNumber);
+                       g = Byte.Parse (colorHex.Substring (3, 2), NumberStyles.HexNumber);
+                       b = Byte.Parse (colorHex.Substring (5, 2), NumberStyles.HexNumber);
+                       if (colorHex.Length == 9) {
+                               a = Byte.Parse (colorHex.Substring (7, 2), NumberStyles.HexNumber);
+                       }
+                       return new Color (r, g, b, a);
+               }
                
                static public Color Black = new Color (0, 0, 0);
                static public Color White = new Color (255, 255, 255);
diff --git a/LongoMatch.Core/Common/Serializer.cs b/LongoMatch.Core/Common/Serializer.cs
index f42b668..5c5b169 100644
--- a/LongoMatch.Core/Common/Serializer.cs
+++ b/LongoMatch.Core/Common/Serializer.cs
@@ -162,10 +162,7 @@ namespace LongoMatch.Core.Common
                                        return new Time((int)t);
                                } else if (objectType == typeof (Color)) {
                                        string rgbStr = (string) reader.Value;
-                                       return new Color(Byte.Parse (rgbStr.Substring(1,2), 
NumberStyles.HexNumber),
-                                                        Byte.Parse (rgbStr.Substring(3,2), 
NumberStyles.HexNumber),
-                                                        Byte.Parse (rgbStr.Substring(5,2), 
NumberStyles.HexNumber),
-                                                        Byte.Parse (rgbStr.Substring(7,2), 
NumberStyles.HexNumber));
+                                       return Color.Parse (rgbStr);
                                } else if (objectType == typeof (Image)) {
                                        byte[] buf = Convert.FromBase64String ((string)reader.Value); 
                                        return Image.Deserialize (buf);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]