All files util.js

57.69% Statements 15/26
57.14% Branches 8/14
100% Functions 3/3
59.09% Lines 13/22

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 666x                         6x 800x 800x 715x                                       3200x     6x 4x   4x           4x               4x       4x 4x        
const NAMED_COLORS = require('./named_colors');
 
/**
 * Clamping is the process of limiting a position to an area
 *
 * @see https://en.wikipedia.org/wiki/Clamping_(graphics)
 *
 * @param {number} value The value to apply the clamp restriction to
 * @param {number} min   Lower limit
 * @param {number} max   Upper limit
 *
 * @returns {number}
 */
exports.clamp = function (value,min,max) {
    Iif(value < min) return min;
    if(value > max) return max;
    return value;
}
 
 
/**
 * Linear Interpolation
 *
 * In mathematics, linear interpolation is a method of curve fitting using linear polynomials to construct new data
 * points within the range of a discrete set of known data points.
 *
 * @param {number} a
 * @param {number} b
 * @param {number} t
 *
 * @ignore
 *
 * @see https://en.wikipedia.org/wiki/Linear_interpolation
 *
 * @returns {number}
 */
exports.lerp = function(a,b,t) {  return a + (b-a)*t; }
 
 
exports.colorStringToUint32 = function(str) {
    Iif(!str) return 0x000000;
    //hex values always get 255 for the alpha channel
    Iif(str.indexOf('#')===0) {
        let int = uint32.toUint32(parseInt(str.substring(1),16));
        int = uint32.shiftLeft(int,8);
        int = uint32.or(int,0xff);
        return int;
    }
    Iif(str.indexOf('rgba')===0) {
        const parts = str.trim().substring(4).replace('(','').replace(')','').split(',');
        return uint32.fromBytesBigEndian(
            parseInt(parts[0]),
            parseInt(parts[1]),
            parseInt(parts[2]),
            Math.floor(parseFloat(parts[3])*255));
    }
    Iif(str.indexOf('rgb')===0) {
        const parts = str.trim().substring(3).replace('(','').replace(')','').split(',');
        return uint32.fromBytesBigEndian(parseInt(parts[0]), parseInt(parts[1]), parseInt(parts[2]), 255);
    }
    Eif(NAMED_COLORS[str]) {
        return NAMED_COLORS[str];
    }
    throw new Error("unknown style format: " + str );
}