Skip to content

Commit 57e6246

Browse files
committed
Added limit parameter
1 parent 652a5e6 commit 57e6246

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

src/display/color/ColorSpectrum.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ var GetColor = require('./GetColor');
99
/**
1010
* Return an array of Colors in a Color Spectrum.
1111
*
12+
* The spectrum colors flow in the order: red, yellow, green, blue.
13+
*
14+
* By default this function will return an array with 1024 elements in.
15+
*
16+
* However, you can reduce this to a smaller quantity if needed, by specitying the `limit` parameter.
17+
*
1218
* @function Phaser.Display.Color.ColorSpectrum
1319
* @since 3.50.0
1420
*
15-
* @return {Phaser.Types.Display.ColorObject[]} An array containing 1024 elements, where each contains a Color Object.
21+
* @param {number} [limit=1024] - How many colors should be returned? The maximum is 1024 but you can set a smaller quantity if required.
22+
*
23+
* @return {Phaser.Types.Display.ColorObject[]} An array containing `limit` parameter number of elements, where each contains a Color Object.
1624
*/
17-
var ColorSpectrum = function ()
25+
var ColorSpectrum = function (limit)
1826
{
27+
if (limit === undefined) { limit = 1024; }
28+
1929
var colors = [];
2030

2131
var range = 255;
@@ -56,7 +66,26 @@ var ColorSpectrum = function ()
5666
colors.push({ r: r, g: g, b: b, color: GetColor(r, g, b) });
5767
}
5868

59-
return colors;
69+
if (limit === 1024)
70+
{
71+
return colors;
72+
}
73+
else
74+
{
75+
var out = [];
76+
77+
var t = 0;
78+
var inc = 1024 / limit;
79+
80+
for (i = 0; i < limit; i++)
81+
{
82+
out.push(colors[Math.floor(t)]);
83+
84+
t += inc;
85+
}
86+
87+
return out;
88+
}
6089
};
6190

6291
module.exports = ColorSpectrum;

0 commit comments

Comments
 (0)