Skip to content

Commit

Permalink
align accepted arguments for constructor and reset method when using …
Browse files Browse the repository at this point in the history
…object pool
  • Loading branch information
obiot committed Aug 14, 2024
1 parent 01cbb22 commit 1d77516
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/melonjs/src/math/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,13 @@ export class Color {
* @param b - The blue component [0 .. 255]. Defaults to 0.
* @param alpha - The alpha value [0.0 .. 1.0]. Defaults to 1.
*/
constructor(r: Color | number = 0, g = 0, b = 0, alpha = 1.0) {
constructor(r: Color | string | number = 0, g = 0, b = 0, alpha = 1.0) {
if (typeof r === "number") {
this.glArray = new Float32Array([0, 0, 0, 1]);
this.setColor(r, g, b, alpha);
} else if (typeof r === "string") {
this.glArray = new Float32Array([0, 0, 0, 1]);
this.parseCSS(r as ColorName);
} else if (typeof r === "object") {
this.glArray = r.glArray.slice();
} else {
Expand Down Expand Up @@ -564,7 +567,12 @@ export class Color {
if (!match) {
return this.parseHex(rgbColor as `#${string}`);
}
return this.setColor(+match[1], +match[2], +match[3], +match[5]);
return this.setColor(
+match[1],
+match[2],
+match[3],
typeof match[5] !== "undefined" ? +match[5] : 1,
);
}

/**
Expand Down Expand Up @@ -700,7 +708,7 @@ export class Color {
export const colorPool = createPool<
Color,
[
r?: number | Color | undefined,
r?: number | string | Color | undefined,
g?: number | undefined,
b?: number | undefined,
alpha?: number | undefined,
Expand Down
16 changes: 16 additions & 0 deletions packages/melonjs/tests/color.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ describe("Color", () => {
expect(pColor2.b).toEqual(0);
expect(pColor2.alpha).toEqual(1);
});

it("get Color instance from the pool using a CSS String object as parameter", () => {
const pColor2 = getPool("color").get("#008000");
expect(pColor2.r).toEqual(0);
expect(pColor2.g).toEqual(128);
expect(pColor2.b).toEqual(0);
expect(pColor2.alpha).toEqual(1);
});

it("get Color instance from the pool using a rgb String object as parameter", () => {
const pColor3 = getPool("color").get("rgb(0,0,255)");
expect(pColor3.r).toEqual(0);
expect(pColor3.g).toEqual(0);
expect(pColor3.b).toEqual(255);
expect(pColor3.alpha).toEqual(1);
});
});

describe("parseHex Function", () => {
Expand Down

0 comments on commit 1d77516

Please sign in to comment.