Skip to content

Commit 2f2d8d6

Browse files
committed
Final few Color functions. Game Config now uses Color object for Background Color.
1 parent e4f72c9 commit 2f2d8d6

11 files changed

Lines changed: 168 additions & 11 deletions

File tree

v3/src/boot/Config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var MATH = require('../math');
88
var CONST = require('../const');
99
var NOOP = require('../utils/NOOP');
1010
var GetObjectValue = require('../utils/object/GetObjectValue');
11+
var ValueToColor = require('../graphics/color/ValueToColor');
1112

1213
var defaultBannerColor = [
1314
'#ff0000',
@@ -58,7 +59,7 @@ var Config = function (config)
5859
this.pixelArt = GetObjectValue(config, 'pixelArt', false);
5960
this.transparent = GetObjectValue(config, 'transparent', false);
6061
this.clearBeforeRender = GetObjectValue(config, 'clearBeforeRender', true);
61-
this.backgroundColor = GetObjectValue(config, 'backgroundColor', '#000000');
62+
this.backgroundColor = ValueToColor(GetObjectValue(config, 'backgroundColor', 0));
6263

6364
// Callbacks
6465
this.preBoot = GetObjectValue(config, 'callbacks.preBoot', NOOP);

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '1c054190-ee36-11e6-8010-4ba93e0d034d'
2+
build: '88f5e960-f031-11e6-8333-5dcb4000555b'
33
};
44
module.exports = CHECKSUM;

v3/src/graphics/color/Color.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ var Color = function (red, green, blue, alpha)
99
if (alpha === undefined) { alpha = 255; }
1010

1111
// All private
12-
this.r = red;
13-
this.g = green;
14-
this.b = blue;
15-
this.a = alpha;
12+
this.r = 0;
13+
this.g = 0;
14+
this.b = 0;
15+
this.a = 255;
1616

1717
this.gl = [ 0.0, 0.0, 0.0, 1.0 ];
1818

@@ -22,7 +22,7 @@ var Color = function (red, green, blue, alpha)
2222

2323
this.dirty = true;
2424

25-
this.update();
25+
this.setTo(red, green, blue, alpha);
2626
};
2727

2828
Color.prototype.contrusctor = Color;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var Color = require('./Color');
2+
3+
/**
4+
* Converts a hex string into a Phaser Color object.
5+
*
6+
* The hex string can supplied as `'#0033ff'` or the short-hand format of `'#03f'`; it can begin with an optional "#" or "0x", or be unprefixed.
7+
*
8+
* An alpha channel is _not_ supported.
9+
*
10+
* @method Phaser.Color.hexToColor
11+
* @static
12+
* @param {string} hex - The color string in a hex format.
13+
* @param {object} [out] - An object into which 3 properties will be created or set: r, g and b. If not provided a new object will be created.
14+
* @return {object} An object with the red, green and blue values set in the r, g and b properties.
15+
*/
16+
var HexStringToColor = function (hex)
17+
{
18+
var color = new Color();
19+
20+
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
21+
hex = hex.replace(/^(?:#|0x)?([a-f\d])([a-f\d])([a-f\d])$/i, function(m, r, g, b) {
22+
return r + r + g + g + b + b;
23+
});
24+
25+
var result = /^(?:#|0x)?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
26+
27+
if (result)
28+
{
29+
var r = parseInt(result[1], 16);
30+
var g = parseInt(result[2], 16);
31+
var b = parseInt(result[3], 16);
32+
33+
color.setTo(r, g, b);
34+
}
35+
36+
return color;
37+
};
38+
39+
module.exports = HexStringToColor;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var Color = require('./Color');
2+
var IntegerToRGB = require('./IntegerToRGB');
3+
4+
var IntegerToColor = function (input)
5+
{
6+
var rgb = IntegerToRGB(input);
7+
8+
return new Color(rgb.r, rgb.g, rgb.b, rgb.a);
9+
};
10+
11+
module.exports = IntegerToColor;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Return the component parts of a color as an Object with the properties alpha, red, green, blue.
3+
*
4+
* Alpha will only be set if it exist in the given color (0xAARRGGBB)
5+
*
6+
* @method Phaser.Color.getRGB
7+
* @static
8+
* @param {number} color - Color in RGB (0xRRGGBB) or ARGB format (0xAARRGGBB).
9+
* @return {object} An Object with properties: alpha, red, green, blue (also r, g, b and a). Alpha will only be present if a color value > 16777215 was given.
10+
*/
11+
var IntegerToRGB = function (color)
12+
{
13+
if (color > 16777215)
14+
{
15+
// The color value has an alpha component
16+
return {
17+
a: color >>> 24,
18+
r: color >> 16 & 0xFF,
19+
g: color >> 8 & 0xFF,
20+
b: color & 0xFF
21+
};
22+
}
23+
else
24+
{
25+
return {
26+
a: 255,
27+
r: color >> 16 & 0xFF,
28+
g: color >> 8 & 0xFF,
29+
b: color & 0xFF
30+
};
31+
}
32+
};
33+
34+
module.exports = IntegerToRGB;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var Color = require('./Color');
2+
3+
/**
4+
* Converts a CSS 'web' string into a Phaser Color object.
5+
*
6+
* The web string can be in the format `'rgb(r,g,b)'` or `'rgba(r,g,b,a)'` where r/g/b are in the range [0..255] and a is in the range [0..1].
7+
*
8+
* @method Phaser.Color.webToColor
9+
* @static
10+
* @param {string} web - The color string in CSS 'web' format.
11+
* @param {object} [out] - An object into which 4 properties will be created: r, g, b and a. If not provided a new object will be created.
12+
* @return {object} An object with the red, green, blue and alpha values set in the r, g, b and a properties.
13+
*/
14+
var RGBStringToColor = function (rgb)
15+
{
16+
var color = new Color();
17+
18+
var result = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+(?:\.\d+)?))?\s*\)$/.exec(rgb.toLowerCase());
19+
20+
if (result)
21+
{
22+
var r = parseInt(result[1], 10);
23+
var g = parseInt(result[2], 10);
24+
var b = parseInt(result[3], 10);
25+
var a = (result[4] !== undefined) ? parseFloat(result[4]) : 1;
26+
27+
color.setTo(r, g, b, a * 255);
28+
}
29+
30+
return color;
31+
};
32+
33+
module.exports = RGBStringToColor;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var RGBStringToColor = require('./RGBStringToColor');
2+
var HexStringToColor = require('./HexStringToColor');
3+
var IntegerToColor = require('./IntegerToColor');
4+
5+
var ValueToColor = function (input)
6+
{
7+
if (typeof input === 'string')
8+
{
9+
if (input.substr(0, 3).toLowerCase() === 'rgb')
10+
{
11+
return RGBStringToColor(input);
12+
}
13+
else
14+
{
15+
return HexStringToColor(input);
16+
}
17+
}
18+
else if (typeof input === 'number')
19+
{
20+
return IntegerToColor(input);
21+
}
22+
};
23+
24+
module.exports = ValueToColor;

v3/src/graphics/color/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
var Color = require('./Color');
44

5-
Color.GetColor = require('./GetColor');
6-
Color.GetColor32 = require('./GetColor32');
75
Color.ColorToRGBA = require('./ColorToRGBA');
86
Color.ComponentToHex = require('./ComponentToHex');
97
Color.CSSToColor = require('./CSSToColor');
10-
Color.HueToComponent = require('./HueToComponent');
8+
Color.GetColor = require('./GetColor');
9+
Color.GetColor32 = require('./GetColor32');
10+
Color.HexStringToColor = require('./HexStringToColor');
1111
Color.HSLToColor = require('./HSLToColor');
1212
Color.HSVColorWheel = require('./HSVColorWheel');
1313
Color.HSVToRGB = require('./HSVToRGB');
14+
Color.HueToComponent = require('./HueToComponent');
15+
Color.IntegerToColor = require('./IntegerToColor');
16+
Color.IntegerToRGB = require('./IntegerToRGB');
17+
Color.Interpolate = require('./Interpolate');
1418
Color.RandomRGB = require('./RandomRGB');
19+
Color.RGBStringToColor = require('./RGBStringToColor');
1520
Color.RGBToHSV = require('./RGBToHSV');
1621
Color.RGBToString = require('./RGBToString');
17-
Color.Interpolate = require('./Interpolate');
22+
Color.ValueToColor = require('./ValueToColor');
1823

1924
module.exports = Color;

v3/src/graphics/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Phaser.Graphics
2+
3+
module.exports = {
4+
5+
BlendModes: require('./blendmodes'),
6+
Color: require('./color')
7+
8+
};

0 commit comments

Comments
 (0)