From 8849ddec5487e24202a14af7731244b181a97ead Mon Sep 17 00:00:00 2001 From: ww-rm Date: Wed, 18 Jun 2025 01:14:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9Color=E7=9A=84?= =?UTF-8?q?=E5=AE=9A=E5=88=B6=E5=BA=8F=E5=88=97=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpineViewer/Utils/JsonHelper.cs | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/SpineViewer/Utils/JsonHelper.cs b/SpineViewer/Utils/JsonHelper.cs index 14fbd6f..dcf8a89 100644 --- a/SpineViewer/Utils/JsonHelper.cs +++ b/SpineViewer/Utils/JsonHelper.cs @@ -1,4 +1,5 @@ -using Microsoft.Win32; +using FFMpegCore; +using Microsoft.Win32; using NLog; using SpineViewer.Models; using SpineViewer.Services; @@ -9,7 +10,9 @@ using System.Linq; using System.Text; using System.Text.Encodings.Web; using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; +using System.Windows.Media; namespace SpineViewer.Utils { @@ -17,6 +20,11 @@ namespace SpineViewer.Utils { private static readonly Logger _logger = LogManager.GetCurrentClassLogger(); + static JsonHelper() + { + _jsonOptions.Converters.Add(new ColorJsonConverter()); + } + /// /// 保存 Json 文件的格式参数 /// @@ -85,4 +93,28 @@ namespace SpineViewer.Utils return true; } } + + public class ColorJsonConverter : JsonConverter + { + public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + // 解析 JSON 对象 + var jsonObject = JsonDocument.ParseValue(ref reader).RootElement; + var r = jsonObject.GetProperty("R").GetByte(); + var g = jsonObject.GetProperty("G").GetByte(); + var b = jsonObject.GetProperty("B").GetByte(); + var a = jsonObject.GetProperty("A").GetByte(); + return Color.FromArgb(a, r, g, b); + } + + public override void Write(Utf8JsonWriter writer, Color value, JsonSerializerOptions options) + { + writer.WriteStartObject(); + writer.WriteNumber("R", value.R); + writer.WriteNumber("G", value.G); + writer.WriteNumber("B", value.B); + writer.WriteNumber("A", value.A); + writer.WriteEndObject(); + } + } }