diff --git a/Dashboard.Common/Gradient.cs b/Dashboard.Common/Gradient.cs index aa1b447..f9f98bc 100644 --- a/Dashboard.Common/Gradient.cs +++ b/Dashboard.Common/Gradient.cs @@ -30,7 +30,7 @@ namespace Dashboard /// /// Represents a linear gradient. /// - public struct Gradient : ICollection + public struct Gradient : ICollection, ICloneable { private readonly List _stops = new List(); @@ -79,6 +79,16 @@ namespace Dashboard Add(new GradientStop(1, b)); } + public Gradient(IEnumerable stops) + { + _stops.AddRange(stops); + + if (_stops.Any(x => x.Position < 0 || x.Position > 1)) + throw new Exception("Gradient stop positions must be in the range [0, 1]."); + + _stops.Sort((a, b) => a.Position.CompareTo(b.Position)); + } + public Color GetColor(float position) { if (Count == 0) @@ -112,6 +122,28 @@ namespace Dashboard return Color.FromArgb((byte)color.W, (byte)color.X, (byte)color.Y, (byte)color.Z); } + public Gradient Clone() + { + Gradient gradient = new Gradient() + { + Type = Type, + C0 = C0, + C1 = C1, + }; + + foreach (GradientStop stop in _stops) + { + gradient.Add(stop); + } + + return gradient; + } + + object ICloneable.Clone() + { + return Clone(); + } + public IEnumerator GetEnumerator() { return _stops.GetEnumerator(); @@ -124,6 +156,9 @@ namespace Dashboard public void Add(GradientStop item) { + if (item.Position < 0 || item.Position > 1) + throw new Exception("Gradient stop positions must be in the range [0, 1]."); + int index = _stops.FindIndex(x => x.Position > item.Position); if (index == -1) index = _stops.Count;