Skip to content

Commit 06b080a

Browse files
feat(Color): add withAlpha method to create color copies with specified alpha
- Introduced a new method `withAlpha` in the Color class to return a copy of the color with a specified alpha value. - Updated the Color class in both `littlejs.release.js` and `engineMath.js`. - Added unit tests to verify the functionality of the new method, ensuring it returns a new instance with the correct alpha while leaving the original color unchanged.
1 parent 121bd6d commit 06b080a

9 files changed

Lines changed: 38 additions & 2 deletions

File tree

REFERENCE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Color.HSLA() // Get the color in HSLA format
102102
Color.mutate(amount=.05, alphaAmount=0) // Randomly diverge from this color
103103
Color.setHex(hex) // Set this color from a hex code
104104
Color.setAlpha(a=1) // Set the alpha of this color
105+
Color.withAlpha(a=1) // Get a copy of this color with the alpha set
105106
Color.rgbaInt() // Get this color as 32 bit RGBA value
106107
Color.toString(useAlpha=true) // Get hex color code as a string
107108

dist/littlejs.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,10 @@ declare module "littlejsengine" {
13951395
/** Returns a new color that is a copy of this
13961396
* @return {Color} */
13971397
copy(): Color;
1398+
/** Returns a copy of this color with the alpha set
1399+
* @param {number} [a] - alpha
1400+
* @return {Color} */
1401+
withAlpha(a?: number): Color;
13981402
/** Returns a copy of this color plus the color passed in
13991403
* @param {Color} c - other color
14001404
* @return {Color} */

dist/littlejs.esm.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,11 @@ class Color
22472247
* @return {Color} */
22482248
copy() { return new Color(this.r, this.g, this.b, this.a); }
22492249

2250+
/** Returns a copy of this color with the alpha set
2251+
* @param {number} [a] - alpha
2252+
* @return {Color} */
2253+
withAlpha(a=1) { return new Color(this.r, this.g, this.b, a); }
2254+
22502255
/** Returns a copy of this color plus the color passed in
22512256
* @param {Color} c - other color
22522257
* @return {Color} */

dist/littlejs.esm.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/littlejs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,11 @@ class Color
22472247
* @return {Color} */
22482248
copy() { return new Color(this.r, this.g, this.b, this.a); }
22492249

2250+
/** Returns a copy of this color with the alpha set
2251+
* @param {number} [a] - alpha
2252+
* @return {Color} */
2253+
withAlpha(a=1) { return new Color(this.r, this.g, this.b, a); }
2254+
22502255
/** Returns a copy of this color plus the color passed in
22512256
* @param {Color} c - other color
22522257
* @return {Color} */

dist/littlejs.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/littlejs.release.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,11 @@ class Color
15591559
* @return {Color} */
15601560
copy() { return new Color(this.r, this.g, this.b, this.a); }
15611561

1562+
/** Returns a copy of this color with the alpha set
1563+
* @param {number} [a] - alpha
1564+
* @return {Color} */
1565+
withAlpha(a=1) { return new Color(this.r, this.g, this.b, a); }
1566+
15621567
/** Returns a copy of this color plus the color passed in
15631568
* @param {Color} c - other color
15641569
* @return {Color} */

src/engineMath.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,11 @@ class Color
938938
* @return {Color} */
939939
copy() { return new Color(this.r, this.g, this.b, this.a); }
940940

941+
/** Returns a copy of this color with the alpha set
942+
* @param {number} [a] - alpha
943+
* @return {Color} */
944+
withAlpha(a=1) { return new Color(this.r, this.g, this.b, a); }
945+
941946
/** Returns a copy of this color plus the color passed in
942947
* @param {Color} c - other color
943948
* @return {Color} */

test/math.test.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,17 @@ test('Color setAlpha mutates alpha and returns self', () =>
647647
assert.equal(c.a, 1);
648648
});
649649

650+
test('Color withAlpha returns a copy with new alpha', () =>
651+
{
652+
const c = new Color(0.25, 0.5, 0.75, 1);
653+
const r = c.withAlpha(0.5);
654+
assert.equal(r.a, 0.5); // copy has the given alpha
655+
assert.equal(r.r, 0.25); assert.equal(r.g, 0.5); assert.equal(r.b, 0.75); // rgb copied
656+
assert.equal(c.a, 1); // original untouched
657+
assert.notStrictEqual(r, c); // returns a new color, not self
658+
assert.equal(new Color(1, 1, 1, 0.3).withAlpha().a, 1); // defaults to 1
659+
});
660+
650661
test('Color divide', () =>
651662
{
652663
const r = new Color(1, 0.5, 0.25, 1).divide(new Color(2, 1, 0.5, 1));

0 commit comments

Comments
 (0)