diff --git a/src/color.class.js b/src/color.class.js index 697cc07f9b6..7c4fd6f8e6a 100644 --- a/src/color.class.js +++ b/src/color.class.js @@ -126,7 +126,7 @@ }, /** - * Returns color represenation in RGB format + * Returns color representation in RGB format * @return {String} ex: rgb(0-255,0-255,0-255) */ toRgb: function() { @@ -135,7 +135,7 @@ }, /** - * Returns color represenation in RGBA format + * Returns color representation in RGBA format * @return {String} ex: rgba(0-255,0-255,0-255,0-1) */ toRgba: function() { @@ -144,7 +144,7 @@ }, /** - * Returns color represenation in HSL format + * Returns color representation in HSL format * @return {String} ex: hsl(0-360,0%-100%,0%-100%) */ toHsl: function() { @@ -155,7 +155,7 @@ }, /** - * Returns color represenation in HSLA format + * Returns color representation in HSLA format * @return {String} ex: hsla(0-360,0%-100%,0%-100%,0-1) */ toHsla: function() { @@ -166,7 +166,7 @@ }, /** - * Returns color represenation in HEX format + * Returns color representation in HEX format * @return {String} ex: FF5555 */ toHex: function() { @@ -184,6 +184,20 @@ return r.toUpperCase() + g.toUpperCase() + b.toUpperCase(); }, + /** + * Returns color representation in HEXA format + * @return {String} ex: FF5555CC + */ + toHexa: function() { + var source = this.getSource(), a; + + a = source[3] * 255; + a = a.toString(16); + a = (a.length === 1) ? ('0' + a) : a; + + return this.toHex() + a.toUpperCase(); + }, + /** * Gets value of alpha channel for this color * @return {Number} 0-1 diff --git a/test/unit/color.js b/test/unit/color.js index 5c1ca2e1e16..7e7f3c8c620 100644 --- a/test/unit/color.js +++ b/test/unit/color.js @@ -84,12 +84,22 @@ equal(oColor.toHex(), '000000'); }); + test('toHexa', function() { + var oColor = new fabric.Color('ffffffff'); + ok(typeof oColor.toHexa == 'function'); + equal(oColor.toHexa(), 'FFFFFFFF'); + oColor.setSource([255,255,255,0.8]); + equal(oColor.toHexa(), 'FFFFFFCC'); + }); + test('getAlpha', function() { var oColor = new fabric.Color('ffffff'); ok(typeof oColor.getAlpha == 'function'); equal(oColor.getAlpha(), 1); oColor.setSource([10,20,30, 0.456]); equal(oColor.getAlpha(), 0.456); + oColor = new fabric.Color('ffffffcc'); + equal(oColor.getAlpha(), 0.8); }); test('setAlpha', function() {