/// <summary> /// Extension method that performs a transformation on the <see cref="Byte"/> subject using /// linear mapping to re-map from the provided initial range <paramref name="startRange"/> /// to the target range <paramref name="endRange"/>. /// </summary> /// <param name="this"> /// The subject <see cref="Byte"/> in which to perform the linear map range re-mapping upon. /// </param> /// <param name="startRange"> /// An instance of the type <see cref="ByteRange"/>, describing a range of a pair of numeric /// <see cref="Byte"/> values in which the linear re-mapping uses as the inital range. /// </param> /// <param name="endRange"> /// An instance of the type <see cref="ByteRange"/>, describing a range of a pair of numeric /// <see cref="Byte"/> values in which the linear re-mapping uses as the target range. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when either the <paramref name="startRange"/> or the <paramref name="endRange"/> /// parameters are equal to <see langword="null"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown when either the <paramref name="@this"/> subject parameter is not within the bounds /// of the provided <paramref name="startRange"/>. This is determined with a call to the /// <see cref="IsNotWithin"/> method, with an implicit <see cref="EndpointExclusivity"/> value /// of <see cref="EndpointExclusivity.Inclusive"/> by default, unless it is specified explicitly /// set this property to <see cref="EndpointExclusivity.Exclusive"/>. /// </exception> /// <returns> /// A <see cref="Byte"/> value that has been linearly mapped on the <paramref name="startRange"/> /// parameter, and re-mapped to the <paramref name="endRange"/> parameter. /// </returns> public static Byte LinearMap( this Byte @this, [NotNull] ByteRange startRange, [NotNull] ByteRange endRange) { startRange.IsNotNull(nameof(startRange)); endRange.IsNotNull(nameof(endRange)); if (@this.IsNotWithin(startRange)) { throw new ArgumentOutOfRangeException( nameof(@this), @this, $"The {nameof(@this).SQuote()} parameter value is outside of the acceptable range. The " + $"value must be within the {nameof(startRange).SQuote()} parameter. The current value is " + $"{@this.ToString().SQuote()}, and the {nameof(startRange).SQuote()} parameter range is " + $"{startRange}."); } var linearMapped = (@this - startRange.Minimum) * (endRange.Maximum - endRange.Minimum) / (startRange.Maximum - startRange.Minimum) + endRange.Minimum; return(Convert .ToByte( linearMapped)); }
/// <summary> /// Extension method that allows for <see cref="IntegralRangeBase{TIntegralType}.Constrain"/> /// to be called on a <see cref="Byte"/> subject with the range and exclusivity passed as a /// parameter, rather than on the <see cref="IntegralRangeBase{TIntegralType}"/> object /// with a <see cref="Byte"/> parameter. /// </summary> /// <param name="this"> /// The subject <see cref="Byte"/> value in which to check against the <paramref name="range"/> /// parameter to constrain a value within a range with an implicit inclusive comparison mode. /// </param> /// <param name="range"> /// An instance of the type <see cref="ByteRange"/>, describing a range of numeric values in /// which the <paramref name="this"/> subject is to be compared against. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when the specified <paramref name="range"/> is <see langword="null"/>. /// </exception> /// <returns> /// A <see cref="Byte"/> value that is the <paramref name="this"/> subject value adjusted to /// force the range of possible values to be within the provided <paramref cref="range"/> /// parameter, using <see cref="EndpointExclusivity.Inclusive"/> as the comparison mode. /// </returns> public static Byte Constrain( this Byte @this, [NotNull] ByteRange range) { range.IsNotNull(nameof(range)); return(range .Constrain( @this)); }
/// <summary> /// Extension method that allows for <see cref="IntegralRangeBase{TIntegralType}.IsNotWithin"/> /// to be called on a <see cref="Byte"/> subject with the range and exclusivity passed as a /// parameter, rather than on the <see cref="IntegralRangeBase{TIntegralType}"/> object /// with a <see cref="Byte"/> parameter. /// </summary> /// <param name="this"> /// The subject <see cref="Byte"/> value in which to check against the <paramref name="range"/> /// parameter to determine whether it is within the range, taking into account the exclusivity. /// </param> /// <param name="range"> /// An instance of the type <see cref="ByteRange"/>, describing a range of numeric values in /// which the <paramref name="this"/> subject is to be compared against. /// </param> /// <param name="exclusivity"> /// A value indicating whether to perform the upper and lower bounds comparisons including /// the range's Minimum and Maximum bounds, or to exclude them. This parameter is optional, /// and the default value is <see cref="EndpointExclusivity.Inclusive"/>. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when the specified <paramref name="range"/> is <see langword="null"/>. /// </exception> /// <returns> /// A <see cref="Byte"/> value indicating whether or not the <paramref name="this"/> subject /// is within the provided <paramref cref="range"/> parameter, taking into account the /// <see cref="EndpointExclusivity"/> mode via the <paramref name="exclusivity"/> parameter. /// This comparison is the logical inverse of the <see cref="IsNotWithin"/> extension method. /// </returns> public static bool IsNotWithin( this Byte @this, [NotNull] ByteRange range, EndpointExclusivity exclusivity = EndpointExclusivity.Inclusive) { range.IsNotNull(nameof(range)); return(range .IsNotWithin( @this, exclusivity)); }
/// <summary> /// Extension method that performs a transformation on the <see cref="Byte"/> subject using /// linear mapping to re-map from the provided initial range <paramref name="startRange"/> /// to the target range <paramref name="endRange"/>. /// </summary> /// <param name="this"> /// The subject <see cref="Byte"/> to perform the linear map range re-mapping upon. /// </param> /// <param name="startRange"> /// An instance of the type <see cref="ByteRange"/>, describing a range of numeric values in /// which the linear re-mapping uses as the initial range of the subject. /// </param> /// <param name="endRange"> /// An instance of the type <see cref="ByteRange"/>, describing a range of numeric values in /// which the linear re-mapping uses as the target range of the return value. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when either the <paramref name="startRange"/> or the <paramref name="endRange"/> /// parameters are equal to <see langword="null"/>. /// </exception> /// <returns> /// A <see cref="Byte"/> value that has been linearly mapped to the <paramref name="startRange"/> /// parameter and re-mapped to the <paramref name="endRange"/> parameter. /// </returns> public static Byte LinearMap( this Byte @this, [NotNull] ByteRange startRange, [NotNull] ByteRange endRange) { startRange.IsNotNull(nameof(startRange)); endRange.IsNotNull(nameof(endRange)); return(( (@this - startRange.Minimum) * (endRange.Maximum - endRange.Minimum) / (startRange.Maximum - startRange.Minimum) + endRange.Minimum) .To <Byte>()); }
public static void ThrowIfNotWithin( [AssertionCondition(IS_NOT_NULL)] this Byte @this, [NotNull] ByteRange range, [InvokerParameterName] string elementName, EndpointExclusivity exclusivity = Inclusive, [CallerMemberName] string callerMemberName = "") { range.IsNotNull(nameof(range)); if (range .IsNotWithin( @this, exclusivity)) { throw new ArgumentOutOfRangeException( elementName, $"Parameter {elementName.SQuote()} passed to the method {callerMemberName.SQuote()} " + $"must be within [{range.Minimum} and {range.Maximum}], {exclusivity}ly."); } }