Skip to content

Commit c195998

Browse files
committed
Color.webToColor restored. Converts a CSS rgba color into a native color value.
1 parent f45f19c commit c195998

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Version 2.1.2 - "Whitebridge" - in development
9696
* Added the `sourceRect` and `maskRect` parameters back into `BitmapData.alphaMask` as they were accidentally removed in 2.1 (thanks seejay92)
9797
* jsdoc fixes (thanks @danxexe #1209)
9898
* AnimationParser is now using `value` instead of `nodeValue` when parsing atlas XML files, avoiding Chrome deprecation warnings (thanks @valtterip #1189)
99+
* Color.webToColor restored. Converts a CSS rgba color into a native color value.
99100

100101

101102

src/utils/Color.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,36 @@ Phaser.Color = {
580580

581581
},
582582

583+
/**
584+
* Converts a CSS 'web' string into a Phaser Color object.
585+
*
586+
* @method Phaser.Color.webToColor
587+
* @static
588+
* @param {string} web - The web string in the format: 'rgba(r,g,b,a)'
589+
* @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.
590+
* @return {object} An object with the red, green and blue values set in the r, g and b properties.
591+
*/
592+
webToColor: function (web, out) {
593+
594+
if (!out)
595+
{
596+
out = Phaser.Color.createColor();
597+
}
598+
599+
var result = /^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/.exec(web);
600+
601+
if (result)
602+
{
603+
out.r = parseInt(result[1], 10);
604+
out.g = parseInt(result[2], 10);
605+
out.b = parseInt(result[3], 10);
606+
}
607+
608+
return out;
609+
610+
},
611+
612+
583613
/**
584614
* Return a string containing a hex representation of the given color component.
585615
*

0 commit comments

Comments
 (0)