51 lines
1.1 KiB
C#
51 lines
1.1 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using Godot;
|
||
|
|
namespace Rokojori
|
||
|
|
{
|
||
|
|
public static class GradientExtensions
|
||
|
|
{
|
||
|
|
public enum GradientRepeatMode
|
||
|
|
{
|
||
|
|
Clamp,
|
||
|
|
Repeat,
|
||
|
|
MirrorRepeat
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Gradient Create( IEnumerable<Color> colors )
|
||
|
|
{
|
||
|
|
var g = new Gradient();
|
||
|
|
g.Colors = Lists.FromAny( colors ).ToArray();
|
||
|
|
|
||
|
|
var offsets = new List<float>();
|
||
|
|
for ( int i = 0; i < g.Colors.Length; i++ )
|
||
|
|
{
|
||
|
|
offsets.Add( i / (float) ( g.Colors.Length - 1f ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
g.Offsets = offsets.ToArray();
|
||
|
|
|
||
|
|
return g;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Color SampleRepeated( this Gradient gradient, float t, GradientRepeatMode repeatMode )
|
||
|
|
{
|
||
|
|
var repeatedT = t;
|
||
|
|
|
||
|
|
if ( GradientRepeatMode.Repeat == repeatMode )
|
||
|
|
{
|
||
|
|
repeatedT = MathX.Repeat( t, 1f );
|
||
|
|
}
|
||
|
|
else if ( GradientRepeatMode.MirrorRepeat == repeatMode )
|
||
|
|
{
|
||
|
|
repeatedT = MathX.Repeat( t, 2f );
|
||
|
|
|
||
|
|
if ( repeatedT > 1f )
|
||
|
|
{
|
||
|
|
repeatedT = 2f - 1f;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return gradient.Sample( repeatedT );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|