Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color.class update #3615

Merged
merged 19 commits into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/color.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -166,7 +166,7 @@
},

/**
* Returns color represenation in HEX format
* Returns color representation in HEX format
* @return {String} ex: FF5555
*/
toHex: function() {
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/unit/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down