From e967368c21147235ad8d216243fea32b834bed58 Mon Sep 17 00:00:00 2001 From: Frank Force Date: Fri, 6 Dec 2024 20:00:54 -0600 Subject: [PATCH] allow 3 or 6 digit hex numbers, assert if vec or colors are invalid --- src/engineUtilities.js | 66 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/src/engineUtilities.js b/src/engineUtilities.js index c83ae594..d689d96f 100644 --- a/src/engineUtilities.js +++ b/src/engineUtilities.js @@ -331,18 +331,24 @@ class Vector2 * @param {Number} [y] - Y axis location */ constructor(x=0, y=0) { - ASSERT(typeof x == 'number' && typeof y == 'number'); /** @property {Number} - X axis location */ this.x = x; /** @property {Number} - Y axis location */ this.y = y; + ASSERT(this.isValid()); } /** Sets values of this vector and returns self * @param {Number} [x] - X axis location * @param {Number} [y] - Y axis location * @return {Vector2} */ - set(x=0, y=0) { this.x=x; this.y=y; return this; } + set(x=0, y=0) + { + this.x = x; + this.y = y; + ASSERT(this.isValid()); + return this; + } /** Returns a new vector that is a copy of this * @return {Vector2} */ @@ -534,6 +540,14 @@ class Vector2 if (debug) return `(${(this.x<0?'':' ') + this.x.toFixed(digits)},${(this.y<0?'':' ') + this.y.toFixed(digits)} )`; } + + /** Checks if this is a valid vector + * @return {Boolean} */ + isValid() + { + return typeof this.x == 'number' && !isNaN(this.x) + && typeof this.y == 'number' && !isNaN(this.y); + } } /////////////////////////////////////////////////////////////////////////////// @@ -594,6 +608,7 @@ class Color this.b = b; /** @property {Number} - Alpha */ this.a = a; + ASSERT(this.isValid()); } /** Sets values of this color and returns self @@ -603,7 +618,14 @@ class Color * @param {Number} [a] - alpha * @return {Color} */ set(r=1, g=1, b=1, a=1) - { this.r=r; this.g=g; this.b=b; this.a=a; return this; } + { + this.r = r; + this.g = g; + this.b = b; + this.a = a; + ASSERT(this.isValid()); + return this; + } /** Returns a new color that is a copy of this * @return {Color} */ @@ -686,6 +708,7 @@ class Color this.g = f(p, q, h); this.b = f(p, q, h - 1/3); this.a = a; + ASSERT(this.isValid()); return this; } @@ -713,7 +736,6 @@ class Color else if (b == max) h = (r - g) / d + 4; } - return [h / 6, s, l, a]; } @@ -746,11 +768,27 @@ class Color * @return {Color} */ setHex(hex) { - const fromHex = (c)=> clamp(parseInt(hex.slice(c,c+2),16)/255); - this.r = fromHex(1); - this.g = fromHex(3), - this.b = fromHex(5); - this.a = hex.length > 7 ? fromHex(7) : 1; + ASSERT(typeof hex == 'string' && hex[0] == '#'); + ASSERT([4,5,7,9].includes(hex.length), 'Invalid hex'); + + if (hex.length < 6) + { + const fromHex = (c)=> clamp(parseInt(hex[c],16)/15); + this.r = fromHex(1); + this.g = fromHex(2), + this.b = fromHex(3); + this.a = hex.length == 5 ? fromHex(4) : 1; + } + else + { + const fromHex = (c)=> clamp(parseInt(hex.slice(c,c+2),16)/255); + this.r = fromHex(1); + this.g = fromHex(3), + this.b = fromHex(5); + this.a = hex.length == 9 ? fromHex(7) : 1; + } + + ASSERT(this.isValid()); return this; } @@ -764,6 +802,16 @@ class Color const a = clamp(this.a)*255<<24; return r + g + b + a; } + + /** Checks if this is a valid color + * @return {Boolean} */ + isValid() + { + return typeof this.r == 'number' && !isNaN(this.r) + && typeof this.g == 'number' && !isNaN(this.g) + && typeof this.b == 'number' && !isNaN(this.b) + && typeof this.a == 'number' && !isNaN(this.a); + } } ///////////////////////////////////////////////////////////////////////////////