Skip to content

Commit 977054a

Browse files
committed
Added new RGB class to encapsulate color data
1 parent 4549fb4 commit 977054a

2 files changed

Lines changed: 141 additions & 1 deletion

File tree

src/display/RGB.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var Class = require('../utils/Class');
8+
var NOOP = require('../utils/NOOP');
9+
10+
/**
11+
* @classdesc
12+
* The RGB class holds a single color value and allows for easy modification and reading of it,
13+
* with optional on-change callback notification and a dirty flag.
14+
*
15+
* @class RGB
16+
* @memberof Phaser.Display
17+
* @constructor
18+
* @since 3.50.0
19+
*
20+
* @param {number} [red=0] - The red color value. A number between 0 and 1.
21+
* @param {number} [green=0] - The green color value. A number between 0 and 1.
22+
* @param {number} [blue=0] - The blue color value. A number between 0 and 1.
23+
*/
24+
var RGB = new Class({
25+
26+
initialize:
27+
28+
function RGB (red, green, blue)
29+
{
30+
/**
31+
* Cached RGB values.
32+
*
33+
* @name Phaser.Display.RGB#_rgb
34+
* @type {number[]}
35+
* @private
36+
* @since 3.50.0
37+
*/
38+
this._rgb = [ 0, 0, 0 ];
39+
40+
/**
41+
* This callback will be invoked each time one of the RGB color values change.
42+
*
43+
* The callback is sent the new color values as the parameters.
44+
*
45+
* @name Phaser.Display.RGB#onChangeCallback
46+
* @type {function}
47+
* @since 3.50.0
48+
*/
49+
this.onChangeCallback = NOOP;
50+
51+
/**
52+
* Is this color dirty?
53+
*
54+
* @name Phaser.Display.RGB#dirty
55+
* @type {boolean}
56+
* @since 3.50.0
57+
*/
58+
this.dirty = false;
59+
60+
this.set(red, green, blue);
61+
},
62+
63+
set: function (red, green, blue)
64+
{
65+
if (red === undefined) { red = 0; }
66+
if (green === undefined) { green = 0; }
67+
if (blue === undefined) { blue = 0; }
68+
69+
this._rgb = [ red, green, blue ];
70+
71+
this.onChange();
72+
73+
return this;
74+
},
75+
76+
equals: function (red, green, blue)
77+
{
78+
var rgb = this._rgb;
79+
80+
return (rgb.r === red && rgb.g === green && rgb.b === blue);
81+
},
82+
83+
onChange: function ()
84+
{
85+
this.dirty = true;
86+
87+
var rgb = this._rgb;
88+
89+
this.onChangeCallback.call(this, rgb[0], rgb[1], rgb[2]);
90+
},
91+
92+
r: {
93+
94+
get: function ()
95+
{
96+
return this._rgb[0];
97+
},
98+
99+
set: function (value)
100+
{
101+
this._rgb[0] = value;
102+
this.onChange();
103+
}
104+
105+
},
106+
107+
g: {
108+
109+
get: function ()
110+
{
111+
return this._rgb[1];
112+
},
113+
114+
set: function (value)
115+
{
116+
this._rgb[1] = value;
117+
this.onChange();
118+
}
119+
120+
},
121+
122+
b: {
123+
124+
get: function ()
125+
{
126+
return this._rgb[2];
127+
},
128+
129+
set: function (value)
130+
{
131+
this._rgb[2] = value;
132+
this.onChange();
133+
}
134+
135+
}
136+
137+
});
138+
139+
module.exports = RGB;

src/display/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
Bounds: require('./bounds'),
1616
Canvas: require('./canvas'),
1717
Color: require('./color'),
18-
Masks: require('./mask')
18+
Masks: require('./mask'),
19+
RGB: require('./RGB')
1920

2021
};

0 commit comments

Comments
 (0)