-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorConverter.cs
30 lines (25 loc) · 961 Bytes
/
ColorConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SFML.Graphics;
public class ColorConverter : JsonConverter<Color>
{
public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
var array = (JArray)token;
if (array.Count != 3)
throw new JsonSerializationException("Invalid color format. RGB values must be provided as an array of three integers.");
var r = array[0].Value<byte>();
var g = array[1].Value<byte>();
var b = array[2].Value<byte>();
return new Color(r, g, b);
}
public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer)
{
writer.WriteStartArray();
writer.WriteValue(value.R);
writer.WriteValue(value.G);
writer.WriteValue(value.B);
writer.WriteEndArray();
}
}