Add Gradient.
This commit is contained in:
parent
6acaf0a7d5
commit
7485ec5eaa
159
Dashboard/Gradient.cs
Normal file
159
Dashboard/Gradient.cs
Normal file
@ -0,0 +1,159 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Dashboard
|
||||
{
|
||||
/// <summary>
|
||||
/// Enumeration of the kinds of gradients available.
|
||||
/// </summary>
|
||||
public enum GradientType
|
||||
{
|
||||
/// <summary>
|
||||
/// A gradient which transitions over a set axis.
|
||||
/// </summary>
|
||||
Axial,
|
||||
/// <summary>
|
||||
/// A gradient which transitions along elliptical curves.
|
||||
/// </summary>
|
||||
Radial,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A single gradient stop.
|
||||
/// </summary>
|
||||
/// <param name="Position">The position of the gradient stop. Must be [0,1].</param>
|
||||
/// <param name="Color">The color value for the stop.</param>
|
||||
public record struct GradientStop(float Position, Color Color);
|
||||
|
||||
/// <summary>
|
||||
/// Represents a linear gradient.
|
||||
/// </summary>
|
||||
public struct Gradient : ICollection<GradientStop>
|
||||
{
|
||||
private readonly List<GradientStop> _stops = new List<GradientStop>();
|
||||
|
||||
/// <summary>
|
||||
/// Gradient type.
|
||||
/// </summary>
|
||||
public GradientType Type { get; set; } = GradientType.Axial;
|
||||
|
||||
/// <summary>
|
||||
/// First gradient control point.
|
||||
/// </summary>
|
||||
public Vector2 C0 { get; set; } = Vector2.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// Second gradient control point.
|
||||
/// </summary>
|
||||
public Vector2 C1 { get; set; } = Vector2.One;
|
||||
|
||||
/// <summary>
|
||||
/// Number of stops in a gradient.
|
||||
/// </summary>
|
||||
public int Count => _stops.Count;
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
/// <summary>
|
||||
/// Get a gradient control point.
|
||||
/// </summary>
|
||||
/// <param name="index">The index to get the control point for.</param>
|
||||
public GradientStop this[int index]
|
||||
{
|
||||
get => _stops[index];
|
||||
set
|
||||
{
|
||||
RemoveAt(index);
|
||||
Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public Gradient()
|
||||
{
|
||||
}
|
||||
|
||||
public Gradient(Color a, Color b)
|
||||
{
|
||||
Add(new GradientStop(0, a));
|
||||
Add(new GradientStop(1, b));
|
||||
}
|
||||
|
||||
public Color GetColor(float position)
|
||||
{
|
||||
if (Count == 0)
|
||||
return Color.Black;
|
||||
else if (Count == 1)
|
||||
return _stops[0].Color;
|
||||
|
||||
int pivot = _stops.FindIndex(x => x.Position < position);
|
||||
|
||||
GradientStop left, right;
|
||||
if (pivot == -1)
|
||||
{
|
||||
left = right = _stops[^1];
|
||||
}
|
||||
else if (pivot == 0)
|
||||
{
|
||||
left = right = _stops[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
left = _stops[pivot-1];
|
||||
right = _stops[pivot];
|
||||
}
|
||||
|
||||
float weight = (position - left.Position) / (right.Position - left.Position);
|
||||
|
||||
Vector4 lcolor = new Vector4(left.Color.R, left.Color.G, left.Color.B, left.Color.A) * (1-weight);
|
||||
Vector4 rcolor = new Vector4(right.Color.R, right.Color.G, right.Color.B, right.Color.A) * weight;
|
||||
Vector4 color = lcolor + rcolor;
|
||||
|
||||
return Color.FromArgb((byte)color.W, (byte)color.X, (byte)color.Y, (byte)color.Z);
|
||||
}
|
||||
|
||||
public IEnumerator<GradientStop> GetEnumerator()
|
||||
{
|
||||
return _stops.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable)_stops).GetEnumerator();
|
||||
}
|
||||
|
||||
public void Add(GradientStop item)
|
||||
{
|
||||
int index = _stops.FindIndex(x => x.Position > item.Position);
|
||||
if (index == -1)
|
||||
index = _stops.Count;
|
||||
|
||||
_stops.Insert(index, item);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_stops.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(GradientStop item)
|
||||
{
|
||||
return _stops.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(GradientStop[] array, int arrayIndex)
|
||||
{
|
||||
_stops.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool Remove(GradientStop item)
|
||||
{
|
||||
return _stops.Remove(item);
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
_stops.RemoveAt(index);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user