From 55dff063802e49b71ddf2f22d53a7fb9cc0bc4cb Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Thu, 22 Dec 2016 12:03:38 +0100 Subject: [PATCH] Implemented Gradient-Wrapping --- Gradients/AbstractGradient.cs | 22 +++++++++++++++++++++ Gradients/LinearGradient.cs | 37 ++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Gradients/AbstractGradient.cs b/Gradients/AbstractGradient.cs index c6ea39c..c399a80 100644 --- a/Gradients/AbstractGradient.cs +++ b/Gradients/AbstractGradient.cs @@ -1,4 +1,6 @@ // ReSharper disable MemberCanBeProtected.Global +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global using System.Collections.Generic; using System.Linq; @@ -18,6 +20,13 @@ namespace CUE.NET.Gradients /// public IList GradientStops { get; } = new List(); + /// + /// Gets or sets if the Gradient wraps around if there isn't a second stop to take. + /// Example: There is a stop at offset 0f, 0.5f and 0.75f. + /// Without wrapping offset 1f will be calculated the same as 0.75f. With wrapping it would be the same as 0f. + /// + public bool WrapGradient { get; set; } + #endregion #region Constructors @@ -38,6 +47,19 @@ namespace CUE.NET.Gradients GradientStops.Add(gradientStop); } + /// + /// Initializes a new instance of the class. + /// + /// Specifies whether the gradient should wrapp or not (see for an example of what this means). + /// The stops with which the gradient should be initialized. + protected AbstractGradient(bool wrapGradient, params GradientStop[] gradientStops) + { + this.WrapGradient = wrapGradient; + + foreach (GradientStop gradientStop in gradientStops) + GradientStops.Add(gradientStop); + } + #endregion #region Methods diff --git a/Gradients/LinearGradient.cs b/Gradients/LinearGradient.cs index 7f6a540..49333db 100644 --- a/Gradients/LinearGradient.cs +++ b/Gradients/LinearGradient.cs @@ -26,6 +26,15 @@ namespace CUE.NET.Gradients : base(gradientStops) { } + /// + /// Initializes a new instance of the class. + /// + /// Specifies whether the gradient should wrapp or not (see for an example of what this means). + /// The stops with which the gradient should be initialized. + public LinearGradient(bool wrapGradient, params GradientStop[] gradientStops) + : base(wrapGradient, gradientStops) + { } + #endregion #region Methods @@ -40,10 +49,32 @@ namespace CUE.NET.Gradients if (!GradientStops.Any()) return CorsairColor.Transparent; if (GradientStops.Count == 1) return GradientStops.First().Color; - offset = ClipOffset(offset); + GradientStop gsBefore; + GradientStop gsAfter; - GradientStop gsBefore = GradientStops.Where(n => n.Offset <= offset).OrderBy(n => n.Offset).Last(); - GradientStop gsAfter = GradientStops.Where(n => n.Offset >= offset).OrderBy(n => n.Offset).First(); + if (WrapGradient) + { + gsBefore = GradientStops.Where(n => n.Offset <= offset).OrderBy(n => n.Offset).LastOrDefault(); + if (gsBefore == null) + { + GradientStop lastStop = GradientStops.OrderBy(n => n.Offset).Last(); + gsBefore = new GradientStop(lastStop.Offset - 1f, lastStop.Color); + } + + gsAfter = GradientStops.Where(n => n.Offset >= offset).OrderBy(n => n.Offset).FirstOrDefault(); + if (gsAfter == null) + { + GradientStop firstStop = GradientStops.OrderBy(n => n.Offset).First(); + gsAfter = new GradientStop(firstStop.Offset + 1f, firstStop.Color); + } + } + else + { + offset = ClipOffset(offset); + + gsBefore = GradientStops.Where(n => n.Offset <= offset).OrderBy(n => n.Offset).Last(); + gsAfter = GradientStops.Where(n => n.Offset >= offset).OrderBy(n => n.Offset).First(); + } float blendFactor = 0f; if (!gsBefore.Offset.Equals(gsAfter.Offset))