|
| 1 | +/** |
| 2 | +* @author Richard Davey <rich@photonstorm.com> |
| 3 | +* @copyright 2015 Photon Storm Ltd. |
| 4 | +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | +* The Phaser.Color stub. This stub only includes the bare minimum functions that Phaser needs. |
| 9 | +* |
| 10 | +* @class Phaser.Color |
| 11 | +*/ |
| 12 | +Phaser.Color = { |
| 13 | + |
| 14 | + /** |
| 15 | + * Converts a value - a "hex" string, a "CSS 'web' string", or a number - into red, green, blue, and alpha components. |
| 16 | + * |
| 17 | + * The value can be a string (see `hexToColor` and `webToColor` for the supported formats) or a packed integer (see `getRGB`). |
| 18 | + * |
| 19 | + * An alpha channel is _not_ supported when specifying a hex string. |
| 20 | + * |
| 21 | + * @method Phaser.Color.valueToColor |
| 22 | + * @static |
| 23 | + * @param {string|number} value - The color expressed as a recognized string format or a packed integer. |
| 24 | + * @param {object} [out] - The object to use for the output. If not provided a new object will be created. |
| 25 | + * @return {object} The (`out`) object with the red, green, blue, and alpha values set as the r/g/b/a properties. |
| 26 | + */ |
| 27 | + valueToColor: function (value, out) { |
| 28 | + |
| 29 | + if (typeof value === 'string') |
| 30 | + { |
| 31 | + if (value.indexOf('rgb') === 0) |
| 32 | + { |
| 33 | + return Phaser.Color.webToColor(value, out); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + // `hexToColor` does not support alpha; match `createColor`. |
| 38 | + out.a = 1; |
| 39 | + return Phaser.Color.hexToColor(value, out); |
| 40 | + } |
| 41 | + } |
| 42 | + else if (typeof value === 'number') |
| 43 | + { |
| 44 | + // `getRGB` does not take optional object to modify; |
| 45 | + // alpha is also adjusted to match `createColor`. |
| 46 | + var tempColor = Phaser.Color.getRGB(value); |
| 47 | + out.r = tempColor.r; |
| 48 | + out.g = tempColor.g; |
| 49 | + out.b = tempColor.b; |
| 50 | + out.a = tempColor.a / 255; |
| 51 | + return out; |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + return out; |
| 56 | + } |
| 57 | + |
| 58 | + }, |
| 59 | + |
| 60 | + /** |
| 61 | + * Return the component parts of a color as an Object with the properties alpha, red, green, blue. |
| 62 | + * |
| 63 | + * Alpha will only be set if it exist in the given color (0xAARRGGBB) |
| 64 | + * |
| 65 | + * @method Phaser.Color.getRGB |
| 66 | + * @static |
| 67 | + * @param {number} color - Color in RGB (0xRRGGBB) or ARGB format (0xAARRGGBB). |
| 68 | + * @returns {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. |
| 69 | + */ |
| 70 | + getRGB: function (color) { |
| 71 | + |
| 72 | + if (color > 16777215) |
| 73 | + { |
| 74 | + // The color value has an alpha component |
| 75 | + return { |
| 76 | + alpha: color >>> 24, |
| 77 | + red: color >> 16 & 0xFF, |
| 78 | + green: color >> 8 & 0xFF, |
| 79 | + blue: color & 0xFF, |
| 80 | + a: color >>> 24, |
| 81 | + r: color >> 16 & 0xFF, |
| 82 | + g: color >> 8 & 0xFF, |
| 83 | + b: color & 0xFF |
| 84 | + }; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + return { |
| 89 | + alpha: 255, |
| 90 | + red: color >> 16 & 0xFF, |
| 91 | + green: color >> 8 & 0xFF, |
| 92 | + blue: color & 0xFF, |
| 93 | + a: 255, |
| 94 | + r: color >> 16 & 0xFF, |
| 95 | + g: color >> 8 & 0xFF, |
| 96 | + b: color & 0xFF |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + }, |
| 101 | + |
| 102 | + /** |
| 103 | + * Converts a CSS 'web' string into a Phaser Color object. |
| 104 | + * |
| 105 | + * 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]. |
| 106 | + * |
| 107 | + * @method Phaser.Color.webToColor |
| 108 | + * @static |
| 109 | + * @param {string} web - The color string in CSS 'web' format. |
| 110 | + * @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. |
| 111 | + * @return {object} An object with the red, green, blue and alpha values set in the r, g, b and a properties. |
| 112 | + */ |
| 113 | + webToColor: function (web, out) { |
| 114 | + |
| 115 | + var result = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+(?:\.\d+)?))?\s*\)$/.exec(web); |
| 116 | + |
| 117 | + if (result) |
| 118 | + { |
| 119 | + out.r = parseInt(result[1], 10); |
| 120 | + out.g = parseInt(result[2], 10); |
| 121 | + out.b = parseInt(result[3], 10); |
| 122 | + out.a = result[4] !== undefined ? parseFloat(result[4]) : 1; |
| 123 | + Phaser.Color.updateColor(out); |
| 124 | + } |
| 125 | + |
| 126 | + return out; |
| 127 | + |
| 128 | + }, |
| 129 | + |
| 130 | + /** |
| 131 | + * Converts a hex string into a Phaser Color object. |
| 132 | + * |
| 133 | + * 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. |
| 134 | + * |
| 135 | + * An alpha channel is _not_ supported. |
| 136 | + * |
| 137 | + * @method Phaser.Color.hexToColor |
| 138 | + * @static |
| 139 | + * @param {string} hex - The color string in a hex format. |
| 140 | + * @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. |
| 141 | + * @return {object} An object with the red, green and blue values set in the r, g and b properties. |
| 142 | + */ |
| 143 | + hexToColor: function (hex, out) { |
| 144 | + |
| 145 | + // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") |
| 146 | + hex = hex.replace(/^(?:#|0x)?([a-f\d])([a-f\d])([a-f\d])$/i, function(m, r, g, b) { |
| 147 | + return r + r + g + g + b + b; |
| 148 | + }); |
| 149 | + |
| 150 | + var result = /^(?:#|0x)?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
| 151 | + |
| 152 | + if (result) |
| 153 | + { |
| 154 | + var r = parseInt(result[1], 16); |
| 155 | + var g = parseInt(result[2], 16); |
| 156 | + var b = parseInt(result[3], 16); |
| 157 | + |
| 158 | + out.r = r; |
| 159 | + out.g = g; |
| 160 | + out.b = b; |
| 161 | + } |
| 162 | + |
| 163 | + return out; |
| 164 | + |
| 165 | + }, |
| 166 | + |
| 167 | + /** |
| 168 | + * Takes a color object and updates the rgba property. |
| 169 | + * |
| 170 | + * @method Phaser.Color.updateColor |
| 171 | + * @static |
| 172 | + * @param {object} out - The color object to update. |
| 173 | + * @returns {number} A native color value integer (format: 0xAARRGGBB). |
| 174 | + */ |
| 175 | + updateColor: function (out) { |
| 176 | + |
| 177 | + out.rgba = 'rgba(' + out.r.toString() + ',' + out.g.toString() + ',' + out.b.toString() + ',' + out.a.toString() + ')'; |
| 178 | + out.color = Phaser.Color.getColor(out.r, out.g, out.b); |
| 179 | + out.color32 = Phaser.Color.getColor32(out.a, out.r, out.g, out.b); |
| 180 | + |
| 181 | + return out; |
| 182 | + |
| 183 | + }, |
| 184 | + |
| 185 | + /** |
| 186 | + * Given an alpha and 3 color values this will return an integer representation of it. |
| 187 | + * |
| 188 | + * @method Phaser.Color.getColor32 |
| 189 | + * @static |
| 190 | + * @param {number} a - The alpha color component, in the range 0 - 255. |
| 191 | + * @param {number} r - The red color component, in the range 0 - 255. |
| 192 | + * @param {number} g - The green color component, in the range 0 - 255. |
| 193 | + * @param {number} b - The blue color component, in the range 0 - 255. |
| 194 | + * @returns {number} A native color value integer (format: 0xAARRGGBB). |
| 195 | + */ |
| 196 | + getColor32: function (a, r, g, b) { |
| 197 | + |
| 198 | + return a << 24 | r << 16 | g << 8 | b; |
| 199 | + |
| 200 | + }, |
| 201 | + |
| 202 | + /** |
| 203 | + * Given 3 color values this will return an integer representation of it. |
| 204 | + * |
| 205 | + * @method Phaser.Color.getColor |
| 206 | + * @static |
| 207 | + * @param {number} r - The red color component, in the range 0 - 255. |
| 208 | + * @param {number} g - The green color component, in the range 0 - 255. |
| 209 | + * @param {number} b - The blue color component, in the range 0 - 255. |
| 210 | + * @returns {number} A native color value integer (format: 0xRRGGBB). |
| 211 | + */ |
| 212 | + getColor: function (r, g, b) { |
| 213 | + |
| 214 | + return r << 16 | g << 8 | b; |
| 215 | + |
| 216 | + } |
| 217 | + |
| 218 | +}; |
0 commit comments