using System;
namespace CMLeonOS.UILib.Animations
{
///
/// Easing utilities for animations.
///
internal static class Easing
{
///
/// Calculate the value of an easing function.
///
/// The absolute progress of the animation, from 0 to 1.
/// The type of the easing function.
/// The direction of the easing function.
/// The value of the easing function at the given progress.
/// An exception is thrown if the progress is out of range.
/// An exception is thrown if the type or direction is ininvalid.
internal static double Ease(double t, EasingType type, EasingDirection direction)
{
if (t < 0 || t > 1) throw new ArgumentOutOfRangeException();
switch (type)
{
case EasingType.Linear:
return t;
case EasingType.Sine:
switch (direction)
{
case EasingDirection.In:
return 1 - Math.Cos(t * Math.PI / 2);
case EasingDirection.Out:
return Math.Sin(t * Math.PI / 2);
case EasingDirection.InOut:
return -0.5 * (Math.Cos(Math.PI * t) - 1);
default:
throw new ArgumentException("Unknown easing direction.");
}
default:
throw new ArgumentException("Unknown easing type.");
}
}
///
/// Linearly interpolate between two values.
///
/// The first value.
/// The second value.
/// The value of the interpolation.
/// The interpolated value.
internal static double Lerp(double x, double y, double z)
{
return x * (1 - z) + y * z;
}
}
}