# All files / fast-colors/src math-utilities.ts

58.18% Statements 32/55
52.78% Branches 19/36
70% Functions 7/10
58.18% Lines 32/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    887x
    11486x
    3422x
    8064x
    3345x

    4719x

    7x
    1077634x
    67508x
    1010126x
    68035x

    942091x

    7x
    3629x

    3629x

    7x
    5810x

    7x
    2905x

    7x
    2741x
    2741x
    264x

    2477x

    7x
    4036x

    4036x

    4036x

    7x

    7x

    7x

    7x

     export function clamp(i: number, min: number, max: number): number {
        if (isNaN(i) || i <= min) {
            return min;
        } else if (i >= max) {
            return max;
        }
        return i;
    }

    export function normalize(i: number, min: number, max: number): number {
        if (isNaN(i) || i <= min) {
            return 0.0;
        } else if (i >= max) {
            return 1.0;
        }
        return i / (max - min);
    }

    export function denormalize(i: number, min: number, max: number): number {
        Iif (isNaN(i)) {
            return min;
        }
        return min + i * (max - min);
    }

    export function degreesToRadians(i: number): number {
        return i * (Math.PI / 180.0);
    }

    export function radiansToDegrees(i: number): number {
        return i * (180.0 / Math.PI);
    }

    export function getHexStringForByte(i: number): string {
        const s: string = Math.round(clamp(i, 0.0, 255.0)).toString(16);
        if (s.length === 1) {
            return "0" + s;
        }
        return s;
    }

    export function lerp(i: number, min: number, max: number): number {
        Iif (isNaN(i) || i <= 0.0) {
            return min;
        } else Iif (i >= 1.0) {
            return max;
        }
        return min + i * (max - min);
    }

    export function lerpAnglesInDegrees(i: number, min: number, max: number): number {
        if (i <= 0.0) {
            return min % 360.0;
        } else if (i >= 1.0) {
            return max % 360.0;
        }
        const a: number = (min - max + 360.0) % 360.0;
        const b: number = (max - min + 360.0) % 360.0;
        if (a <= b) {
            return (min - a * i + 360.0) % 360.0;
        }
        return (min + a * i + 360.0) % 360.0;
    }

    const TwoPI: number = Math.PI * 2;

    export function lerpAnglesInRadians(i: number, min: number, max: number): number {
        if (isNaN(i) || i <= 0.0) {
            return min % TwoPI;
        } else if (i >= 1.0) {
            return max % TwoPI;
        }
        const a: number = (min - max + TwoPI) % TwoPI;
        const b: number = (max - min + TwoPI) % TwoPI;
        if (a <= b) {
            return (min - a * i + TwoPI) % TwoPI;
        }
        return (min + a * i + TwoPI) % TwoPI;
    }

    // Will return infinity if i*10^(precision) overflows number
    // note that floating point rounding rules come into play here so values that end up rouding on a .5 round to the nearest even not always up
    // so 2.5 rounds to 2
    export function roundToPrecisionSmall(i: number, precision: number): number {
        const factor: number = Math.pow(10, precision);
        return Math.round(i * factor) / factor;
    }