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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144              7x 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        
/**
 * Ensures that an input number does not exceed a max value and is not less than a min value.
 * @param i - the number to clamp
 * @param min - the maximum (inclusive) value
 * @param max - the minimum (inclusive) value
 * @public
 */
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;
}
 
/**
 * Scales an input to a number between 0 and 1
 * @param i - a number between min and max
 * @param min - the max value
 * @param max - the min value
 * @public
 */
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);
}
 
/**
 * Scales a number between 0 and 1
 * @param i - the number to denormalize
 * @param min - the min value
 * @param max - the max value
 * @public
 */
export function denormalize(i: number, min: number, max: number): number {
    Iif (isNaN(i)) {
        return min;
    }
    return min + i * (max - min);
}
 
/**
 * Converts degrees to radians.
 * @param i - degrees
 * @public
 */
export function degreesToRadians(i: number): number {
    return i * (Math.PI / 180.0);
}
 
/**
 * Converts radians to degrees.
 * @param i - radians
 * @public
 */
export function radiansToDegrees(i: number): number {
    return i * (180.0 / Math.PI);
}
 
/**
 * Converts a number between 0 and 255 to a hex string.
 * @param i - the number to convert to a hex string
 * @public
 */
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;
}
 
/**
 * Linearly interpolate
 * @public
 */
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);
}
 
/**
 * Linearly interpolate angles in degrees
 * @public
 */
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;
 
/**
 * Linearly interpolate angles in radians
 * @public
 */
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 rounding on a .5 round to the nearest
 * even not always up so 2.5 rounds to 2
 * @param i - the number to round
 * @param precision - the precision to round to
 *
 * @public
 */
export function roundToPrecisionSmall(i: number, precision: number): number {
    const factor: number = Math.pow(10, precision);
    return Math.round(i * factor) / factor;
}