Eris 3 years ago
parent
commit
740bc97e91
1 changed files with 37 additions and 0 deletions
  1. +37
    -0
      IPA.Loader/Config/Stores/Converters.cs

+ 37
- 0
IPA.Loader/Config/Stores/Converters.cs View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Boolean = IPA.Config.Data.Boolean;
namespace IPA.Config.Stores.Converters
@ -483,6 +484,42 @@ namespace IPA.Config.Stores.Converters
public IReadOnlyDictionaryConverter() : base(new TConverter()) { }
}
#endif
/// <summary>
/// A converter for <see cref="Color"/> objects.
/// </summary>
public sealed class HexColorConverter : ValueConverter<Color>
{
/// <summary>
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding <see cref="Color" /> object.
/// </summary>
/// <param name="value">the <see cref="Value"/> to convert</param>
/// <param name="parent">the object which will own the created object</param>
/// <returns>the deserialized Color object</returns>
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node or couldn't be parsed into a Color object</exception>
public override Color FromValue(Value value, object parent)
{
if (value is Text t)
{
if (ColorUtility.TryParseHtmlString(t.Value, out Color color))
{
return color;
}
throw new ArgumentException("Value cannot be parsed into a Color.", nameof(value));
}
throw new ArgumentException("Value not a string", nameof(value));
}
/// <summary>
/// Converts color of type <see cref="Color"/> to a <see cref="Value"/> node.
/// </summary>
/// <param name="obj">the object to serialize</param>
/// <param name="parent">the object which owns <paramref name="obj"/></param>
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
public override Value ToValue(Color obj, object parent) => Value.Text($"#{ColorUtility.ToHtmlStringRGB(obj)}");
}
internal class StringConverter : ValueConverter<string>
{


Loading…
Cancel
Save