Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/melonjs/melonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
hornta committed Aug 6, 2024
2 parents 1d48912 + 1692f41 commit a7b9094
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/melonjs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added
- Chore: new GitHub Workflow for running the tests (@hornta)
- Chore: new GitHub Workflow for doc generation and publishing (@hornta)
- Color: Color constructor now also accepts another Color object as paramater
- Renderer: new `backgroundColor` property allowing to change the color when clearing the background between frames

### Fixed
Expand Down
34 changes: 20 additions & 14 deletions packages/melonjs/src/math/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,20 @@ export class Color {

/**
* Creates a new Color instance.
* @param [r] - The red component [0 .. 255]. Defaults to 0.
* @param [g] - The green component [0 .. 255]. Defaults to 0.
* @param [b] - The blue component [0 .. 255]. Defaults to 0.
* @param [alpha] - The alpha value [0.0 .. 1.0]. Defaults to 1.
* @param r - A Color object or the red component [0 .. 255]. Defaults to 0.
* @param g - The green component [0 .. 255]. Defaults to 0.
* @param b - The blue component [0 .. 255]. Defaults to 0.
* @param alpha - The alpha value [0.0 .. 1.0]. Defaults to 1.
*/
constructor(r = 0, g = 0, b = 0, alpha = 1.0) {
this.glArray = new Float32Array([0, 0, 0, 1]);
this.setColor(r, g, b, alpha);
constructor(r: Color | 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 === "object") {
this.glArray = r.glArray.slice();
} else {
throw new Error("Color: invalid parameter");
}
}

/**
Expand All @@ -242,7 +248,7 @@ export class Color {
* @param value - The red component [0 .. 255].
*/
set r(value) {
this.glArray[0] = clamp(~~value || 0, 0, 255) / 255.0;
this.glArray[0] = clamp(value, 0, 255) / 255.0;
}

/**
Expand All @@ -258,7 +264,7 @@ export class Color {
* @param value - The green component [0 .. 255].
*/
set g(value) {
this.glArray[1] = clamp(~~value || 0, 0, 255) / 255.0;
this.glArray[1] = clamp(value, 0, 255) / 255.0;
}

/**
Expand All @@ -274,7 +280,7 @@ export class Color {
* @param value - The blue component [0 .. 255].
*/
set b(value) {
this.glArray[2] = clamp(~~value || 0, 0, 255) / 255.0;
this.glArray[2] = clamp(value, 0, 255) / 255.0;
}

/**
Expand All @@ -290,7 +296,7 @@ export class Color {
* @param value - The alpha component [0.0 .. 1.0].
*/
set alpha(value) {
this.glArray[3] = clamp(+value, 0, 1.0);
this.glArray[3] = clamp(value, 0, 1.0);
}

/**
Expand Down Expand Up @@ -410,7 +416,7 @@ export class Color {
* @returns Reference to the newly cloned object.
*/
clone() {
return colorPool.get().copy(this);
return colorPool.get(this as Color);
}

/**
Expand Down Expand Up @@ -693,7 +699,7 @@ export class Color {
export const colorPool = createPool<
Color,
[
r?: number | undefined,
r?: number | Color | undefined,
g?: number | undefined,
b?: number | undefined,
alpha?: number | undefined,
Expand All @@ -703,7 +709,7 @@ export const colorPool = createPool<
return {
instance: color,
reset(r = 0, g = 0, b = 0, alpha = 1) {
color.setColor(r, g, b, alpha);
color.setColor(r as number, g, b, alpha);
},
};
});
37 changes: 36 additions & 1 deletion packages/melonjs/tests/color.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeAll, describe, expect, it } from "vitest";
import { Color } from "../src/index.js";
import { Color, getPool } from "../src/index.js";

describe("Color", () => {
let red_color: Color;
Expand All @@ -13,6 +13,41 @@ describe("Color", () => {
blue_color = new Color().parseHex("#0000FF");
});

describe("Color constructor", () => {
it("creates a new Color instance with default values", () => {
expect(green_color.r).toEqual(0);
expect(green_color.g).toEqual(128);
expect(green_color.b).toEqual(0);
expect(green_color.alpha).toEqual(1);
});

it("creates a new Color instance using another Color object as parameter", () => {
const color = new Color(red_color);
expect(color.r).toEqual(255);
expect(color.g).toEqual(0);
expect(color.b).toEqual(0);
expect(color.alpha).toEqual(0.5);
});
});

describe("get a Color instance from the pool", () => {
const pColor = getPool("color").get(0, 128, 0, 1);
it("creates a new Color instance with default values", () => {
expect(pColor.r).toEqual(0);
expect(pColor.g).toEqual(128);
expect(pColor.b).toEqual(0);
expect(pColor.alpha).toEqual(1);
});

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

describe("parseHex Function", () => {
// #RGB
it("#00F value is rgb(0, 0, 255)", () => {
Expand Down

0 comments on commit a7b9094

Please sign in to comment.