using System; using System.Collections.Generic; using Amphora; namespace Prism.Glass { /// /// A gradient being edited: its stops, the space they blend in, and which stop is selected. The stops are kept in /// order of offset, so moving one past another changes its index. Each stop's color is in the gradient's space, at /// that space's precision, and the gradient it builds holds them as they are. /// public class GradientModel { private struct Stop { public double Offset; public StopColor Color; } private readonly List stops = new List(); private GradientSpace space; private HueDirection direction; private int selectedIndex; /// /// Gets the gradient as edited so far. It is replaced by a new one each time the model changes. /// public Gradient Gradient { get; private set; } /// /// Raised with the new gradient each time a stop is added, removed, moved or recolored, or the space or hue /// direction changes. It comes before when an edit does both. /// public event Action Changed; /// /// Raised with the new index when the selected stop is a different one, or has moved to a different index. /// public event Action SelectionChanged; /// /// Gets or sets what is done with each gradient an edit would make, before the model takes it. A failed status /// turns the edit down and changes nothing. Loading a gradient doesn't ask. /// public Func Apply { get; set; } /// /// Makes a model of the gradient, with its first stop selected. /// public GradientModel(Gradient gradient) { if (gradient == null) { throw new ArgumentNullException(nameof(gradient)); } Fill(gradient); } private void Fill(Gradient gradient) { space = gradient.Space; direction = gradient.Direction; Gradient = gradient; selectedIndex = 0; stops.Clear(); for (int i = 0; i < gradient.StopCount; ++i) { ExactStop stop = gradient.GetExactStop(i); stops.Add(new Stop { Offset = stop.Offset, Color = stop.Color }); } } /// /// Replaces everything the model holds with another gradient's stops, space and direction, and selects the /// first stop. is raised, then , even when the gradient /// looks the same as before. /// /// The gradient is null. public void Load(Gradient gradient) { if (gradient == null) { throw new ArgumentNullException(nameof(gradient)); } Fill(gradient); Changed?.Invoke(Gradient); SelectionChanged?.Invoke(selectedIndex); } public int StopCount => stops.Count; /// /// Gets the index of the stop the color picker is editing. /// public int SelectedIndex => selectedIndex; // The gradient an edit would make, if what is applying it takes it. private StatusOr Build(List edited, GradientSpace editedSpace, HueDirection editedDirection) { var built = new ExactStop[edited.Count]; for (int i = 0; i < built.Length; ++i) { built[i] = new ExactStop(edited[i].Offset, edited[i].Color); } var returnMe = new Gradient(editedSpace, editedDirection, built); Status applied = Apply == null ? Status.Ok : Apply(returnMe); if (!applied.IsOk) { return applied; } return returnMe; } private void Replace( Gradient built, List edited, GradientSpace editedSpace, HueDirection editedDirection) { stops.Clear(); stops.AddRange(edited); space = editedSpace; direction = editedDirection; Gradient = built; Changed?.Invoke(Gradient); } /// /// Gets the space the stops blend in. changes it. /// public GradientSpace Space => space; /// /// Gets which way a blend goes round the hue circle. changes it. /// public HueDirection Direction => direction; /// /// Sets the space the stops blend in, which puts every stop's color in the new space and can round or clamp it. /// It is refused, changing nothing, with 's status when that turns the gradient down. /// /// The space isn't a defined one. public Status SetSpace(GradientSpace value) { if (!Enum.IsDefined(typeof(GradientSpace), value)) { throw new ArgumentOutOfRangeException(nameof(value), value, "Not a gradient space."); } if (value == space) { return Status.Ok; } var edited = new List(stops.Count); for (int i = 0; i < stops.Count; ++i) { edited.Add(new Stop { Offset = stops[i].Offset, Color = stops[i].Color.ConvertTo(value) }); } StatusOr built = Build(edited, value, direction); if (!built.IsOk) { return built.Status; } Replace(built.Value, edited, value, direction); return Status.Ok; } /// /// Sets which way a blend goes round the hue circle. It is refused, changing nothing, with 's status when that turns the gradient down. /// /// The direction isn't a defined one. public Status SetDirection(HueDirection value) { if (!Enum.IsDefined(typeof(HueDirection), value)) { throw new ArgumentOutOfRangeException(nameof(value), value, "Not a hue direction."); } if (value == direction) { return Status.Ok; } var edited = new List(stops); StatusOr built = Build(edited, space, value); if (!built.IsOk) { return built.Status; } Replace(built.Value, edited, space, value); return Status.Ok; } private void CheckIndex(int index) { if (index < 0 || index >= stops.Count) { throw new ArgumentOutOfRangeException(nameof(index), index, "Not a stop of this gradient."); } } // After the stops that share its offset, so it is the later color at a hard edge. private static int InsertAfterEquals(List list, Stop stop) { int returnMe = 0; while (returnMe < list.Count && list[returnMe].Offset <= stop.Offset) { ++returnMe; } list.Insert(returnMe, stop); return returnMe; } /// /// Returns where a stop is along the gradient, from 0 to 1. The stops are in order of offset. /// /// The index is not a stop. public double GetOffset(int index) { CheckIndex(index); return stops[index].Offset; } /// /// Returns a stop's color, in the model's space. /// /// The index is not a stop. public StopColor GetColor(int index) { CheckIndex(index); return stops[index].Color; } /// /// Selects a stop. Selecting the one already selected changes nothing. /// /// The index is not a stop. public void Select(int index) { CheckIndex(index); if (index != selectedIndex) { selectedIndex = index; SelectionChanged?.Invoke(selectedIndex); } } /// /// Adds a stop at this offset, held to 0 through 1, in the color the gradient has there. Blending in a hue /// space the long way round can make the halves take different arcs. It goes after any stop with the same /// offset, and is selected. Returns its index, or 's status and changes nothing when that /// turns the gradient down. /// public StatusOr AddStop(double offset) { double clamped = Gradient.ClampToUnit(offset); StopColor color = StopColor.FromSrgb(space, Gradient.EvaluateExact(clamped)); var edited = new List(stops); int index = InsertAfterEquals(edited, new Stop { Offset = clamped, Color = color }); StatusOr built = Build(edited, space, direction); if (!built.IsOk) { return built.Status; } selectedIndex = index; Replace(built.Value, edited, space, direction); SelectionChanged?.Invoke(selectedIndex); return index; } /// /// Removes a stop. The last stop stays, since a gradient needs one, and comes back. The selection stays on the stop it was on, or on the one /// that takes the removed stop's place if that was it. 's status comes back, and nothing /// changes, when that turns the gradient down. /// /// The index is not a stop. public Status RemoveStop(int index) { CheckIndex(index); if (stops.Count == 1) { return GradientFailures.NeedsAStop.Fail(); } var edited = new List(stops); edited.RemoveAt(index); StatusOr built = Build(edited, space, direction); if (!built.IsOk) { return built.Status; } bool selectionMoved = index <= selectedIndex; if (index < selectedIndex) { --selectedIndex; } else if (index == selectedIndex) { selectedIndex = Math.Min(selectedIndex, edited.Count - 1); } Replace(built.Value, edited, space, direction); if (selectionMoved) { SelectionChanged?.Invoke(selectedIndex); } return Status.Ok; } /// /// Moves a stop to this offset, held to 0 through 1, after any stop with the same offset. The selection stays /// on the stop it was on. Returns the stop's new index, which differs when it passes another, or 's status and changes nothing when that turns the gradient down. /// /// The index is not a stop. public StatusOr MoveStop(int index, double offset) { CheckIndex(index); double clamped = Gradient.ClampToUnit(offset); if (stops[index].Offset == clamped) { return index; } var edited = new List(stops); edited.RemoveAt(index); int returnMe = InsertAfterEquals(edited, new Stop { Offset = clamped, Color = stops[index].Color }); StatusOr built = Build(edited, space, direction); if (!built.IsOk) { return built.Status; } int selectedBefore = selectedIndex; if (selectedIndex == index) { selectedIndex = returnMe; } else if (index < selectedIndex && selectedIndex <= returnMe) { --selectedIndex; } else if (returnMe <= selectedIndex && selectedIndex < index) { ++selectedIndex; } Replace(built.Value, edited, space, direction); if (selectedIndex != selectedBefore) { SelectionChanged?.Invoke(selectedIndex); } return returnMe; } /// /// Gives a stop a color, which is put in the model's space if it isn't in it. The color it already has changes /// nothing. 's status comes back, and nothing changes, when that turns the gradient down. /// /// The index is not a stop. public Status SetStopColor(int index, StopColor color) { CheckIndex(index); StopColor inSpace = color.ConvertTo(space); if (stops[index].Color == inSpace) { return Status.Ok; } var edited = new List(stops); edited[index] = new Stop { Offset = stops[index].Offset, Color = inSpace }; StatusOr built = Build(edited, space, direction); if (!built.IsOk) { return built.Status; } Replace(built.Value, edited, space, direction); return Status.Ok; } } }