Skip to content

Commit a7b9094

Browse files
committed
Merge branch 'master' of https://github.com/melonjs/melonJS
2 parents 1d48912 + 1692f41 commit a7b9094

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

packages/melonjs/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66
- Chore: new GitHub Workflow for running the tests (@hornta)
77
- Chore: new GitHub Workflow for doc generation and publishing (@hornta)
8+
- Color: Color constructor now also accepts another Color object as paramater
89
- Renderer: new `backgroundColor` property allowing to change the color when clearing the background between frames
910

1011
### Fixed

packages/melonjs/src/math/color.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,20 @@ export class Color {
219219

220220
/**
221221
* Creates a new Color instance.
222-
* @param [r] - The red component [0 .. 255]. Defaults to 0.
223-
* @param [g] - The green component [0 .. 255]. Defaults to 0.
224-
* @param [b] - The blue component [0 .. 255]. Defaults to 0.
225-
* @param [alpha] - The alpha value [0.0 .. 1.0]. Defaults to 1.
222+
* @param r - A Color object or the red component [0 .. 255]. Defaults to 0.
223+
* @param g - The green component [0 .. 255]. Defaults to 0.
224+
* @param b - The blue component [0 .. 255]. Defaults to 0.
225+
* @param alpha - The alpha value [0.0 .. 1.0]. Defaults to 1.
226226
*/
227-
constructor(r = 0, g = 0, b = 0, alpha = 1.0) {
228-
this.glArray = new Float32Array([0, 0, 0, 1]);
229-
this.setColor(r, g, b, alpha);
227+
constructor(r: Color | number = 0, g = 0, b = 0, alpha = 1.0) {
228+
if (typeof r === "number") {
229+
this.glArray = new Float32Array([0, 0, 0, 1]);
230+
this.setColor(r, g, b, alpha);
231+
} else if (typeof r === "object") {
232+
this.glArray = r.glArray.slice();
233+
} else {
234+
throw new Error("Color: invalid parameter");
235+
}
230236
}
231237

232238
/**
@@ -242,7 +248,7 @@ export class Color {
242248
* @param value - The red component [0 .. 255].
243249
*/
244250
set r(value) {
245-
this.glArray[0] = clamp(~~value || 0, 0, 255) / 255.0;
251+
this.glArray[0] = clamp(value, 0, 255) / 255.0;
246252
}
247253

248254
/**
@@ -258,7 +264,7 @@ export class Color {
258264
* @param value - The green component [0 .. 255].
259265
*/
260266
set g(value) {
261-
this.glArray[1] = clamp(~~value || 0, 0, 255) / 255.0;
267+
this.glArray[1] = clamp(value, 0, 255) / 255.0;
262268
}
263269

264270
/**
@@ -274,7 +280,7 @@ export class Color {
274280
* @param value - The blue component [0 .. 255].
275281
*/
276282
set b(value) {
277-
this.glArray[2] = clamp(~~value || 0, 0, 255) / 255.0;
283+
this.glArray[2] = clamp(value, 0, 255) / 255.0;
278284
}
279285

280286
/**
@@ -290,7 +296,7 @@ export class Color {
290296
* @param value - The alpha component [0.0 .. 1.0].
291297
*/
292298
set alpha(value) {
293-
this.glArray[3] = clamp(+value, 0, 1.0);
299+
this.glArray[3] = clamp(value, 0, 1.0);
294300
}
295301

296302
/**
@@ -410,7 +416,7 @@ export class Color {
410416
* @returns Reference to the newly cloned object.
411417
*/
412418
clone() {
413-
return colorPool.get().copy(this);
419+
return colorPool.get(this as Color);
414420
}
415421

416422
/**
@@ -693,7 +699,7 @@ export class Color {
693699
export const colorPool = createPool<
694700
Color,
695701
[
696-
r?: number | undefined,
702+
r?: number | Color | undefined,
697703
g?: number | undefined,
698704
b?: number | undefined,
699705
alpha?: number | undefined,
@@ -703,7 +709,7 @@ export const colorPool = createPool<
703709
return {
704710
instance: color,
705711
reset(r = 0, g = 0, b = 0, alpha = 1) {
706-
color.setColor(r, g, b, alpha);
712+
color.setColor(r as number, g, b, alpha);
707713
},
708714
};
709715
});

packages/melonjs/tests/color.spec.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { beforeAll, describe, expect, it } from "vitest";
2-
import { Color } from "../src/index.js";
2+
import { Color, getPool } from "../src/index.js";
33

44
describe("Color", () => {
55
let red_color: Color;
@@ -13,6 +13,41 @@ describe("Color", () => {
1313
blue_color = new Color().parseHex("#0000FF");
1414
});
1515

16+
describe("Color constructor", () => {
17+
it("creates a new Color instance with default values", () => {
18+
expect(green_color.r).toEqual(0);
19+
expect(green_color.g).toEqual(128);
20+
expect(green_color.b).toEqual(0);
21+
expect(green_color.alpha).toEqual(1);
22+
});
23+
24+
it("creates a new Color instance using another Color object as parameter", () => {
25+
const color = new Color(red_color);
26+
expect(color.r).toEqual(255);
27+
expect(color.g).toEqual(0);
28+
expect(color.b).toEqual(0);
29+
expect(color.alpha).toEqual(0.5);
30+
});
31+
});
32+
33+
describe("get a Color instance from the pool", () => {
34+
const pColor = getPool("color").get(0, 128, 0, 1);
35+
it("creates a new Color instance with default values", () => {
36+
expect(pColor.r).toEqual(0);
37+
expect(pColor.g).toEqual(128);
38+
expect(pColor.b).toEqual(0);
39+
expect(pColor.alpha).toEqual(1);
40+
});
41+
42+
it("get Color instance from the pool using another Color object as parameter", () => {
43+
const pColor2 = getPool("color").get(pColor);
44+
expect(pColor2.r).toEqual(0);
45+
expect(pColor2.g).toEqual(128);
46+
expect(pColor2.b).toEqual(0);
47+
expect(pColor2.alpha).toEqual(1);
48+
});
49+
});
50+
1651
describe("parseHex Function", () => {
1752
// #RGB
1853
it("#00F value is rgb(0, 0, 255)", () => {

0 commit comments

Comments
 (0)