mirror of
https://github.com/photonstorm/phaser
synced 2024-11-10 07:04:31 +00:00
Finally - fully working setHSL and shiftHSL.
This commit is contained in:
parent
6fb4ad06d7
commit
11ca2deaf6
5 changed files with 805 additions and 462 deletions
|
@ -76,6 +76,7 @@ Version 2.0.4 - "Mos Shirare" - in development
|
|||
* If Tween.yoyo was true but repeat was 0 then it wouldn't yoyo. Now if yoyo is set, but not repeat, the repeat count gets set to 1 (thanks @hilts-vaughan, fix #744)
|
||||
* RandomDataGenerator.integerInRange uses a new method of rounding the value to an integer to avoid distribution probability issues (thanks PhaserFan)
|
||||
* Updated the Device little / big endianess check.
|
||||
* Time has been updated so that physicsElapsed can never be zero (falls back to 1/60), also fixes p2 elapsed time bug (thanks @georgiee, fix #758)
|
||||
|
||||
|
||||
### New Features
|
||||
|
@ -92,6 +93,7 @@ Version 2.0.4 - "Mos Shirare" - in development
|
|||
* Game.scratch is a single handy BitmapData instance that can be used as a visual scratch-pad, for off-screen bitmap manipulation (and is used as such by BitmapData itself).
|
||||
* Device.support32bit is a new boolean that sets if the context supports 32bit pixel manipulation using array buffer views or not.
|
||||
* BitmapData.processPixelRGB lets you perform a custom callback on every pixel in the BitmapData.
|
||||
* P2.World now has its own pause and resume methods, so you can pause the physics simulation independent of your game (thanks @georgiee)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
|
|
@ -85,7 +85,8 @@ Phaser.BitmapData = function (game, key, width, height) {
|
|||
/**
|
||||
* @property {Int32Array} pixels - An Int32Array view into BitmapData.buffer.
|
||||
*/
|
||||
this.pixels = new Int32Array(this.buffer);
|
||||
// this.pixels = new Int32Array(this.buffer);
|
||||
this.pixels = new Uint32Array(this.buffer);
|
||||
|
||||
/**
|
||||
* @property {PIXI.BaseTexture} baseTexture - The PIXI.BaseTexture.
|
||||
|
@ -121,15 +122,28 @@ Phaser.BitmapData = function (game, key, width, height) {
|
|||
*/
|
||||
this.dirty = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} littleEndian - True if Little Endian, false if Big Endian.
|
||||
*/
|
||||
this.littleEndian = this.game.device.littleEndian;
|
||||
|
||||
// Aliases
|
||||
this.cls = this.clear;
|
||||
this.update = this.refreshBuffer;
|
||||
|
||||
/**
|
||||
* @property {number} _tempR - Internal cache var.
|
||||
* @private
|
||||
*/
|
||||
this._tempR = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _tempG - Internal cache var.
|
||||
* @private
|
||||
*/
|
||||
this._tempG = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _tempB - Internal cache var.
|
||||
* @private
|
||||
*/
|
||||
this._tempB = 0;
|
||||
|
||||
};
|
||||
|
||||
Phaser.BitmapData.prototype = {
|
||||
|
@ -232,6 +246,8 @@ Phaser.BitmapData.prototype = {
|
|||
*/
|
||||
|
||||
/**
|
||||
* DEPRECATED: This method will be removed in Phaser 2.1. Please use BitmapData.update instead.
|
||||
*
|
||||
* This re-creates the BitmapData.imageData from the current context.
|
||||
* It then re-builds the ArrayBuffer, the data Uint8ClampedArray reference and the pixels Int32Array.
|
||||
* If not given the dimensions defaults to the full size of the context.
|
||||
|
@ -261,7 +277,8 @@ Phaser.BitmapData.prototype = {
|
|||
}
|
||||
|
||||
this.data = this.imageData.data;
|
||||
this.pixels = new Int32Array(this.buffer);
|
||||
// this.pixels = new Int32Array(this.buffer);
|
||||
this.pixels = new Uint32Array(this.buffer);
|
||||
|
||||
},
|
||||
|
||||
|
@ -291,18 +308,17 @@ Phaser.BitmapData.prototype = {
|
|||
|
||||
var w = x + width;
|
||||
var h = y + height;
|
||||
var pixel = { r: 0, g: 0, b: 0, a: 0, rgba: '' };
|
||||
var pixel = Phaser.Color.createColor();
|
||||
var result = { r: 0, g: 0, b: 0, a: 0 };
|
||||
var color = 0;
|
||||
var dirty = false;
|
||||
|
||||
for (var ty = y; ty < h; ty++)
|
||||
{
|
||||
for (var tx = x; tx < w; tx++)
|
||||
{
|
||||
color = this.getPixel32(tx, ty);
|
||||
this.unpackPixel(color, pixel);
|
||||
result = callback.call(callbackContext, pixel, color);
|
||||
Phaser.Color.unpackPixel(this.getPixel32(tx, ty), pixel);
|
||||
|
||||
result = callback.call(callbackContext, pixel);
|
||||
|
||||
if (result !== false && result !== null)
|
||||
{
|
||||
|
@ -321,8 +337,58 @@ Phaser.BitmapData.prototype = {
|
|||
},
|
||||
|
||||
/**
|
||||
* Replaces all pixels matching the given RGBA values with the new RGBA values in the given region,
|
||||
* or the whole BitmapData if no region provided.
|
||||
* Scans through the area specified in this BitmapData and sends the color for every pixel to the given callback.
|
||||
* Whatever value the callback returns is set as the new color for that pixel, unless it returns the same color, in which case it's skipped.
|
||||
* Note that the format of the color received will be different depending on if the system is big or little endian.
|
||||
* It is expected that your callback will deal with endianess. If you'd rather Phaser did it then use processPixelRGB instead.
|
||||
*
|
||||
* @method Phaser.BitmapData#processPixel
|
||||
* @param {function} callback - The callback that will be sent each pixel color to be processed.
|
||||
* @param {object} callbackContext - The context under which the callback will be called.
|
||||
* @param {number} [x=0] - The x coordinate of the top-left of the region to process from.
|
||||
* @param {number} [y=0] - The y coordinate of the top-left of the region to process from.
|
||||
* @param {number} [width] - The width of the region to process.
|
||||
* @param {number} [height] - The height of the region to process.
|
||||
*/
|
||||
processPixel: function (callback, callbackContext, x, y, width, height) {
|
||||
|
||||
if (typeof x === 'undefined') { x = 0; }
|
||||
if (typeof y === 'undefined') { y = 0; }
|
||||
if (typeof width === 'undefined') { width = this.width; }
|
||||
if (typeof height === 'undefined') { height = this.height; }
|
||||
|
||||
var w = x + width;
|
||||
var h = y + height;
|
||||
var pixel = 0;
|
||||
var result = 0;
|
||||
var dirty = false;
|
||||
|
||||
for (var ty = y; ty < h; ty++)
|
||||
{
|
||||
for (var tx = x; tx < w; tx++)
|
||||
{
|
||||
pixel = this.getPixel32(tx, ty);
|
||||
result = callback.call(callbackContext, pixel);
|
||||
|
||||
if (result !== pixel)
|
||||
{
|
||||
this.pixels[ty * this.width + tx] = result;
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dirty)
|
||||
{
|
||||
this.context.putImageData(this.imageData, 0, 0);
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Replaces all pixels matching one color with another. The color values are given as two sets of RGBA values.
|
||||
* An optional region parameter controls if the replacement happens in just a specific area of the BitmapData or the entire thing.
|
||||
*
|
||||
* @method Phaser.BitmapData#replaceRGB
|
||||
* @param {number} r1 - The red color value to be replaced. Between 0 and 255.
|
||||
|
@ -341,7 +407,7 @@ Phaser.BitmapData.prototype = {
|
|||
var sy = 0;
|
||||
var w = this.width;
|
||||
var h = this.height;
|
||||
var source = this.packPixel(r1, g1, b1, a1);
|
||||
var source = Phaser.Color.packPixel(r1, g1, b1, a1);
|
||||
|
||||
if (region !== undefined && region instanceof Phaser.Rectangle)
|
||||
{
|
||||
|
@ -367,6 +433,129 @@ Phaser.BitmapData.prototype = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Does hsl stuff.
|
||||
*
|
||||
* @method Phaser.BitmapData#setHSL
|
||||
* @param {number} [h=null] -
|
||||
* @param {number} [s=null] -
|
||||
* @param {number} [l=null] -
|
||||
* @param {Phaser.Rectangle} [region] - The area to perform the search over. If not given it will replace over the whole BitmapData.
|
||||
*/
|
||||
setHSL: function (h, s, l, region) {
|
||||
|
||||
if (typeof h === 'undefined' || h === null) { h = false; }
|
||||
if (typeof s === 'undefined' || s === null) { s = false; }
|
||||
if (typeof l === 'undefined' || l === null) { l = false; }
|
||||
|
||||
if (!h && !s && !l)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// need to find a smarter way to do this
|
||||
var sx = 0;
|
||||
var sy = 0;
|
||||
var width = this.width;
|
||||
var height = this.height;
|
||||
var pixel = Phaser.Color.createColor();
|
||||
|
||||
if (region !== undefined && region instanceof Phaser.Rectangle)
|
||||
{
|
||||
sx = region.x;
|
||||
sy = region.y;
|
||||
width = region.width;
|
||||
height = region.height;
|
||||
}
|
||||
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
Phaser.Color.unpackPixel(this.getPixel32(sx + x, sy + y), pixel, true);
|
||||
|
||||
if (h)
|
||||
{
|
||||
pixel.h = h;
|
||||
}
|
||||
|
||||
if (s)
|
||||
{
|
||||
pixel.s = s;
|
||||
}
|
||||
|
||||
if (l)
|
||||
{
|
||||
pixel.l = l;
|
||||
}
|
||||
|
||||
Phaser.Color.HSLtoRGB(pixel.h, pixel.s, pixel.l, pixel);
|
||||
this.setPixel32(sx + x, sy + y, pixel.r, pixel.g, pixel.b, pixel.a, false);
|
||||
}
|
||||
}
|
||||
|
||||
this.context.putImageData(this.imageData, 0, 0);
|
||||
this.dirty = true;
|
||||
|
||||
},
|
||||
|
||||
shiftHSL: function(h, s, l, region) {
|
||||
|
||||
if (typeof h === 'undefined' || h === null) { h = false; }
|
||||
if (typeof s === 'undefined' || s === null) { s = false; }
|
||||
if (typeof l === 'undefined' || l === null) { l = false; }
|
||||
|
||||
if (!h && !s && !l)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// need to find a smarter way to do this
|
||||
var sx = 0;
|
||||
var sy = 0;
|
||||
var width = this.width;
|
||||
var height = this.height;
|
||||
var pixel = Phaser.Color.createColor();
|
||||
|
||||
if (region !== undefined && region instanceof Phaser.Rectangle)
|
||||
{
|
||||
sx = region.x;
|
||||
sy = region.y;
|
||||
width = region.width;
|
||||
height = region.height;
|
||||
}
|
||||
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
Phaser.Color.unpackPixel(this.getPixel32(sx + x, sy + y), pixel, true);
|
||||
|
||||
if (h)
|
||||
{
|
||||
pixel.h = this.game.math.wrap(pixel.h + h, 0, 1);
|
||||
}
|
||||
|
||||
if (s)
|
||||
{
|
||||
pixel.s = this.game.math.limitValue(pixel.s + s, 0, 1);
|
||||
}
|
||||
|
||||
if (l)
|
||||
{
|
||||
pixel.l = this.game.math.limitValue(pixel.l + l, 0, 1);
|
||||
}
|
||||
|
||||
Phaser.Color.HSLtoRGB(pixel.h, pixel.s, pixel.l, pixel);
|
||||
this.setPixel32(sx + x, sy + y, pixel.r, pixel.g, pixel.b, pixel.a, false);
|
||||
}
|
||||
}
|
||||
|
||||
this.context.putImageData(this.imageData, 0, 0);
|
||||
this.dirty = true;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the color of the given pixel to the specified red, green, blue and alpha values.
|
||||
*
|
||||
|
@ -385,7 +574,7 @@ Phaser.BitmapData.prototype = {
|
|||
|
||||
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
|
||||
{
|
||||
if (this.littleEndian)
|
||||
if (Phaser.Device.LITTLE_ENDIAN)
|
||||
{
|
||||
this.pixels[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
|
||||
}
|
||||
|
@ -431,15 +620,15 @@ Phaser.BitmapData.prototype = {
|
|||
*/
|
||||
getPixel: function (x, y, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = Phaser.Color.createColor();
|
||||
}
|
||||
|
||||
var index = ~~(x + (y * this.width));
|
||||
|
||||
index *= 4;
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = { r:0, g:0, b:0, a:0 };
|
||||
}
|
||||
|
||||
out.r = this.data[index];
|
||||
out.g = this.data[++index];
|
||||
out.b = this.data[++index];
|
||||
|
@ -477,7 +666,7 @@ Phaser.BitmapData.prototype = {
|
|||
*/
|
||||
getPixelRGB: function (x, y) {
|
||||
|
||||
return this.unpackPixel(this.getPixel32(x, y));
|
||||
return Phaser.Color.unpackPixel(this.getPixel32(x, y));
|
||||
|
||||
},
|
||||
|
||||
|
@ -494,145 +683,6 @@ Phaser.BitmapData.prototype = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Packs the r, g, b, a components into a single integer, for use with Int32Array.
|
||||
* If device is little endian then ABGR order is used. Otherwise RGBA order is used.
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.BitmapData#packPixel
|
||||
* @param {number} r - The red byte, 0-255
|
||||
* @param {number} g - The green byte, 0-255
|
||||
* @param {number} b - The blue byte, 0-255
|
||||
* @param {number} a - The alpha byte, 0-255
|
||||
* @return {number} The packed color
|
||||
*/
|
||||
packPixel: function (r, g, b, a) {
|
||||
|
||||
if (this.littleEndian)
|
||||
{
|
||||
return (a << 24) | (b << 16) | (g << 8) | r;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (r << 24) | (g << 16) | (b << 8) | a;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Unpacks the r, g, b, a components into the specified color object, or a new
|
||||
* object, for use with Int32Array. If little endian, then ABGR order is used when
|
||||
* unpacking, otherwise, RGBA order is used. The resulting color object has the
|
||||
* `r, g, b, a` properties which are unrelated to endianness.
|
||||
*
|
||||
* Note that the integer is assumed to be packed in the correct endianness. On little-endian
|
||||
* the format is 0xAABBGGRR and on big-endian the format is 0xRRGGBBAA. If you want a
|
||||
* endian-independent method, use fromRGBA(rgba) and toRGBA(r, g, b, a).
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.BitmapData#unpackPixel
|
||||
* @param {number} rgba - The integer, packed in endian order by packPixel.
|
||||
* @param {object} out - The color object with `r, g, b, a, rgba and color` properties, or null.
|
||||
* @return {object} A color object.
|
||||
*/
|
||||
unpackPixel: function (rgba, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = { r: 0, g: 0, b: 0, a: 0, color: 0, rgba: '' };
|
||||
}
|
||||
|
||||
if (this.littleEndian)
|
||||
{
|
||||
out.a = ((rgba & 0xff000000) >>> 24);
|
||||
out.b = ((rgba & 0x00ff0000) >>> 16);
|
||||
out.g = ((rgba & 0x0000ff00) >>> 8);
|
||||
out.r = ((rgba & 0x000000ff));
|
||||
}
|
||||
else
|
||||
{
|
||||
out.r = ((rgba & 0xff000000) >>> 24);
|
||||
out.g = ((rgba & 0x00ff0000) >>> 16);
|
||||
out.b = ((rgba & 0x0000ff00) >>> 8);
|
||||
out.a = ((rgba & 0x000000ff));
|
||||
}
|
||||
|
||||
out.color = rgba;
|
||||
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* A utility to convert an integer in 0xRRGGBBAA format to a color object.
|
||||
* This does not rely on endianness.
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.BitmapData#fromRGBA
|
||||
* @param {number} rgba - An RGBA hex
|
||||
* @param {object} [out] - The object to use, optional.
|
||||
* @return {object} A color object.
|
||||
*/
|
||||
fromRGBA: function (rgba, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = { r: 0, g: 0, b: 0, a: 0, rgba: '' };
|
||||
}
|
||||
|
||||
out.r = ((rgba & 0xff000000) >>> 24);
|
||||
out.g = ((rgba & 0x00ff0000) >>> 16);
|
||||
out.b = ((rgba & 0x0000ff00) >>> 8);
|
||||
out.a = ((rgba & 0x000000ff));
|
||||
|
||||
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* A utility to convert RGBA components to a 32 bit integer in RRGGBBAA format.
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.BitmapData#toRGBA
|
||||
* @param {number} r - The r color component (0 - 255)
|
||||
* @param {number} g - The g color component (0 - 255)
|
||||
* @param {number} b - The b color component (0 - 255)
|
||||
* @param {number} a - The a color component (0 - 255)
|
||||
* @return {number} A RGBA-packed 32 bit integer
|
||||
*/
|
||||
toRGBA: function (r, g, b, a) {
|
||||
|
||||
return (r << 24) | (g << 16) | (b << 8) | a;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* A utility function to create a lightweight 'color' object with the default components.
|
||||
* Any components that are not specified will default to zero.
|
||||
*
|
||||
* This is useful when you want to use a shared color object for the getPixel and getPixelAt methods.
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.BitmapData#createColor
|
||||
* @param {number} [r=0] - The r color component (0 - 255)
|
||||
* @param {number} [g=0] - The g color component (0 - 255)
|
||||
* @param {number} [b=0] - The b color component (0 - 255)
|
||||
* @param {number} [a=0] - The a color component (0 - 255)
|
||||
* @return {object} The resulting color object, with r, g, b, a properties
|
||||
*/
|
||||
createColor: function (r, g, b, a) {
|
||||
|
||||
var out = { r: r||0, g: g||0, b: b||0, a: a||0 };
|
||||
|
||||
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Copies the pixels from the source image to this BitmapData based on the given area and destination.
|
||||
*
|
||||
|
@ -751,215 +801,6 @@ Phaser.BitmapData.prototype = {
|
|||
|
||||
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts an RGB color to HSL.
|
||||
*
|
||||
* @author http://mjijackson.com
|
||||
* @method Phaser.BitmapData#rgbToHsl
|
||||
* @param {number} r - The r color component (0 - 255)
|
||||
* @param {number} g - The g color component (0 - 255)
|
||||
* @param {number} b - The b color component (0 - 255)
|
||||
* @return {object} An object containing 3 properties: h, s and l.
|
||||
*/
|
||||
rgbToHsl: function (r, g, b) {
|
||||
|
||||
if (Array.isArray(r)) {
|
||||
b = r[2];
|
||||
g = r[1];
|
||||
r = r[0];
|
||||
}
|
||||
|
||||
r /= 255;
|
||||
g /= 255;
|
||||
b /= 255;
|
||||
|
||||
var max = Math.max(r, g, b);
|
||||
var min = Math.min(r, g, b);
|
||||
var h, s, l = (max + min) / 2;
|
||||
|
||||
if (max === min)
|
||||
{
|
||||
h = s = 0; // achromatic
|
||||
}
|
||||
else
|
||||
{
|
||||
var d = max - min;
|
||||
|
||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
|
||||
switch (max) {
|
||||
case r:
|
||||
h = (g - b) / d + (g < b ? 6 : 0);
|
||||
break;
|
||||
case g:
|
||||
h = (b - r) / d + 2;
|
||||
break;
|
||||
case b:
|
||||
h = (r - g) / d + 4;
|
||||
break;
|
||||
}
|
||||
|
||||
h /= 6;
|
||||
|
||||
}
|
||||
|
||||
return { h: h, s: s, l: l };
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts an RGB color to HSL.
|
||||
*
|
||||
* @author http://mjijackson.com
|
||||
* @method Phaser.BitmapData#hslToRgb
|
||||
* @param {number} h - The h color component (0 - 255)
|
||||
* @param {number} s - The s color component (0 - 255)
|
||||
* @param {number} l - The l color component (0 - 255)
|
||||
* @return {object} A color object.
|
||||
*/
|
||||
hslToRgb: function (h, s, l) {
|
||||
|
||||
var r, g, b;
|
||||
|
||||
if (s === 0)
|
||||
{
|
||||
r = g = b = l; // achromatic
|
||||
}
|
||||
else
|
||||
{
|
||||
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
var p = 2 * l - q;
|
||||
r = this.hue2rgb(p, q, h + 1 / 3);
|
||||
g = this.hue2rgb(p, q, h);
|
||||
b = this.hue2rgb(p, q, h - 1 / 3);
|
||||
}
|
||||
|
||||
return this.createColor(r * 255 | 0, g * 255 | 0, b * 255 | 0);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts a hue to an RGB color.
|
||||
*
|
||||
* @author http://mjijackson.com
|
||||
* @method Phaser.BitmapData#hue2rgb
|
||||
* @private
|
||||
* @param {number} p
|
||||
* @param {number} q
|
||||
* @param {number} t
|
||||
* @return {number} The color component value.
|
||||
*/
|
||||
hue2rgb: function (p, q, t) {
|
||||
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1 / 2) return q;
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
|
||||
return p;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts an RGB color to HSV.
|
||||
*
|
||||
* @author http://mjijackson.com
|
||||
* @method Phaser.BitmapData#rgbToHsv
|
||||
* @param {number} r - The r color component (0 - 255)
|
||||
* @param {number} g - The g color component (0 - 255)
|
||||
* @param {number} b - The b color component (0 - 255)
|
||||
* @return {object} An object containing 3 properties: h, s and v.
|
||||
*/
|
||||
rgbToHsv: function (r, g, b) {
|
||||
|
||||
if (Array.isArray(r))
|
||||
{
|
||||
b = r[2];
|
||||
g = r[1];
|
||||
r = r[0];
|
||||
}
|
||||
|
||||
r = r / 255, g = g / 255, b = b / 255;
|
||||
|
||||
var max = Math.max(r, g, b);
|
||||
var min = Math.min(r, g, b);
|
||||
var h, s, v = max;
|
||||
var d = max - min;
|
||||
|
||||
s = max == 0 ? 0 : d / max;
|
||||
|
||||
if (max === min)
|
||||
{
|
||||
h = 0; // achromatic
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (max) {
|
||||
case r:
|
||||
h = (g - b) / d + (g < b ? 6 : 0);
|
||||
break;
|
||||
case g:
|
||||
h = (b - r) / d + 2;
|
||||
break;
|
||||
case b:
|
||||
h = (r - g) / d + 4;
|
||||
break;
|
||||
}
|
||||
|
||||
h /= 6;
|
||||
}
|
||||
|
||||
return { h: h, s: s, v: v };
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts a HSV color to RGB.
|
||||
*
|
||||
* @author http://mjijackson.com
|
||||
* @method Phaser.BitmapData#hsvToRgb
|
||||
* @param {number} h - The h color component (0 - 255)
|
||||
* @param {number} s - The s color component (0 - 255)
|
||||
* @param {number} v - The v color component (0 - 255)
|
||||
* @return {object} A color object.
|
||||
*/
|
||||
hsvToRgb: function (h, s, v) {
|
||||
|
||||
var r, g, b;
|
||||
|
||||
var i = Math.floor(h * 6);
|
||||
var f = h * 6 - i;
|
||||
var p = v * (1 - s);
|
||||
var q = v * (1 - f * s);
|
||||
var t = v * (1 - (1 - f) * s);
|
||||
|
||||
switch (i % 6)
|
||||
{
|
||||
case 0:
|
||||
r = v, g = t, b = p;
|
||||
break;
|
||||
case 1:
|
||||
r = q, g = v, b = p;
|
||||
break;
|
||||
case 2:
|
||||
r = p, g = v, b = t;
|
||||
break;
|
||||
case 3:
|
||||
r = p, g = q, b = v;
|
||||
break;
|
||||
case 4:
|
||||
r = t, g = p, b = v;
|
||||
break;
|
||||
case 5:
|
||||
r = v, g = p, b = q;
|
||||
break;
|
||||
}
|
||||
|
||||
return this.createColor(r * 255 | 0, g * 255 | 0, b * 255 | 0);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -606,6 +606,21 @@ Phaser.Math = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Ensures the given value is between min and max inclusive.
|
||||
*
|
||||
* @method Phaser.Math#limitValue
|
||||
* @param {number} value - The value to limit.
|
||||
* @param {number} min - The minimum the value can be.
|
||||
* @param {number} max - The maximum the value can be.
|
||||
* @return {number} The limited value.
|
||||
*/
|
||||
limitValue: function(value, min, max) {
|
||||
|
||||
return value < min ? min : value > max ? max : value;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Randomly returns either a 1 or -1.
|
||||
*
|
||||
|
@ -613,7 +628,9 @@ Phaser.Math = {
|
|||
* @return {number} 1 or -1
|
||||
*/
|
||||
randomSign: function () {
|
||||
|
||||
return (Math.random() > 0.5) ? 1 : -1;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -381,6 +381,8 @@ Phaser.Device = function (game) {
|
|||
|
||||
};
|
||||
|
||||
Phaser.Device.LITTLE_ENDIAN = false;
|
||||
|
||||
Phaser.Device.prototype = {
|
||||
|
||||
/**
|
||||
|
@ -692,6 +694,7 @@ Phaser.Device.prototype = {
|
|||
if (typeof ArrayBuffer !== 'undefined' && typeof Uint8Array !== 'undefined' && typeof Uint32Array !== 'undefined')
|
||||
{
|
||||
this.littleEndian = this._checkIsLittleEndian();
|
||||
Phaser.Device.LITTLE_ENDIAN = this.littleEndian;
|
||||
}
|
||||
|
||||
this.support32bit = (typeof ArrayBuffer !== "undefined" && typeof Uint8ClampedArray !== "undefined" && typeof Int32Array !== "undefined" && this.littleEndian !== null && this._checkIsUint8ClampedImageData());
|
||||
|
|
|
@ -5,25 +5,427 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* A collection of methods useful for manipulating and comparing colors.
|
||||
* The Phaser.Color class is a set of static methods that assist in color manipulation and conversion.
|
||||
*
|
||||
* @class Phaser.Color
|
||||
*/
|
||||
Phaser.Color = {
|
||||
|
||||
/**
|
||||
* Packs the r, g, b, a components into a single integer, for use with Int32Array.
|
||||
* If device is little endian then ABGR order is used. Otherwise RGBA order is used.
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.Color.packPixel
|
||||
* @static
|
||||
* @param {number} r - The red color component, in the range 0 - 255.
|
||||
* @param {number} g - The green color component, in the range 0 - 255.
|
||||
* @param {number} b - The blue color component, in the range 0 - 255.
|
||||
* @param {number} a - The alpha color component, in the range 0 - 255.
|
||||
* @return {number} The packed color
|
||||
*/
|
||||
packPixel: function (r, g, b, a) {
|
||||
|
||||
if (Phaser.Device.LITTLE_ENDIAN)
|
||||
{
|
||||
return (a << 24) | (b << 16) | (g << 8) | r;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (r << 24) | (g << 16) | (b << 8) | a;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Unpacks the r, g, b, a components into the specified color object, or a new
|
||||
* object, for use with Int32Array. If little endian, then ABGR order is used when
|
||||
* unpacking, otherwise, RGBA order is used. The resulting color object has the
|
||||
* `r, g, b, a` properties which are unrelated to endianness.
|
||||
*
|
||||
* Note that the integer is assumed to be packed in the correct endianness. On little-endian
|
||||
* the format is 0xAABBGGRR and on big-endian the format is 0xRRGGBBAA. If you want a
|
||||
* endian-independent method, use fromRGBA(rgba) and toRGBA(r, g, b, a).
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.Color.unpackPixel
|
||||
* @static
|
||||
* @param {number} rgba - The integer, packed in endian order by packPixel.
|
||||
* @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.
|
||||
* @param {boolean} [hsl=false] - Also convert the rgb values into hsl?
|
||||
* @param {boolean} [hsv=false] - Also convert the rgb values into hsv?
|
||||
* @return {object} An object with the red, green and blue values set in the r, g and b properties.
|
||||
*/
|
||||
unpackPixel: function (rgba, out, hsl, hsv) {
|
||||
|
||||
if (typeof out === 'undefined' || out === null) { out = Phaser.Color.createColor(); }
|
||||
if (typeof hsl === 'undefined' || hsl === null) { hsl = false; }
|
||||
if (typeof hsv === 'undefined' || hsv === null) { hsv = false; }
|
||||
|
||||
if (Phaser.Device.LITTLE_ENDIAN)
|
||||
{
|
||||
out.a = ((rgba & 0xff000000) >>> 24);
|
||||
out.b = ((rgba & 0x00ff0000) >>> 16);
|
||||
out.g = ((rgba & 0x0000ff00) >>> 8);
|
||||
out.r = ((rgba & 0x000000ff));
|
||||
}
|
||||
else
|
||||
{
|
||||
out.r = ((rgba & 0xff000000) >>> 24);
|
||||
out.g = ((rgba & 0x00ff0000) >>> 16);
|
||||
out.b = ((rgba & 0x0000ff00) >>> 8);
|
||||
out.a = ((rgba & 0x000000ff));
|
||||
}
|
||||
|
||||
out.color = rgba;
|
||||
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + (out.a / 255) + ')';
|
||||
|
||||
if (hsl)
|
||||
{
|
||||
Phaser.Color.RGBtoHSL(out.r, out.g, out.b, out);
|
||||
}
|
||||
|
||||
if (hsv)
|
||||
{
|
||||
Phaser.Color.RGBtoHSV(out.r, out.g, out.b, out);
|
||||
}
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* A utility to convert an integer in 0xRRGGBBAA format to a color object.
|
||||
* This does not rely on endianness.
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.Color.fromRGBA
|
||||
* @static
|
||||
* @param {number} rgba - An RGBA hex
|
||||
* @param {object} [out] - The object to use, optional.
|
||||
* @return {object} A color object.
|
||||
*/
|
||||
fromRGBA: function (rgba, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = Phaser.Color.createColor();
|
||||
}
|
||||
|
||||
out.r = ((rgba & 0xff000000) >>> 24);
|
||||
out.g = ((rgba & 0x00ff0000) >>> 16);
|
||||
out.b = ((rgba & 0x0000ff00) >>> 8);
|
||||
out.a = ((rgba & 0x000000ff));
|
||||
|
||||
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* A utility to convert RGBA components to a 32 bit integer in RRGGBBAA format.
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.Color.toRGBA
|
||||
* @static
|
||||
* @param {number} r - The red color component, in the range 0 - 255.
|
||||
* @param {number} g - The green color component, in the range 0 - 255.
|
||||
* @param {number} b - The blue color component, in the range 0 - 255.
|
||||
* @param {number} a - The alpha color component, in the range 0 - 255.
|
||||
* @return {number} A RGBA-packed 32 bit integer
|
||||
*/
|
||||
toRGBA: function (r, g, b, a) {
|
||||
|
||||
return (r << 24) | (g << 16) | (b << 8) | a;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts an RGB color value to HSL (hue, saturation and lightness).
|
||||
* Conversion forumla from http://en.wikipedia.org/wiki/HSL_color_space.
|
||||
* Assumes RGB values are contained in the set [0, 255] and returns h, s and l in the set [0, 1].
|
||||
* Based on code by Michael Jackson (https://github.com/mjijackson)
|
||||
*
|
||||
* @method Phaser.Color.RGBtoHSL
|
||||
* @static
|
||||
* @param {number} r - The red color component, in the range 0 - 255.
|
||||
* @param {number} g - The green color component, in the range 0 - 255.
|
||||
* @param {number} b - The blue color component, in the range 0 - 255.
|
||||
* @param {object} [out] - An object into which 3 properties will be created, h, s and l. If not provided a new object will be created.
|
||||
* @return {object} An object with the hue, saturation and lightness values set in the h, s and l properties.
|
||||
*/
|
||||
RGBtoHSL: function (r, g, b, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = Phaser.Color.createColor(r, g, b, 1);
|
||||
}
|
||||
|
||||
r /= 255;
|
||||
g /= 255;
|
||||
b /= 255;
|
||||
|
||||
var min = Math.min(r, g, b);
|
||||
var max = Math.max(r, g, b);
|
||||
|
||||
// achromatic by default
|
||||
out.h = 0;
|
||||
out.s = 0;
|
||||
out.l = (max + min) / 2;
|
||||
|
||||
if (max !== min)
|
||||
{
|
||||
var d = max - min;
|
||||
|
||||
out.s = out.l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
|
||||
if (max === r)
|
||||
{
|
||||
out.h = (g - b) / d + (g < b ? 6 : 0);
|
||||
}
|
||||
else if (max === g)
|
||||
{
|
||||
out.h = (b - r) / d + 2;
|
||||
}
|
||||
else if (max === b)
|
||||
{
|
||||
out.h = (r - g) / d + 4;
|
||||
}
|
||||
|
||||
out.h /= 6;
|
||||
}
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts an HSL (hue, saturation and lightness) color value to RGB.
|
||||
* Conversion forumla from http://en.wikipedia.org/wiki/HSL_color_space.
|
||||
* Assumes HSL values are contained in the set [0, 1] and returns r, g and b values in the set [0, 255].
|
||||
* Based on code by Michael Jackson (https://github.com/mjijackson)
|
||||
*
|
||||
* @method Phaser.Color.HSLtoRGB
|
||||
* @static
|
||||
* @param {number} h - The hue, in the range 0 - 1.
|
||||
* @param {number} s - The saturation, in the range 0 - 1.
|
||||
* @param {number} l - The lightness, in the range 0 - 1.
|
||||
* @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.
|
||||
* @return {object} An object with the red, green and blue values set in the r, g and b properties.
|
||||
*/
|
||||
HSLtoRGB: function (h, s, l, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = Phaser.Color.createColor(l, l, l);
|
||||
}
|
||||
else
|
||||
{
|
||||
// achromatic by default
|
||||
out.r = l;
|
||||
out.g = l;
|
||||
out.b = l;
|
||||
}
|
||||
|
||||
if (s !== 0)
|
||||
{
|
||||
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
var p = 2 * l - q;
|
||||
out.r = Phaser.Color.hueToColor(p, q, h + 1 / 3);
|
||||
out.g = Phaser.Color.hueToColor(p, q, h);
|
||||
out.b = Phaser.Color.hueToColor(p, q, h - 1 / 3);
|
||||
}
|
||||
|
||||
out.r = (out.r * 255 | 0);
|
||||
out.g = (out.g * 255 | 0);
|
||||
out.b = (out.b * 255 | 0);
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts an RGB color value to HSV (hue, saturation and value).
|
||||
* Conversion forumla from http://en.wikipedia.org/wiki/HSL_color_space.
|
||||
* Assumes RGB values are contained in the set [0, 255] and returns h, s and v in the set [0, 1].
|
||||
* Based on code by Michael Jackson (https://github.com/mjijackson)
|
||||
*
|
||||
* @method Phaser.Color.RGBtoHSV
|
||||
* @static
|
||||
* @param {number} r - The red color component, in the range 0 - 255.
|
||||
* @param {number} g - The green color component, in the range 0 - 255.
|
||||
* @param {number} b - The blue color component, in the range 0 - 255.
|
||||
* @param {object} [out] - An object into which 3 properties will be created, h, s and v. If not provided a new object will be created.
|
||||
* @return {object} An object with the hue, saturation and value set in the h, s and v properties.
|
||||
*/
|
||||
RGBtoHSV: function (r, g, b, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = Phaser.Color.createColor(r, g, b, 255);
|
||||
}
|
||||
|
||||
r /= 255;
|
||||
g /= 255;
|
||||
b /= 255;
|
||||
|
||||
var min = Math.min(r, g, b);
|
||||
var max = Math.max(r, g, b);
|
||||
var d = max - min;
|
||||
|
||||
// achromatic by default
|
||||
out.h = 0;
|
||||
out.s = max == 0 ? 0 : d / max;
|
||||
out.v = max;
|
||||
|
||||
if (max !== min)
|
||||
{
|
||||
if (max === r)
|
||||
{
|
||||
out.h = (g - b) / d + (g < b ? 6 : 0);
|
||||
}
|
||||
else if (max === g)
|
||||
{
|
||||
out.h = (b - r) / d + 2;
|
||||
}
|
||||
else if (max === b)
|
||||
{
|
||||
out.h = (r - g) / d + 4;
|
||||
}
|
||||
|
||||
out.h /= 6;
|
||||
}
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts an HSV (hue, saturation and value) color value to RGB.
|
||||
* Conversion forumla from http://en.wikipedia.org/wiki/HSL_color_space.
|
||||
* Assumes HSV values are contained in the set [0, 1] and returns r, g and b values in the set [0, 255].
|
||||
* Based on code by Michael Jackson (https://github.com/mjijackson)
|
||||
*
|
||||
* @method Phaser.Color.HSVtoRGB
|
||||
* @static
|
||||
* @param {number} h - The hue, in the range 0 - 1.
|
||||
* @param {number} s - The saturation, in the range 0 - 1.
|
||||
* @param {number} v - The value, in the range 0 - 1.
|
||||
* @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.
|
||||
* @return {object} An object with the red, green and blue values set in the r, g and b properties.
|
||||
*/
|
||||
HSVtoRGB: function (h, s, v, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = Phaser.Color.createColor(0, 0, 0, 0, h, s, 0, v);
|
||||
}
|
||||
|
||||
var r, g, b;
|
||||
var i = Math.floor(h * 6);
|
||||
var f = h * 6 - i;
|
||||
var p = v * (1 - s);
|
||||
var q = v * (1 - f * s);
|
||||
var t = v * (1 - (1 - f) * s);
|
||||
|
||||
switch (i % 6)
|
||||
{
|
||||
case 0:
|
||||
r = v, g = t, b = p;
|
||||
break;
|
||||
case 1:
|
||||
r = q, g = v, b = p;
|
||||
break;
|
||||
case 2:
|
||||
r = p, g = v, b = t;
|
||||
break;
|
||||
case 3:
|
||||
r = p, g = q, b = v;
|
||||
break;
|
||||
case 4:
|
||||
r = t, g = p, b = v;
|
||||
break;
|
||||
case 5:
|
||||
r = v, g = p, b = q;
|
||||
break;
|
||||
}
|
||||
|
||||
out.r = r * 255;
|
||||
out.g = g * 255;
|
||||
out.b = b * 255;
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts a hue to an RGB color.
|
||||
* Based on code by Michael Jackson (https://github.com/mjijackson)
|
||||
*
|
||||
* @method Phaser.Color.hueToColor
|
||||
* @static
|
||||
* @param {number} p
|
||||
* @param {number} q
|
||||
* @param {number} t
|
||||
* @return {number} The color component value.
|
||||
*/
|
||||
hueToColor: function (p, q, t) {
|
||||
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1 / 2) return q;
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
|
||||
return p;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* A utility function to create a lightweight 'color' object with the default components.
|
||||
* Any components that are not specified will default to zero.
|
||||
*
|
||||
* This is useful when you want to use a shared color object for the getPixel and getPixelAt methods.
|
||||
*
|
||||
* @author Matt DesLauriers (@mattdesl)
|
||||
* @method Phaser.Color.createColor
|
||||
* @static
|
||||
* @param {number} [r=0] - The red color component, in the range 0 - 255.
|
||||
* @param {number} [g=0] - The green color component, in the range 0 - 255.
|
||||
* @param {number} [b=0] - The blue color component, in the range 0 - 255.
|
||||
* @param {number} [a=0] - The alpha color component, in the range 0 - 1.
|
||||
* @param {number} [h=0] - The hue, in the range 0 - 1.
|
||||
* @param {number} [s=0] - The saturation, in the range 0 - 1.
|
||||
* @param {number} [l=0] - The lightness, in the range 0 - 1.
|
||||
* @param {number} [v=0] - The value, in the range 0 - 1.
|
||||
* @return {object} The resulting object with r, g, b, a properties and h, s, l and v.
|
||||
*/
|
||||
createColor: function (r, g, b, a, h, s, l, v) {
|
||||
|
||||
var out = { r: r || 0, g: g || 0, b: b || 0, a: a || 0, h: h || 0, s: s || 0, l: l || 0, v: v || 0, color: 0 };
|
||||
|
||||
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Given an alpha and 3 color values this will return an integer representation of it.
|
||||
*
|
||||
* @method Phaser.Color.getColor32
|
||||
* @static
|
||||
* @param {number} alpha - The Alpha value (between 0 and 255).
|
||||
* @param {number} red - The Red channel value (between 0 and 255).
|
||||
* @param {number} green - The Green channel value (between 0 and 255).
|
||||
* @param {number} blue - The Blue channel value (between 0 and 255).
|
||||
* @param {number} a - The alpha color component, in the range 0 - 255.
|
||||
* @param {number} r - The red color component, in the range 0 - 255.
|
||||
* @param {number} g - The green color component, in the range 0 - 255.
|
||||
* @param {number} b - The blue color component, in the range 0 - 255.
|
||||
* @returns {number} A native color value integer (format: 0xAARRGGBB).
|
||||
*/
|
||||
getColor32: function (alpha, red, green, blue) {
|
||||
return alpha << 24 | red << 16 | green << 8 | blue;
|
||||
getColor32: function (a, r, g, b) {
|
||||
|
||||
return a << 24 | r << 16 | g << 8 | b;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -31,114 +433,113 @@ Phaser.Color = {
|
|||
*
|
||||
* @method Phaser.Color.getColor
|
||||
* @static
|
||||
* @param {number} red - The Red channel value (between 0 and 255).
|
||||
* @param {number} green - The Green channel value (between 0 and 255).
|
||||
* @param {number} blue - The Blue channel value (between 0 and 255).
|
||||
* @param {number} r - The red color component, in the range 0 - 255.
|
||||
* @param {number} g - The green color component, in the range 0 - 255.
|
||||
* @param {number} b - The blue color component, in the range 0 - 255.
|
||||
* @returns {number} A native color value integer (format: 0xRRGGBB).
|
||||
*/
|
||||
getColor: function (red, green, blue) {
|
||||
return red << 16 | green << 8 | blue;
|
||||
getColor: function (r, g, b) {
|
||||
|
||||
return r << 16 | g << 8 | b;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts the given hex string into an integer color value.
|
||||
* Converts the given color values into a string.
|
||||
* If prefix was '#' it will be in the format `#RRGGBB` otherwise `0xAARRGGBB`.
|
||||
*
|
||||
* @method Phaser.Color.RGBtoString
|
||||
* @static
|
||||
* @param {number} r - The red color component, in the range 0 - 255.
|
||||
* @param {number} g - The green color component, in the range 0 - 255.
|
||||
* @param {number} b - The blue color component, in the range 0 - 255.
|
||||
* @param {number} [a=255] - The alpha color component, in the range 0 - 255.
|
||||
* @param {string} [prefix='#'] - The prefix used in the return string. If '#' it will return `#RRGGBB`, else `0xAARRGGBB`.
|
||||
* @return {string} A string containing the color values. If prefix was '#' it will be in the format `#RRGGBB` otherwise `0xAARRGGBB`.
|
||||
*/
|
||||
RGBtoString: function (r, g, b, a, prefix) {
|
||||
|
||||
if (typeof a === 'undefined') { a = 255 };
|
||||
if (typeof prefix === 'undefined') { prefix = '#' };
|
||||
|
||||
if (prefix === '#')
|
||||
{
|
||||
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return '0x' + Phaser.Color.componentToHex(a) + Phaser.Color.componentToHex(r) + Phaser.Color.componentToHex(g) + Phaser.Color.componentToHex(b);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts a hex string into an integer color value.
|
||||
*
|
||||
* @method Phaser.Color.hexToRGB
|
||||
* @static
|
||||
* @param {string} h - The string hex color to convert.
|
||||
* @returns {number} The rgb color value.
|
||||
* @param {string} hex - The hex string to convert. Can be in the short-hand format `#03f` or `#0033ff`.
|
||||
* @return {number} The rgb color value in the format 0xAARRGGBB.
|
||||
*/
|
||||
hexToRGB: function (h) {
|
||||
hexToRGB: function (hex) {
|
||||
|
||||
var hex16 = (h.charAt(0) == "#") ? h.substring(1, 7) : h;
|
||||
var rgb = Phaser.Color.hexToColor(hex);
|
||||
|
||||
if (hex16.length == 3)
|
||||
if (rgb)
|
||||
{
|
||||
hex16 = hex16.charAt(0) + hex16.charAt(0) + hex16.charAt(1) + hex16.charAt(1) + hex16.charAt(2) + hex16.charAt(2);
|
||||
return Phaser.Color.getColor32(rgb.a, rgb.r, rgb.g, rgb.b);
|
||||
}
|
||||
|
||||
var red = parseInt(hex16.substring(0, 2), 16);
|
||||
var green = parseInt(hex16.substring(2, 4), 16);
|
||||
var blue = parseInt(hex16.substring(4, 6), 16);
|
||||
},
|
||||
|
||||
return red << 16 | green << 8 | blue;
|
||||
/**
|
||||
* Converts a hex string into a Phaser Color object.
|
||||
*
|
||||
* @method Phaser.Color.hexToColor
|
||||
* @static
|
||||
* @param {string} hex - The hex string to convert. Can be in the short-hand format `#03f` or `#0033ff`.
|
||||
* @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.
|
||||
* @return {object} An object with the red, green and blue values set in the r, g and b properties.
|
||||
*/
|
||||
hexToColor: function (hex, out) {
|
||||
|
||||
if (!out)
|
||||
{
|
||||
out = Phaser.Color.createColor();
|
||||
}
|
||||
|
||||
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
|
||||
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
|
||||
|
||||
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
|
||||
return r + r + g + g + b + b;
|
||||
});
|
||||
|
||||
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
||||
|
||||
if (result)
|
||||
{
|
||||
out.r = parseInt(result[1], 16);
|
||||
out.g = parseInt(result[2], 16);
|
||||
out.b = parseInt(result[3], 16);
|
||||
}
|
||||
|
||||
return out;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a string containing handy information about the given color including string hex value,
|
||||
* RGB format information and HSL information. Each section starts on a newline, 3 lines in total.
|
||||
* Return a string containing a hex representation of the given color component.
|
||||
*
|
||||
* @method Phaser.Color.getColorInfo
|
||||
* @method Phaser.Color.componentToHex
|
||||
* @static
|
||||
* @param {number} color - A color value in the format 0xAARRGGBB.
|
||||
* @returns {string} String containing the 3 lines of information.
|
||||
* @param {number} color - The color channel to get the hex value for, must be a value between 0 and 255.
|
||||
* @returns {string} A string of length 2 characters, i.e. 255 = ff, 100 = 64.
|
||||
*/
|
||||
getColorInfo: function (color) {
|
||||
componentToHex: function (color) {
|
||||
|
||||
var argb = Phaser.Color.getRGB(color);
|
||||
var hsl = Phaser.Color.RGBtoHSV(color);
|
||||
|
||||
// Hex format
|
||||
var result = Phaser.Color.RGBtoHexstring(color) + "\n";
|
||||
|
||||
// RGB format
|
||||
result = result.concat("Alpha: " + argb.alpha + " Red: " + argb.red + " Green: " + argb.green + " Blue: " + argb.blue) + "\n";
|
||||
|
||||
// HSL info
|
||||
result = result.concat("Hue: " + hsl.hue + " Saturation: " + hsl.saturation + " Lightnes: " + hsl.lightness);
|
||||
|
||||
return result;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Return a string representation of the color in the format 0xAARRGGBB.
|
||||
*
|
||||
* @method Phaser.Color.RGBtoHexstring
|
||||
* @static
|
||||
* @param {number} color - The color to get the string representation for
|
||||
* @returns {string} A string of length 10 characters in the format 0xAARRGGBB
|
||||
*/
|
||||
RGBtoHexstring: function (color) {
|
||||
|
||||
var argb = Phaser.Color.getRGB(color);
|
||||
|
||||
return "0x" + Phaser.Color.colorToHexstring(argb.alpha) + Phaser.Color.colorToHexstring(argb.red) + Phaser.Color.colorToHexstring(argb.green) + Phaser.Color.colorToHexstring(argb.blue);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Return a string representation of the color in the format #RRGGBB.
|
||||
*
|
||||
* @method Phaser.Color.RGBtoWebstring
|
||||
* @static
|
||||
* @param {number} color - The color to get the string representation for.
|
||||
* @returns {string} A string of length 10 characters in the format 0xAARRGGBB.
|
||||
*/
|
||||
RGBtoWebstring: function (color) {
|
||||
|
||||
var argb = Phaser.Color.getRGB(color);
|
||||
|
||||
return "#" + Phaser.Color.colorToHexstring(argb.red) + Phaser.Color.colorToHexstring(argb.green) + Phaser.Color.colorToHexstring(argb.blue);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Return a string containing a hex representation of the given color.
|
||||
*
|
||||
* @method Phaser.Color.colorToHexstring
|
||||
* @static
|
||||
* @param {number} color - The color channel to get the hex value for, must be a value between 0 and 255).
|
||||
* @returns {string} A string of length 2 characters, i.e. 255 = FF, 0 = 00.
|
||||
*/
|
||||
colorToHexstring: function (color) {
|
||||
|
||||
var digits = "0123456789ABCDEF";
|
||||
var lsd = color % 16;
|
||||
var msd = (color - lsd) / 16;
|
||||
var hexified = digits.charAt(msd) + digits.charAt(lsd);
|
||||
return hexified;
|
||||
var hex = color.toString(16);
|
||||
return hex.length == 1 ? "0" + hex : hex;
|
||||
|
||||
},
|
||||
|
||||
|
@ -343,6 +744,85 @@ Phaser.Color = {
|
|||
*/
|
||||
getBlue: function (color) {
|
||||
return color & 0xFF;
|
||||
},
|
||||
|
||||
// The following are all DEPRECATED
|
||||
|
||||
/**
|
||||
* DEPRECATED: This method will be removed in Phaser 2.1.
|
||||
* Returns a string containing handy information about the given color including string hex value,
|
||||
* RGB format information. Each section starts on a newline, 3 lines in total.
|
||||
*
|
||||
* @method Phaser.Color.getColorInfo
|
||||
* @static
|
||||
* @param {number} color - A color value in the format 0xAARRGGBB.
|
||||
* @returns {string} String containing the 3 lines of information.
|
||||
*/
|
||||
getColorInfo: function (color) {
|
||||
|
||||
var argb = Phaser.Color.getRGB(color);
|
||||
|
||||
// Hex format
|
||||
var result = Phaser.Color.RGBtoHexstring(color) + "\n";
|
||||
|
||||
// RGB format
|
||||
result = result.concat("Alpha: " + argb.alpha + " Red: " + argb.red + " Green: " + argb.green + " Blue: " + argb.blue) + "\n";
|
||||
|
||||
return result;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* DEPRECATED: This method will be removed in Phaser 2.1. Please use Phaser.Color.RGBtoString instead.
|
||||
* Return a string representation of the color in the format 0xAARRGGBB.
|
||||
*
|
||||
* @method Phaser.Color.RGBtoHexstring
|
||||
* @static
|
||||
* @param {number} color - The color to get the string representation for
|
||||
* @returns {string} A string of length 10 characters in the format 0xAARRGGBB
|
||||
*/
|
||||
RGBtoHexstring: function (color) {
|
||||
|
||||
var argb = Phaser.Color.getRGB(color);
|
||||
|
||||
return "0x" + Phaser.Color.colorToHexstring(argb.alpha) + Phaser.Color.colorToHexstring(argb.red) + Phaser.Color.colorToHexstring(argb.green) + Phaser.Color.colorToHexstring(argb.blue);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* DEPRECATED: This method will be removed in Phaser 2.1. Please use Phaser.Color.RGBtoString instead.
|
||||
* Return a string representation of the color in the format #RRGGBB.
|
||||
*
|
||||
* @method Phaser.Color.RGBtoWebstring
|
||||
* @static
|
||||
* @param {number} color - The color to get the string representation for.
|
||||
* @returns {string} A string of length 10 characters in the format 0xAARRGGBB.
|
||||
*/
|
||||
RGBtoWebstring: function (color) {
|
||||
|
||||
var argb = Phaser.Color.getRGB(color);
|
||||
|
||||
return "#" + Phaser.Color.colorToHexstring(argb.red) + Phaser.Color.colorToHexstring(argb.green) + Phaser.Color.colorToHexstring(argb.blue);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* DEPRECATED: This method will be removed in Phaser 2.1. Please use Phaser.Color.componentToHex instead.
|
||||
* Return a string containing a hex representation of the given color.
|
||||
*
|
||||
* @method Phaser.Color.colorToHexstring
|
||||
* @static
|
||||
* @param {number} color - The color channel to get the hex value for, must be a value between 0 and 255).
|
||||
* @returns {string} A string of length 2 characters, i.e. 255 = FF, 0 = 00.
|
||||
*/
|
||||
colorToHexstring: function (color) {
|
||||
|
||||
var digits = "0123456789ABCDEF";
|
||||
var lsd = color % 16;
|
||||
var msd = (color - lsd) / 16;
|
||||
var hexified = digits.charAt(msd) + digits.charAt(lsd);
|
||||
return hexified;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue