Skip to content

Commit fccd58b

Browse files
committed
The HSVToRGB function can now take an optional out argument, which is either a HSVColorObject or a Color object, and the results will be set into that object instead of creating a new one.
1 parent 664d5ef commit fccd58b

1 file changed

Lines changed: 44 additions & 27 deletions

File tree

src/display/color/HSVToRGB.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ var GetColor = require('./GetColor');
1515
* @function Phaser.Display.Color.HSVToRGB
1616
* @since 3.0.0
1717
*
18-
* @param {number} h - The hue, in the range 0 - 1.
19-
* @param {number} s - The saturation, in the range 0 - 1.
20-
* @param {number} v - The value, in the range 0 - 1.
18+
* @param {number} h - The hue, in the range 0 - 1. This is the base color.
19+
* @param {number} s - The saturation, in the range 0 - 1. This controls how much of the hue will be in the final color, where 1 is fully saturated and 0 will give you white.
20+
* @param {number} v - The value, in the range 0 - 1. This controls how dark the color is. Where 1 is as bright as possible and 0 is black.
21+
* @param {(ColorObject|Phaser.Display.Color)} [out] - A Color object to store the results in. If not given a new ColorObject will be created.
2122
*
22-
* @return {ColorObject} An object with the red, green and blue values set in the r, g and b properties.
23+
* @return {(ColorObject|Phaser.Display.Color)} An object with the red, green and blue values set in the r, g and b properties.
2324
*/
24-
var HSVToRGB = function (h, s, v)
25+
var HSVToRGB = function (h, s, v, out)
2526
{
2627
if (s === undefined) { s = 1; }
2728
if (v === undefined) { v = 1; }
@@ -35,44 +36,60 @@ var HSVToRGB = function (h, s, v)
3536

3637
v = Math.floor(v *= 255);
3738

38-
var output = { r: v, g: v, b: v, color: 0 };
39+
var r = v;
40+
var g = v;
41+
var b = v;
3942

40-
var r = i % 6;
43+
var c = i % 6;
4144

42-
if (r === 0)
45+
if (c === 0)
4346
{
44-
output.g = t;
45-
output.b = p;
47+
g = t;
48+
b = p;
4649
}
47-
else if (r === 1)
50+
else if (c === 1)
4851
{
49-
output.r = q;
50-
output.b = p;
52+
r = q;
53+
b = p;
5154
}
52-
else if (r === 2)
55+
else if (c === 2)
5356
{
54-
output.r = p;
55-
output.b = t;
57+
r = p;
58+
b = t;
5659
}
57-
else if (r === 3)
60+
else if (c === 3)
5861
{
59-
output.r = p;
60-
output.g = q;
62+
r = p;
63+
g = q;
6164
}
62-
else if (r === 4)
65+
else if (c === 4)
6366
{
64-
output.r = t;
65-
output.g = p;
67+
r = t;
68+
g = p;
6669
}
67-
else if (r === 5)
70+
else if (c === 5)
6871
{
69-
output.g = p;
70-
output.b = q;
72+
g = p;
73+
b = q;
7174
}
7275

73-
output.color = GetColor(output.r, output.g, output.b);
76+
if (!out)
77+
{
78+
return { r: r, g: g, b: b, color: GetColor(r, g, b) };
79+
}
80+
else if (out.setTo)
81+
{
82+
return out.setTo(r, g, b, out.alpha, false);
83+
}
84+
else
85+
{
86+
out.r = r;
87+
out.g = g;
88+
out.b = b;
89+
out.color = GetColor(r, g, b);
7490

75-
return output;
91+
return out;
92+
}
7693
};
7794

7895
module.exports = HSVToRGB;

0 commit comments

Comments
 (0)