Skip to content

Commit 664d5ef

Browse files
committed
The RGBToHSV 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 4ac490e commit 664d5ef

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/display/color/RGBToHSV.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
*/
66

77
/**
8-
* @typedef {object} HSLColorObject
8+
* @typedef {object} HSVColorObject
99
*
1010
* @property {number} h - The hue color value. A number between 0 and 1
1111
* @property {number} s - The saturation color value. A number between 0 and 1
12-
* @property {number} l - The lightness color value. A number between 0 and 1
12+
* @property {number} v - The lightness color value. A number between 0 and 1
1313
*/
1414

1515
/**
@@ -24,11 +24,14 @@
2424
* @param {integer} r - The red color value. A number between 0 and 255.
2525
* @param {integer} g - The green color value. A number between 0 and 255.
2626
* @param {integer} b - The blue color value. A number between 0 and 255.
27+
* @param {(HSVColorObject|Phaser.Display.Color)} [out] - An object to store the color values in. If not given an HSV Color Object will be created.
2728
*
28-
* @return {HSLColorObject} An object with the properties `h`, `s` and `v`.
29+
* @return {(HSVColorObject|Phaser.Display.Color)} An object with the properties `h`, `s` and `v` set.
2930
*/
30-
var RGBToHSV = function (r, g, b)
31+
var RGBToHSV = function (r, g, b, out)
3132
{
33+
if (out === undefined) { out = { h: 0, s: 0, v: 0 }; }
34+
3235
r /= 255;
3336
g /= 255;
3437
b /= 255;
@@ -60,7 +63,20 @@ var RGBToHSV = function (r, g, b)
6063
h /= 6;
6164
}
6265

63-
return { h: h, s: s, v: v };
66+
if (out.hasOwnProperty('_h'))
67+
{
68+
out._h = h;
69+
out._s = s;
70+
out._v = v;
71+
}
72+
else
73+
{
74+
out.h = h;
75+
out.s = s;
76+
out.v = v;
77+
}
78+
79+
return out;
6480
};
6581

6682
module.exports = RGBToHSV;

0 commit comments

Comments
 (0)