Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MathExtensions.cs « Extensions « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72c33bc810993c47b105fccba36f312141a562b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Text;

namespace UVtools.Core.Extensions
{
    public static class MathExtensions
    {
        public static byte Clamp(this byte value, byte min, byte max)
        {
            return value <= min ? min : value >= max ? max : value;
        }

        public static sbyte Clamp(this sbyte value, sbyte min, sbyte max)
        {
            return value <= min ? min : value >= max ? max : value;
        }

        public static ushort Clamp(this ushort value, ushort min, ushort max)
        {
            return value <= min ? min : value >= max ? max : value;
        }

        public static short Clamp(this short value, short min, short max)
        {
            return value <= min ? min : value >= max ? max : value;
        }

        public static uint Clamp(this uint value, uint min, uint max)
        {
            return value <= min ? min : value >= max ? max : value;
        }

        public static int Clamp(this int value, int min, int max)
        {
            return value <= min ? min : value >= max ? max : value;
        }

        public static ulong Clamp(this ulong value, ulong min, ulong max)
        {
            return value <= min ? min : value >= max ? max : value;
        }

        public static long Clamp(this long value, long min, long max)
        {
            return value <= min ? min : value >= max ? max : value;
        }

        public static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
        {
            if (value.CompareTo(min) < 0)
                return min;
            if (value.CompareTo(max) > 0)
                return max;

            return value;
        }
    }
}