1.6 DitherAlgorithm value type

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-07 14:10:43 +02:00
parent fa0081b923
commit 1b236e03af
3 changed files with 98 additions and 1 deletions

View File

@@ -0,0 +1,40 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace FrameProcessor.Domain;
[JsonConverter(typeof(DitherAlgorithmJsonConverter))]
public enum DitherAlgorithm
{
FloydSteinberg,
Atkinson,
Stucki,
Jarvis,
}
internal sealed class DitherAlgorithmJsonConverter : JsonConverter<DitherAlgorithm>
{
public override DitherAlgorithm Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return s switch
{
"floyd-steinberg" => DitherAlgorithm.FloydSteinberg,
"atkinson" => DitherAlgorithm.Atkinson,
"stucki" => DitherAlgorithm.Stucki,
"jarvis" => DitherAlgorithm.Jarvis,
_ => throw new JsonException(
$"'{s}' is not a valid dither algorithm. Expected 'floyd-steinberg', 'atkinson', 'stucki', or 'jarvis'."),
};
}
public override void Write(Utf8JsonWriter writer, DitherAlgorithm value, JsonSerializerOptions options)
=> writer.WriteStringValue(value switch
{
DitherAlgorithm.FloydSteinberg => "floyd-steinberg",
DitherAlgorithm.Atkinson => "atkinson",
DitherAlgorithm.Stucki => "stucki",
DitherAlgorithm.Jarvis => "jarvis",
_ => throw new JsonException($"Unknown dither algorithm: {value}."),
});
}