-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Kook; | ||
|
||
/// <summary> | ||
/// Represents the type of color a role has. | ||
/// </summary> | ||
public enum ColorType : ushort | ||
{ | ||
/// <summary> | ||
/// The color is a solid color. | ||
/// </summary> | ||
Solid = 1, | ||
/// <summary> | ||
/// The color is a gradient. | ||
/// </summary> | ||
Gradient = 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Kook; | ||
|
||
/// <summary> | ||
/// Represents a gradient color. | ||
/// </summary> | ||
public struct GradientColor | ||
{ | ||
public GradientColor(Color left, Color right) | ||
{ | ||
Left = left; | ||
Right = right; | ||
} | ||
|
||
/// <summary> | ||
/// The left color of the gradient. | ||
/// </summary> | ||
public Color Left { get; } | ||
|
||
/// <summary> | ||
/// The right color of the gradient. | ||
/// </summary> | ||
public Color Right { get; } | ||
|
||
public static implicit operator (Color Left, Color Right)(GradientColor gradient) => (gradient.Left, gradient.Right); | ||
public static implicit operator GradientColor((Color Left, Color Right) gradient) => new(gradient.Left, gradient.Right); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Kook.Net.Rest/Net/Converters/NullableGradientColorConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Kook.Net.Converters; | ||
|
||
internal class NullableGradientColorConverter : JsonConverter<GradientColor?> | ||
{ | ||
/// <inheritdoc /> | ||
public override GradientColor? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.StartObject) | ||
{ | ||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.PropertyName) | ||
throw new JsonException($"{nameof(NullableGradientColorConverter)} expects property name token, but got {reader.TokenType}"); | ||
string propertyName = reader.GetString(); | ||
if (propertyName != "color_list") | ||
throw new JsonException($"{nameof(NullableGradientColorConverter)} expects property name 'color_list', but got {propertyName}"); | ||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.StartArray) | ||
throw new JsonException($"{nameof(NullableGradientColorConverter)} expects start array token, but got {reader.TokenType}"); | ||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.Number) | ||
throw new JsonException($"{nameof(NullableGradientColorConverter)} expects number token, but got {reader.TokenType}"); | ||
var left = reader.GetUInt32(); | ||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.Number) | ||
throw new JsonException($"{nameof(NullableGradientColorConverter)} expects number token, but got {reader.TokenType}"); | ||
var right = reader.GetUInt32(); | ||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.EndArray) | ||
throw new JsonException($"{nameof(NullableGradientColorConverter)} expects end array token, but got {reader.TokenType}"); | ||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.EndObject) | ||
throw new JsonException($"{nameof(NullableGradientColorConverter)} expects end object token, but got {reader.TokenType}"); | ||
return new GradientColor(new Color(left), new Color(right)); | ||
} | ||
|
||
if (reader.TokenType == JsonTokenType.StartArray) | ||
{ | ||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.EndArray) | ||
throw new JsonException($"{nameof(NullableGradientColorConverter)} expects end array token, but got {reader.TokenType}"); | ||
return null; | ||
} | ||
|
||
throw new JsonException( | ||
$"{nameof(NullableGradientColorConverter)} expects start object or start array token, but got {reader.TokenType}"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, GradientColor? value, JsonSerializerOptions options) | ||
{ | ||
if (value.HasValue) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("color_list"); | ||
writer.WriteStartArray(); | ||
writer.WriteNumberValue(value.Value.Left); | ||
writer.WriteNumberValue(value.Value.Right); | ||
writer.WriteEndArray(); | ||
writer.WriteEndObject(); | ||
} | ||
else | ||
{ | ||
writer.WriteStartArray(); | ||
writer.WriteEndArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters