// --------------------------------------------------------------------------------------------------------------------
//
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
//
//
// Extension methods for classes that implement .
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor
{
using System;
///
/// Extension methods for classes that implement .
///
internal static class ComparableExtensions
{
///
/// Restricts a value to be within a specified range.
///
/// The The value to clamp.
/// The minimum value. If value is less than min, min will be returned.
/// The maximum value. If value is greater than max, max will be returned.
/// The to clamp.
///
/// The representing the clamped value.
///
public static T Clamp(this T value, T min, T max)
where T : IComparable
{
if (value.CompareTo(min) < 0)
{
return min;
}
if (value.CompareTo(max) > 0)
{
return max;
}
return value;
}
///
/// Converts an to a first restricting the value between the
/// minimum and maximum allowable ranges.
///
/// The this method extends.
/// The
public static byte ToByte(this int value)
{
return (byte)value.Clamp(0, 255);
}
///
/// Converts an to a first restricting the value between the
/// minimum and maximum allowable ranges.
///
/// The this method extends.
/// The
public static byte ToByte(this float value)
{
return (byte)value.Clamp(0, 255);
}
///
/// Converts an to a first restricting the value between the
/// minimum and maximum allowable ranges.
///
/// The this method extends.
/// The
public static byte ToByte(this double value)
{
return (byte)value.Clamp(0, 255);
}
}
}