Color.hexToRGBArray converts a hex color value to an [R, G, B] array.

Color.RGBArrayToHex converts an RGB color array, in the format: [R, G, B], to a hex color value.
This commit is contained in:
photonstorm 2016-09-28 13:57:29 +01:00
parent eda4961c06
commit b0f27b3f3c
3 changed files with 36 additions and 0 deletions

View file

@ -342,6 +342,8 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
* Device.canUseMultiply is a new boolean property that stores whether or not the Canvas BlendModes are supported, consequently the ability to tint using the multiply method.
* Math.getNextPowerOfTwo will get the next power of two for the given value.
* Math.isPowerOfTwo will return a boolean if the given width and height are a power of two.
* Color.hexToRGBArray converts a hex color value to an [R, G, B] array.
* Color.RGBArrayToHex converts an RGB color array, in the format: [R, G, B], to a hex color value.
### Bug Fixes

View file

@ -158,6 +158,38 @@ Phaser.Color = {
},
/**
* Converts a hex color value to an [R, G, B] array.
*
* @static
* @method Phaser.Color.hexToRGBArray
* @param {number} color - The color to convert to an RGB array. In the format 0xRRGGBB.
* @return {array} An array with element 0 containing the Red value, 1 containing Green, and 2 containing Blue.
*/
hexToRGBArray: function (color) {
return [
(color >> 16 & 0xFF) / 255,
(color >> 8 & 0xFF) / 255,
(color & 0xFF) / 255
];
},
/**
* Converts an RGB color array, in the format: [R, G, B], to a hex color value.
*
* @static
* @method Phaser.Color.RGBArrayToHex
* @param {array} rgb - An array with element 0 containing the Red value, 1 containing Green, and 2 containing Blue.
* @return {number} The color value, in the format 0xRRGGBB.
*/
RGBArrayToHex: function (rgb) {
return ((rgb[0] * 255 << 16) + (rgb[1] * 255 << 8) + rgb[2] * 255);
},
/**
* Converts an RGB color value to HSL (hue, saturation and lightness).
* Conversion forumla from http://en.wikipedia.org/wiki/HSL_color_space.

View file

@ -749,6 +749,7 @@ declare module Phaser {
static getRed(color: number): number;
static getRGB(color: number): RGBColor;
static getWebRGB(color: number | RGBColor): string;
static hexToRGBArray(color: number): number[];
static hexToRGB(h: string): number;
static hexToColor(hex: string, out?: ColorComponents): ColorComponents;
static HSLtoRGB(h: number, s: number, l: number, out?: ColorComponents): ColorComponents;
@ -760,6 +761,7 @@ declare module Phaser {
static interpolateColorWithRGB(color: number, r: number, g: number, b: number, steps: number, currentStep: number): number;
static interpolateRGB(r1: number, g1: number, b1: number, r2: number, g2: number, b2: number, steps: number, currentStep: number): number;
static packPixel(r: number, g: number, b: number, a: number): number;
static RGBArrayToHex(rgb: number[]): number;
static RGBtoHSL(r: number, g: number, b: number, out?: ColorComponents): ColorComponents;
static RGBtoHSV(r: number, g: number, b: number, out?: ColorComponents): ColorComponents;
static RGBtoString(r: number, g: number, b: number, a?: number, prefix?: string): string;