mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.0 KiB
63 lines
2.0 KiB
// <copyright file="ComparableExtensions.cs" company="James Jackson-South">
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
|
|
namespace ImageSharp
|
|
{
|
|
using System;
|
|
|
|
/// <summary>
|
|
/// Extension methods for classes that implement <see cref="IComparable{T}"/>.
|
|
/// </summary>
|
|
internal static class ComparableExtensions
|
|
{
|
|
/// <summary>
|
|
/// Restricts a <see cref="int"/> to be within a specified range.
|
|
/// </summary>
|
|
/// <param name="value">The The value to clamp.</param>
|
|
/// <param name="min">The minimum value. If value is less than min, min will be returned.</param>
|
|
/// <param name="max">The maximum value. If value is greater than max, max will be returned.</param>
|
|
/// <returns>
|
|
/// The <see cref="int"/> representing the clamped value.
|
|
/// </returns>
|
|
public static int Clamp(this int value, int min, int max)
|
|
{
|
|
if (value > max)
|
|
{
|
|
return max;
|
|
}
|
|
|
|
if (value < min)
|
|
{
|
|
return min;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restricts a <see cref="float"/> to be within a specified range.
|
|
/// </summary>
|
|
/// <param name="value">The The value to clamp.</param>
|
|
/// <param name="min">The minimum value. If value is less than min, min will be returned.</param>
|
|
/// <param name="max">The maximum value. If value is greater than max, max will be returned.</param>
|
|
/// <returns>
|
|
/// The <see cref="float"/> representing the clamped value.
|
|
/// </returns>
|
|
public static float Clamp(this float value, float min, float max)
|
|
{
|
|
if (value > max)
|
|
{
|
|
return max;
|
|
}
|
|
|
|
if (value < min)
|
|
{
|
|
return min;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
}
|
|
}
|
|
|