Skip to content

Commit

Permalink
allow 3 or 6 digit hex numbers, assert if vec or colors are invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
KilledByAPixel committed Dec 7, 2024
1 parent a09ecd2 commit e967368
Showing 1 changed file with 57 additions and 9 deletions.
66 changes: 57 additions & 9 deletions src/engineUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -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);
}
}

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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
Expand All @@ -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} */
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -713,7 +736,6 @@ class Color
else if (b == max)
h = (r - g) / d + 4;
}

return [h / 6, s, l, a];
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
}

///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit e967368

Please sign in to comment.