ecere/sys/JSON: add ColorAlpha de/stringification into JSON parser/writer.
authorRejean Loyer <redj@ecere.com>
Tue, 24 Mar 2015 17:00:32 +0000 (13:00 -0400)
committerJerome St-Louis <jerome@ecere.com>
Thu, 15 Oct 2015 00:19:38 +0000 (20:19 -0400)
ecere/src/sys/JSON.ec

index 08b87d1..efd77f5 100644 (file)
@@ -76,6 +76,10 @@ public:
                result = success;
                delete string;
             }
+            else if(type && eClass_IsDerived(type, class(ColorAlpha)))
+            {
+               result = GetColorAlpha(string, value);
+            }
             else if(type && (type.type == structClass))
             {
                if(((bool (*)(void *, void *, const char *))(void *)type._vTbl[__ecereVMethodID_class_OnGetDataFromString])(type, value.p, string))
@@ -741,6 +745,38 @@ public:
       }
       return result;
    }
+
+   JSONResult GetColorAlpha(String string, DataValue value)
+   {
+      ColorAlpha color = 0;
+      DefinedColor c = 0;
+      if(string)
+      {
+         if(string[0] == '0' && string[1] == 'x')
+            color = (uint)strtoul(string, null, 0);
+         else
+         {
+            char *d;
+            byte a = 255;
+            if((d = strchr(string, ',')))
+            {
+               a = (byte)atoi(string);
+               d += 2;
+            }
+            else
+               d = string;
+            if(c.class::OnGetDataFromString(d))
+            {
+               color.a = a;
+               color.color = c;
+            }
+            else
+               color = (uint)strtoul(string, null, 16);
+         }
+      }
+      value.i = color;
+      return success;
+   }
 }
 
 bool WriteMap(File f, Class type, Map map, int indent)
@@ -882,6 +918,32 @@ bool WriteNumber(File f, Class type, DataValue value, int indent)
    return true;
 }
 
+public bool WriteColorAlpha(File f, Class type, DataValue value, int indent)
+{
+   char buffer[1024];
+   char * string = buffer;
+   ColorAlpha color = value.i;
+   int a = color.a;
+   int len;
+   DefinedColor c = color;
+   buffer[0] = '\0';
+   if(a != 255)
+   {
+      a.class::OnGetString(buffer, null, null);
+      len = strlen(buffer);
+      buffer[len++] = ',';
+      buffer[len++] = ' ';
+      buffer[len] = '\0';
+      string += len;
+   }
+   if(!c.class::OnGetString(string, null, null))
+      sprintf(buffer, "0x%x", color);
+   f.Puts("\"");
+   f.Puts(buffer);
+   f.Puts("\"");
+   return true;
+}
+
 bool WriteValue(File f, Class type, DataValue value, int indent)
 {
    if(!strcmp(type.name, "String") || !strcmp(type.dataTypeString, "char *"))
@@ -966,6 +1028,10 @@ bool WriteValue(File f, Class type, DataValue value, int indent)
    {
       _WriteJSONObject(f, type, value.p, indent);
    }
+   else if(eClass_IsDerived(type, class(ColorAlpha)))
+   {
+      WriteColorAlpha(f, type, value, indent);
+   }
    else if(type.type == bitClass)
    {
       Class dataType;