﻿using System.Runtime.CompilerServices;

namespace Trigon
{
    /// <summary>
    /// Provides extension methods for common types.
    /// </summary>
    public static partial class Extensions
    {
        /// <summary>
        /// Rescales the value from one range to another.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="sourceMin">The source range minimum value.</param>
        /// <param name="sourceMax">The source range maximum value.</param>
        /// <param name="targetMin">The target range minimum value.</param>
        /// <param name="targetMax">The target range maximum value.</param>
        /// <returns>The rescaled value.</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        internal static float Rescale(this float value, float sourceMin, float sourceMax, float targetMin, float targetMax)
        {
            return ((value.Clamp(sourceMin, sourceMax) - sourceMin) / (sourceMax - sourceMin) * (targetMax - targetMin)) + targetMin;
        }

        /// <summary>
        /// Rescales the value from one range to another.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="sourceMin">The source range minimum value.</param>
        /// <param name="sourceMax">The source range maximum value.</param>
        /// <param name="targetMin">The target range minimum value.</param>
        /// <param name="targetMax">The target range maximum value.</param>
        /// <returns>The rescaled value.</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        internal static double Rescale(this double value, double sourceMin, double sourceMax, double targetMin, double targetMax)
        {
            return ((value.Clamp(sourceMin, sourceMax) - sourceMin) / (sourceMax - sourceMin) * (targetMax - targetMin)) + targetMin;
        }
    }
}
