@@ -548,20 +548,24 @@ Phaser.Color = {
548548 /**
549549 * Converts a hex string into a Phaser Color object.
550550 *
551+ * 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.
552+ *
553+ * An alpha channel is _not_ supported.
554+ *
551555 * @method Phaser.Color.hexToColor
552556 * @static
553- * @param {string } hex - The hex string to convert. Can be in the short-hand format `#03f` or `#0033ff` .
557+ * @param {string } hex - The color string in a hex format.
554558 * @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.
555559 * @return {object } An object with the red, green and blue values set in the r, g and b properties.
556560 */
557561 hexToColor : function ( hex , out ) {
558562
559563 // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
560- hex = hex . replace ( / ^ # ? ( [ a - f \d ] ) ( [ a - f \d ] ) ( [ a - f \d ] ) $ / i, function ( m , r , g , b ) {
564+ hex = hex . replace ( / ^ (?: # | 0 x ) ? ( [ a - f \d ] ) ( [ a - f \d ] ) ( [ a - f \d ] ) $ / i, function ( m , r , g , b ) {
561565 return r + r + g + g + b + b ;
562566 } ) ;
563567
564- var result = / ^ # ? ( [ a - f \d ] { 2 } ) ( [ a - f \d ] { 2 } ) ( [ a - f \d ] { 2 } ) $ / i. exec ( hex ) ;
568+ var result = / ^ (?: # | 0 x ) ? ( [ a - f \d ] { 2 } ) ( [ a - f \d ] { 2 } ) ( [ a - f \d ] { 2 } ) $ / i. exec ( hex ) ;
565569
566570 if ( result )
567571 {
@@ -588,11 +592,13 @@ Phaser.Color = {
588592 /**
589593 * Converts a CSS 'web' string into a Phaser Color object.
590594 *
595+ * 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].
596+ *
591597 * @method Phaser.Color.webToColor
592598 * @static
593- * @param {string } web - The web string in the format: 'rgba(r,g,b,a)'
594- * @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.
595- * @return {object } An object with the red, green and blue values set in the r, g and b properties.
599+ * @param {string } web - The color string in CSS 'web' format.
600+ * @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.
601+ * @return {object } An object with the red, green, blue and alpha values set in the r, g, b and a properties.
596602 */
597603 webToColor : function ( web , out ) {
598604
@@ -608,12 +614,63 @@ Phaser.Color = {
608614 out . r = parseInt ( result [ 1 ] , 10 ) ;
609615 out . g = parseInt ( result [ 2 ] , 10 ) ;
610616 out . b = parseInt ( result [ 3 ] , 10 ) ;
617+ out . a = result [ 4 ] !== undefined ? parseFloat ( result [ 4 ] ) : 1 ;
611618 }
612619
613620 return out ;
614621
615622 } ,
616623
624+ /**
625+ * Converts a string - either in "hex" or a "CSS 'web' string" into a Phaser Color object.
626+ *
627+ * An alpha channel is _not_ supported when specifying a hex string.
628+ *
629+ * @method Phaser.Color.valueToColor
630+ * @static
631+ * @param {string|number } value - Can either be a string (see `hexToColor` and `webToColor` for the supported formats) or a number (see `getRGB`).
632+ * @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.
633+ * @return {object } An object with the red, green, blue and alpha values set in the r, g, b and a properties.
634+ */
635+ valueToColor : function ( value , out ) {
636+
637+ // The behavior is not consistent between hexToColor/webToColor on invalid input.
638+ // This unifies both by returning a new object, but returning null may be better.
639+ if ( ! out )
640+ {
641+ out = Phaser . Color . createColor ( ) ;
642+ }
643+
644+ if ( typeof value === 'string' )
645+ {
646+ if ( value . indexOf ( 'rgb' ) === 0 )
647+ {
648+ return Phaser . Color . webToColor ( value , out ) ;
649+ }
650+ else
651+ {
652+ // `hexToColor` does not support alpha; match `createColor`.
653+ out . a = 1 ;
654+ return Phaser . Color . hexToColor ( value , out ) ;
655+ }
656+ }
657+ else if ( typeof value === 'number' )
658+ {
659+ // `getRGB` does not take optional object to modify;
660+ // alpha is also adjusted to match `createColor`.
661+ var tempColor = Phaser . Color . getRGB ( value ) ;
662+ out . r = tempColor . r ;
663+ out . g = tempColor . g ;
664+ out . b = tempColor . b ;
665+ out . a = tempColor . a / 255 ;
666+ return out ;
667+ }
668+ else
669+ {
670+ return out ;
671+ }
672+
673+ } ,
617674
618675 /**
619676 * Return a string containing a hex representation of the given color component.
0 commit comments