2017-06-18 19:29:23 +00:00
|
|
|
using System;
|
2017-11-10 19:32:51 +00:00
|
|
|
using Newtonsoft.Json;
|
2017-11-09 09:38:19 +00:00
|
|
|
using StyleValue = System.Object;
|
|
|
|
|
2017-06-18 19:29:23 +00:00
|
|
|
namespace Ooui
|
|
|
|
{
|
2017-11-10 19:32:51 +00:00
|
|
|
[Newtonsoft.Json.JsonConverter (typeof (ColorJsonConverter))]
|
|
|
|
public struct Color : IEquatable<Color>
|
2017-06-18 19:29:23 +00:00
|
|
|
{
|
|
|
|
public byte R, G, B, A;
|
|
|
|
|
2017-11-10 00:09:48 +00:00
|
|
|
public Color (byte r, byte g, byte b, byte a)
|
2017-11-10 19:32:51 +00:00
|
|
|
{
|
|
|
|
R = r;
|
|
|
|
G = g;
|
|
|
|
B = b;
|
|
|
|
A = a;
|
|
|
|
}
|
2017-11-09 09:38:19 +00:00
|
|
|
|
2017-06-18 19:29:23 +00:00
|
|
|
public double Red {
|
|
|
|
get => R / 255.0;
|
|
|
|
set => R = value >= 1.0 ? (byte)255 : ((value <= 0.0) ? (byte)0 : (byte)(value * 255.0 + 0.5));
|
|
|
|
}
|
|
|
|
public double Green {
|
|
|
|
get => G / 255.0;
|
|
|
|
set => G = value >= 1.0 ? (byte)255 : ((value <= 0.0) ? (byte)0 : (byte)(value * 255.0 + 0.5));
|
|
|
|
}
|
|
|
|
public double Blue {
|
|
|
|
get => B / 255.0;
|
|
|
|
set => B = value >= 1.0 ? (byte)255 : ((value <= 0.0) ? (byte)0 : (byte)(value * 255.0 + 0.5));
|
|
|
|
}
|
|
|
|
public double Alpha {
|
|
|
|
get => A / 255.0;
|
|
|
|
set => A = value >= 1.0 ? (byte)255 : ((value <= 0.0) ? (byte)0 : (byte)(value * 255.0 + 0.5));
|
|
|
|
}
|
2017-11-09 09:38:19 +00:00
|
|
|
|
2017-11-10 19:32:51 +00:00
|
|
|
public override bool Equals (object obj)
|
|
|
|
{
|
|
|
|
if (obj is Color other)
|
|
|
|
return R == other.R && G == other.G && B == other.B && A == other.A;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals (Color other) => R == other.R && G == other.G && B == other.B && A == other.A;
|
|
|
|
|
|
|
|
public override int GetHashCode () => R.GetHashCode () + G.GetHashCode () * 2 + B.GetHashCode () * 3 + A.GetHashCode () * 5;
|
|
|
|
|
|
|
|
public static Color FromStyleValue (StyleValue styleColor)
|
|
|
|
{
|
|
|
|
if (styleColor is Color c)
|
|
|
|
return c;
|
|
|
|
if (styleColor is string s)
|
|
|
|
return Parse (s);
|
2017-11-10 00:09:48 +00:00
|
|
|
return Colors.Clear;
|
2017-11-10 19:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Color Parse (string styleValue)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace (styleValue) || styleValue.Length < 4)
|
|
|
|
throw new ArgumentException ("Cannot parse empty strings", nameof (styleValue));
|
|
|
|
|
|
|
|
if (styleValue.Length > 32)
|
|
|
|
throw new ArgumentException ("Color string is too long", nameof (styleValue));
|
|
|
|
|
|
|
|
if (styleValue == "inherit")
|
|
|
|
return Colors.Clear;
|
|
|
|
|
|
|
|
//if (styleValue[0] == '#' && styleValue.Length == 4) {
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if (styleValue[0] == '#' && styleValue.Length == 7) {
|
|
|
|
//}
|
|
|
|
|
|
|
|
throw new ArgumentException ($"Cannot parse color string `{styleValue}`", nameof (styleValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString ()
|
|
|
|
{
|
|
|
|
if (A == 255)
|
|
|
|
return string.Format ("#{0:x2}{1:x2}{2:x2}", R, G, B);
|
|
|
|
return string.Format ("rgba({0},{1},{2},{3})", R, G, B, A / 255.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ColorJsonConverter : Newtonsoft.Json.JsonConverter
|
|
|
|
{
|
|
|
|
public override bool CanConvert (Type objectType)
|
|
|
|
{
|
|
|
|
return objectType == typeof (Color);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object ReadJson (JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
|
|
{
|
|
|
|
var str = reader.ReadAsString ();
|
|
|
|
return Color.Parse (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
|
|
|
|
{
|
|
|
|
writer.WriteValue (value.ToString ());
|
|
|
|
}
|
|
|
|
}
|
2017-06-18 19:29:23 +00:00
|
|
|
}
|