Skip to content

Commit

Permalink
fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
minggo committed May 23, 2024
1 parent 30d9af4 commit d767dc3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
15 changes: 2 additions & 13 deletions cocos/core/math/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ export class Color extends ValueType {
* @zh 全通道统一缩放颜色
*/
public static scale<Out extends IColorLike> (out: Out, a: Out, b: number): Out {
if (b < 0) {
return out;
}

out.r = a.r * b;
out.g = a.g * b;
out.b = a.b * b;
Expand Down Expand Up @@ -459,11 +455,7 @@ export class Color extends ValueType {
* @param ratio The interpolation coefficient.The range is [0,1].
*/
public lerp (to: Readonly<Color>, ratio: number): Color {
const toColor = to as Color;
this.r += (toColor._r - this._r) * ratio;
this.g += (toColor._g - this._g) * ratio;
this.b += (toColor._b - this._b) * ratio;
this.a += (toColor._a - this._a) * ratio;
Color.lerp(this, this, to, ratio);
return this;
}

Expand Down Expand Up @@ -735,10 +727,7 @@ export class Color extends ValueType {
* @param other The specified color.
*/
public multiply (other: Color): Color {
this.r *= other._r;
this.g *= other._g;
this.b *= other._b;
this.a = other._a;
Color.multiply(this, this, other);
return this;
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/core/math/color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('Test Color', () => {
expect(color3.a).toBe(0);
});
test('multiply', () => {
// test static function
const color = new Color(128, 234, 10, 255);
const color2 = new Color(128, 234, 20, 255);
const color3 = new Color();
Expand All @@ -80,6 +81,13 @@ describe('Test Color', () => {
expect(color3.g).toBe(255);
expect(color3.b).toBe(200);
expect(color3.a).toBe(255);

// test object function
color.multiply(color2);
expect(color.r).toBe(255);
expect(color.g).toBe(255);
expect(color.b).toBe(200);
expect(color.a).toBe(255);
});
test('divide', () => {
const color = new Color(128, 234, 10, 255);
Expand All @@ -101,6 +109,7 @@ describe('Test Color', () => {
expect(color2.a).toBe(255);
});
test('lerp', () => {
// static function version
const color = new Color(1, 235, 15, 255);
const color2 = new Color(255, 255, 255, 255);
const color3 = new Color();
Expand All @@ -109,6 +118,13 @@ describe('Test Color', () => {
expect(color3.g).toBe(245);
expect(color3.b).toBe(135);
expect(color3.a).toBe(255);

// object method version
color.lerp(color2, 0.5);
expect(color.r).toBe(128);
expect(color.g).toBe(245);
expect(color.b).toBe(135);
expect(color.a).toBe(255);
});
test('toArray', () => {
const color = new Color(1, 235, 15, 255);
Expand Down

0 comments on commit d767dc3

Please sign in to comment.