Skip to content

Commit cb50cce

Browse files
committed
Added Color and Color32 support
1 parent a5df8b8 commit cb50cce

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

SimpleJSONUnity.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public partial class JSONNode
4949
public static JSONContainerType VectorContainerType = JSONContainerType.Array;
5050
public static JSONContainerType QuaternionContainerType = JSONContainerType.Array;
5151
public static JSONContainerType RectContainerType = JSONContainerType.Array;
52+
public static JSONContainerType ColorContainerType = JSONContainerType.Array;
5253
private static JSONNode GetContainer(JSONContainerType aType)
5354
{
5455
if (aType == JSONContainerType.Array)
@@ -75,6 +76,18 @@ public static implicit operator JSONNode(Vector4 aVec)
7576
n.WriteVector4(aVec);
7677
return n;
7778
}
79+
public static implicit operator JSONNode(Color aCol)
80+
{
81+
JSONNode n = GetContainer(ColorContainerType);
82+
n.WriteColor(aCol);
83+
return n;
84+
}
85+
public static implicit operator JSONNode(Color32 aCol)
86+
{
87+
JSONNode n = GetContainer(ColorContainerType);
88+
n.WriteColor32(aCol);
89+
return n;
90+
}
7891
public static implicit operator JSONNode(Quaternion aRot)
7992
{
8093
JSONNode n = GetContainer(QuaternionContainerType);
@@ -106,6 +119,14 @@ public static implicit operator Vector4(JSONNode aNode)
106119
{
107120
return aNode.ReadVector4();
108121
}
122+
public static implicit operator Color(JSONNode aNode)
123+
{
124+
return aNode.ReadColor();
125+
}
126+
public static implicit operator Color32(JSONNode aNode)
127+
{
128+
return aNode.ReadColor32();
129+
}
109130
public static implicit operator Quaternion(JSONNode aNode)
110131
{
111132
return aNode.ReadQuaternion();
@@ -234,6 +255,75 @@ public JSONNode WriteVector4(Vector4 aVec)
234255
}
235256
#endregion Vector4
236257

258+
#region Color / Color32
259+
public Color ReadColor(Color aDefault)
260+
{
261+
if (IsObject)
262+
return new Color(this["a"].AsFloat, this["r"].AsFloat, this["g"].AsFloat, this["b"].AsFloat);
263+
if (IsArray)
264+
return new Color(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
265+
return aDefault;
266+
}
267+
public Color ReadColor()
268+
{
269+
return ReadColor(Color.clear);
270+
}
271+
public JSONNode WriteColor(Color aCol)
272+
{
273+
if (IsObject)
274+
{
275+
Inline = true;
276+
this["r"].AsFloat = aCol.r;
277+
this["g"].AsFloat = aCol.g;
278+
this["b"].AsFloat = aCol.b;
279+
this["a"].AsFloat = aCol.a;
280+
}
281+
else if (IsArray)
282+
{
283+
Inline = true;
284+
this[0].AsFloat = aCol.r;
285+
this[1].AsFloat = aCol.g;
286+
this[2].AsFloat = aCol.b;
287+
this[3].AsFloat = aCol.a;
288+
}
289+
return this;
290+
}
291+
292+
public Color32 ReadColor32(Color32 aDefault)
293+
{
294+
if (IsObject)
295+
return new Color32((byte)this["r"].AsInt, (byte)this["g"].AsInt, (byte)this["b"].AsInt, (byte)this["a"].AsInt);
296+
if (IsArray)
297+
return new Color32((byte)this[0].AsInt, (byte)this[1].AsInt, (byte)this[2].AsInt, (byte)this[3].AsInt);
298+
return aDefault;
299+
}
300+
public Color32 ReadColor32()
301+
{
302+
return ReadColor32(new Color32());
303+
}
304+
public JSONNode WriteColor32(Color32 aCol)
305+
{
306+
if (IsObject)
307+
{
308+
Inline = true;
309+
this["r"].AsInt = aCol.r;
310+
this["g"].AsInt = aCol.g;
311+
this["b"].AsInt = aCol.b;
312+
this["a"].AsInt = aCol.a;
313+
}
314+
else if (IsArray)
315+
{
316+
Inline = true;
317+
this[0].AsInt = aCol.r;
318+
this[1].AsInt = aCol.g;
319+
this[2].AsInt = aCol.b;
320+
this[3].AsInt = aCol.a;
321+
}
322+
return this;
323+
}
324+
325+
#endregion Color / Color32
326+
237327
#region Quaternion
238328
public Quaternion ReadQuaternion(Quaternion aDefault)
239329
{

0 commit comments

Comments
 (0)