Skip to content

Commit 173786c

Browse files
committed
Color.createColor now populates the color property of the returned object with the results of Phaser.Color.getColor.
Color.createColor now has a `color32` property with the results of `Phaser.Color.getColor32`. Color.hexToColor has been optimised to inline the regex and has moved the createColor call so it now populates the color object fully, not just setting the r,g,b properties.
1 parent 2794d94 commit 173786c

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ Version 2.1.2 - "Whitebridge" - in development
102102
* jsdoc fixes (thanks @danxexe #1209)
103103
* AnimationParser is now using `value` instead of `nodeValue` when parsing atlas XML files, avoiding Chrome deprecation warnings (thanks @valtterip #1189)
104104
* Color.webToColor restored. Converts a CSS rgba color into a native color value.
105-
* Color.createColor now populates the color objects `color` and `color32` properties with the results of `Phaser.Color.getColor` and `getColor32` respectively.
105+
* Color.createColor now populates the `color` property of the returned object with the results of `Phaser.Color.getColor`.
106+
* Color.createColor now has a `color32` property with the results of `Phaser.Color.getColor32`.
107+
* Color.hexToColor has been optimised to inline the regex and has moved the createColor call so it now populates the color object fully, not just setting the r,g,b properties.
106108

107109

108110

src/utils/Color.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,12 @@ Phaser.Color = {
439439
*/
440440
createColor: function (r, g, b, a, h, s, l, v) {
441441

442-
var out = { r: r || 0, g: g || 0, b: b || 0, a: a || 1, h: h || 0, s: s || 0, l: l || 0, v: v || 0, color: 0, color32: 0 };
443-
444-
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
442+
var out = { r: r || 0, g: g || 0, b: b || 0, a: a || 1, h: h || 0, s: s || 0, l: l || 0, v: v || 0, color: 0, color32: 0, rgba: '' };
445443

446444
out.color = Phaser.Color.getColor(out.r, out.g, out.b);
447445
out.color32 = Phaser.Color.getColor32(out.a, out.r, out.g, out.b);
448446

449-
return out;
447+
return Phaser.Color.updateColor(out);
450448

451449
},
452450

@@ -460,7 +458,7 @@ Phaser.Color = {
460458
*/
461459
updateColor: function (out) {
462460

463-
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
461+
out.rgba = 'rgba(' + out.r.toString() + ',' + out.g.toString() + ',' + out.b.toString() + ',' + out.a.toString() + ')';
464462

465463
return out;
466464

@@ -553,30 +551,34 @@ Phaser.Color = {
553551
* @method Phaser.Color.hexToColor
554552
* @static
555553
* @param {string} hex - The hex string to convert. Can be in the short-hand format `#03f` or `#0033ff`.
556-
* @param {object} [out] - An object into which 3 properties will be created: r, g and b. If not provided a new object will be created.
554+
* @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.
557555
* @return {object} An object with the red, green and blue values set in the r, g and b properties.
558556
*/
559557
hexToColor: function (hex, out) {
560558

561-
if (!out)
562-
{
563-
out = Phaser.Color.createColor();
564-
}
565-
566559
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
567-
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
568-
569-
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
560+
hex = hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function(m, r, g, b) {
570561
return r + r + g + g + b + b;
571562
});
572563

573564
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
574565

575566
if (result)
576567
{
577-
out.r = parseInt(result[1], 16);
578-
out.g = parseInt(result[2], 16);
579-
out.b = parseInt(result[3], 16);
568+
var r = parseInt(result[1], 16);
569+
var g = parseInt(result[2], 16);
570+
var b = parseInt(result[3], 16);
571+
572+
if (!out)
573+
{
574+
out = Phaser.Color.createColor(r, g, b);
575+
}
576+
else
577+
{
578+
out.r = r;
579+
out.g = g;
580+
out.b = b;
581+
}
580582
}
581583

582584
return out;

0 commit comments

Comments
 (0)