1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 16:58:29 +00:00

Implemented Gradient-Wrapping

This commit is contained in:
Darth Affe 2016-12-22 12:03:38 +01:00
parent da15638817
commit 55dff06380
2 changed files with 56 additions and 3 deletions

View File

@ -1,4 +1,6 @@
// ReSharper disable MemberCanBeProtected.Global // ReSharper disable MemberCanBeProtected.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -18,6 +20,13 @@ namespace CUE.NET.Gradients
/// </summary> /// </summary>
public IList<GradientStop> GradientStops { get; } = new List<GradientStop>(); public IList<GradientStop> GradientStops { get; } = new List<GradientStop>();
/// <summary>
/// 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.
/// </summary>
public bool WrapGradient { get; set; }
#endregion #endregion
#region Constructors #region Constructors
@ -38,6 +47,19 @@ namespace CUE.NET.Gradients
GradientStops.Add(gradientStop); GradientStops.Add(gradientStop);
} }
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
/// <param name="wrapGradient">Specifies whether the gradient should wrapp or not (see <see cref="WrapGradient"/> for an example of what this means).</param>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
protected AbstractGradient(bool wrapGradient, params GradientStop[] gradientStops)
{
this.WrapGradient = wrapGradient;
foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop);
}
#endregion #endregion
#region Methods #region Methods

View File

@ -26,6 +26,15 @@ namespace CUE.NET.Gradients
: base(gradientStops) : base(gradientStops)
{ } { }
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
/// <param name="wrapGradient">Specifies whether the gradient should wrapp or not (see <see cref="AbstractGradient.WrapGradient"/> for an example of what this means).</param>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
public LinearGradient(bool wrapGradient, params GradientStop[] gradientStops)
: base(wrapGradient, gradientStops)
{ }
#endregion #endregion
#region Methods #region Methods
@ -40,10 +49,32 @@ namespace CUE.NET.Gradients
if (!GradientStops.Any()) return CorsairColor.Transparent; if (!GradientStops.Any()) return CorsairColor.Transparent;
if (GradientStops.Count == 1) return GradientStops.First().Color; 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(); if (WrapGradient)
GradientStop gsAfter = GradientStops.Where(n => n.Offset >= offset).OrderBy(n => n.Offset).First(); {
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; float blendFactor = 0f;
if (!gsBefore.Offset.Equals(gsAfter.Offset)) if (!gsBefore.Offset.Equals(gsAfter.Offset))