From 8faa6b7a492788dd1637c8cd72e6c7c0d75d6b66 Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 27 Feb 2022 23:06:58 +0100 Subject: [PATCH] Gradient picker - Added color randomization and condensed style --- .../Models/Profile/Colors/ColorGradient.cs | 126 ++++++++++-------- .../Controls/GradientPicker/GradientPicker.cs | 15 ++- .../Artemis.UI.Shared/Styles/Artemis.axaml | 11 +- .../Artemis.UI.Shared/Styles/Condensed.axaml | 4 + .../Styles/Controls/GradientPicker.axaml | 3 + .../Controls/GradientPickerButton.axaml | 16 ++- 6 files changed, 109 insertions(+), 66 deletions(-) diff --git a/src/Artemis.Core/Models/Profile/Colors/ColorGradient.cs b/src/Artemis.Core/Models/Profile/Colors/ColorGradient.cs index 173c1481e..81854dda5 100644 --- a/src/Artemis.Core/Models/Profile/Colors/ColorGradient.cs +++ b/src/Artemis.Core/Models/Profile/Colors/ColorGradient.cs @@ -13,52 +13,6 @@ namespace Artemis.Core; /// public class ColorGradient : IList, IList, INotifyCollectionChanged { - #region Equality members - - /// - /// Determines whether all the stops in this gradient are equal to the stops in the given gradient. - /// - /// The other gradient to compare to - protected bool Equals(ColorGradient other) - { - if (Count != other.Count) - return false; - - for (int i = 0; i < Count; i++) - { - if (!Equals(this[i], other[i])) - return false; - } - - return true; - } - - /// - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) - return false; - if (ReferenceEquals(this, obj)) - return true; - if (obj.GetType() != this.GetType()) - return false; - return Equals((ColorGradient) obj); - } - - /// - public override int GetHashCode() - { - unchecked - { - int hash = 19; - foreach (ColorGradientStop stops in this) - hash = hash * 31 + stops.GetHashCode(); - return hash; - } - } - - #endregion - private static readonly SKColor[] FastLedRainbow = { new(0xFFFF0000), // Red @@ -233,14 +187,7 @@ public class ColorGradient : IList, IList, INotifyCollectionC public ColorGradient GetRandom(int stops) { ColorGradient gradient = new(); - Random random = new(); - for (int index = 0; index < stops; index++) - { - SKColor skColor = SKColor.FromHsv(random.NextSingle() * 360, 100, 100); - float position = 1f / (stops - 1f) * index; - gradient.Add(new ColorGradientStop(skColor, position)); - } - + gradient.Randomize(stops); return gradient; } @@ -360,6 +307,32 @@ public class ColorGradient : IList, IList, INotifyCollectionC } } + /// + /// Randomizes the color gradient with the given amount of . + /// + /// The amount of stops to put into the gradient. + public void Randomize(int stops) + { + try + { + _updating = true; + + Clear(); + Random random = new(); + for (int index = 0; index < stops; index++) + { + SKColor skColor = SKColor.FromHsv(random.NextSingle() * 360, 100, 100); + float position = 1f / (stops - 1f) * index; + Add(new ColorGradientStop(skColor, position)); + } + } + finally + { + _updating = false; + Sort(); + } + } + /// /// Occurs when any of the stops has changed in some way /// @@ -396,6 +369,51 @@ public class ColorGradient : IList, IList, INotifyCollectionC StopChanged?.Invoke(this, EventArgs.Empty); } + #region Equality members + + /// + /// Determines whether all the stops in this gradient are equal to the stops in the given + /// gradient. + /// + /// The other gradient to compare to + protected bool Equals(ColorGradient other) + { + if (Count != other.Count) + return false; + + for (int i = 0; i < Count; i++) + if (!Equals(this[i], other[i])) + return false; + + return true; + } + + /// + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) + return false; + if (ReferenceEquals(this, obj)) + return true; + if (obj.GetType() != GetType()) + return false; + return Equals((ColorGradient) obj); + } + + /// + public override int GetHashCode() + { + unchecked + { + int hash = 19; + foreach (ColorGradientStop stops in this) + hash = hash * 31 + stops.GetHashCode(); + return hash; + } + } + + #endregion + #region Implementation of IEnumerable /// diff --git a/src/Avalonia/Artemis.UI.Shared/Controls/GradientPicker/GradientPicker.cs b/src/Avalonia/Artemis.UI.Shared/Controls/GradientPicker/GradientPicker.cs index 78dd6cf72..05d2ca73e 100644 --- a/src/Avalonia/Artemis.UI.Shared/Controls/GradientPicker/GradientPicker.cs +++ b/src/Avalonia/Artemis.UI.Shared/Controls/GradientPicker/GradientPicker.cs @@ -60,12 +60,13 @@ public class GradientPicker : TemplatedControl AvaloniaProperty.RegisterDirect(nameof(DeleteStop), g => g.DeleteStop); private readonly ICommand _deleteStop; + private bool _shiftDown; private Button? _flipStops; private Border? _gradient; private Button? _rotateStops; - private bool _shiftDown; private Button? _spreadStops; private Button? _toggleSeamless; + private Button? _randomize; private ColorGradient? _lastColorGradient; private ColorPicker? _colorPicker; @@ -153,6 +154,8 @@ public class GradientPicker : TemplatedControl _flipStops.Click -= FlipStopsOnClick; if (_rotateStops != null) _rotateStops.Click -= RotateStopsOnClick; + if (_randomize != null) + _randomize.Click -= RandomizeOnClick; _colorPicker = e.NameScope.Find("ColorPicker"); _gradient = e.NameScope.Find("Gradient"); @@ -160,6 +163,7 @@ public class GradientPicker : TemplatedControl _toggleSeamless = e.NameScope.Find + diff --git a/src/Avalonia/Artemis.UI.Shared/Styles/Controls/GradientPickerButton.axaml b/src/Avalonia/Artemis.UI.Shared/Styles/Controls/GradientPickerButton.axaml index f1b53611a..498f01733 100644 --- a/src/Avalonia/Artemis.UI.Shared/Styles/Controls/GradientPickerButton.axaml +++ b/src/Avalonia/Artemis.UI.Shared/Styles/Controls/GradientPickerButton.axaml @@ -21,7 +21,7 @@ - + \ No newline at end of file