Skip to content

Commit b0f27b3

Browse files
committed
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.
1 parent eda4961 commit b0f27b3

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
342342
* 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.
343343
* Math.getNextPowerOfTwo will get the next power of two for the given value.
344344
* Math.isPowerOfTwo will return a boolean if the given width and height are a power of two.
345+
* Color.hexToRGBArray converts a hex color value to an [R, G, B] array.
346+
* Color.RGBArrayToHex converts an RGB color array, in the format: [R, G, B], to a hex color value.
345347

346348
### Bug Fixes
347349

src/utils/Color.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,38 @@ Phaser.Color = {
158158

159159
},
160160

161+
/**
162+
* Converts a hex color value to an [R, G, B] array.
163+
*
164+
* @static
165+
* @method Phaser.Color.hexToRGBArray
166+
* @param {number} color - The color to convert to an RGB array. In the format 0xRRGGBB.
167+
* @return {array} An array with element 0 containing the Red value, 1 containing Green, and 2 containing Blue.
168+
*/
169+
hexToRGBArray: function (color) {
170+
171+
return [
172+
(color >> 16 & 0xFF) / 255,
173+
(color >> 8 & 0xFF) / 255,
174+
(color & 0xFF) / 255
175+
];
176+
177+
},
178+
179+
/**
180+
* Converts an RGB color array, in the format: [R, G, B], to a hex color value.
181+
*
182+
* @static
183+
* @method Phaser.Color.RGBArrayToHex
184+
* @param {array} rgb - An array with element 0 containing the Red value, 1 containing Green, and 2 containing Blue.
185+
* @return {number} The color value, in the format 0xRRGGBB.
186+
*/
187+
RGBArrayToHex: function (rgb) {
188+
189+
return ((rgb[0] * 255 << 16) + (rgb[1] * 255 << 8) + rgb[2] * 255);
190+
191+
},
192+
161193
/**
162194
* Converts an RGB color value to HSL (hue, saturation and lightness).
163195
* Conversion forumla from http://en.wikipedia.org/wiki/HSL_color_space.

typescript/phaser.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ declare module Phaser {
749749
static getRed(color: number): number;
750750
static getRGB(color: number): RGBColor;
751751
static getWebRGB(color: number | RGBColor): string;
752+
static hexToRGBArray(color: number): number[];
752753
static hexToRGB(h: string): number;
753754
static hexToColor(hex: string, out?: ColorComponents): ColorComponents;
754755
static HSLtoRGB(h: number, s: number, l: number, out?: ColorComponents): ColorComponents;
@@ -760,6 +761,7 @@ declare module Phaser {
760761
static interpolateColorWithRGB(color: number, r: number, g: number, b: number, steps: number, currentStep: number): number;
761762
static interpolateRGB(r1: number, g1: number, b1: number, r2: number, g2: number, b2: number, steps: number, currentStep: number): number;
762763
static packPixel(r: number, g: number, b: number, a: number): number;
764+
static RGBArrayToHex(rgb: number[]): number;
763765
static RGBtoHSL(r: number, g: number, b: number, out?: ColorComponents): ColorComponents;
764766
static RGBtoHSV(r: number, g: number, b: number, out?: ColorComponents): ColorComponents;
765767
static RGBtoString(r: number, g: number, b: number, a?: number, prefix?: string): string;

0 commit comments

Comments
 (0)