Skip to content

Commit fcbcb9b

Browse files
committed
Add color object to accept array of rgb(a)
1 parent 3b0352a commit fcbcb9b

4 files changed

Lines changed: 53 additions & 5 deletions

File tree

dist/html2canvas.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,8 @@ function Color(value) {
826826
this.g = 0;
827827
this.b = 0;
828828
this.a = null;
829-
var result = this.namedColor(value) ||
829+
var result = this.fromArray(value) ||
830+
this.namedColor(value) ||
830831
this.rgb(value) ||
831832
this.rgba(value) ||
832833
this.hex6(value) ||
@@ -837,6 +838,19 @@ Color.prototype.isTransparent = function() {
837838
return this.a === 0;
838839
};
839840

841+
Color.prototype.fromArray = function(array) {
842+
if (Array.isArray(array)) {
843+
this.r = array[0];
844+
this.g = array[1];
845+
this.b = array[2];
846+
if (array.length > 3) {
847+
this.a = array[3];
848+
}
849+
}
850+
851+
return (Array.isArray(array));
852+
};
853+
840854
var _hex3 = /^#([a-f0-9]{3})$/i;
841855

842856
Color.prototype.hex3 = function(value) {
@@ -888,7 +902,7 @@ Color.prototype.rgba = function(value) {
888902
};
889903

890904
Color.prototype.toString = function() {
891-
return this.a !== null ?
905+
return this.a !== null && this.a !== 1 ?
892906
"rgba(" + [this.r, this.g, this.b, this.a].join(",") + ")" :
893907
"rgb(" + [this.r, this.g, this.b].join(",") + ")";
894908
};

dist/html2canvas.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/color.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ function Color(value) {
55
this.g = 0;
66
this.b = 0;
77
this.a = null;
8-
var result = this.namedColor(value) ||
8+
var result = this.fromArray(value) ||
9+
this.namedColor(value) ||
910
this.rgb(value) ||
1011
this.rgba(value) ||
1112
this.hex6(value) ||
@@ -16,6 +17,19 @@ Color.prototype.isTransparent = function() {
1617
return this.a === 0;
1718
};
1819

20+
Color.prototype.fromArray = function(array) {
21+
if (Array.isArray(array)) {
22+
this.r = array[0];
23+
this.g = array[1];
24+
this.b = array[2];
25+
if (array.length > 3) {
26+
this.a = array[3];
27+
}
28+
}
29+
30+
return (Array.isArray(array));
31+
};
32+
1933
var _hex3 = /^#([a-f0-9]{3})$/i;
2034

2135
Color.prototype.hex3 = function(value) {
@@ -67,7 +81,7 @@ Color.prototype.rgba = function(value) {
6781
};
6882

6983
Color.prototype.toString = function() {
70-
return this.a !== null ?
84+
return this.a !== null && this.a !== 1 ?
7185
"rgba(" + [this.r, this.g, this.b, this.a].join(",") + ")" :
7286
"rgb(" + [this.r, this.g, this.b].join(",") + ")";
7387
};

tests/mocha/colors.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@
8686
});
8787
});
8888

89+
describe("from array", function() {
90+
it("[1,2,3]", function () {
91+
var c = new Color([1,2,3]);
92+
assertColor(c, 1, 2, 3, null);
93+
expect(c.isTransparent()).to.equal(false);
94+
});
95+
96+
it("[5,6,7,1]", function () {
97+
var c = new Color([5,6,7, 1]);
98+
assertColor(c, 5, 6, 7, 1);
99+
expect(c.isTransparent()).to.equal(false);
100+
});
101+
102+
it("[5,6,7,0]", function () {
103+
var c = new Color([5,6,7, 0]);
104+
assertColor(c, 5, 6, 7, 0);
105+
expect(c.isTransparent()).to.equal(true);
106+
});
107+
});
108+
89109
describe("transparency", function() {
90110
it("transparent", function () {
91111
var c = new Color("transparent");

0 commit comments

Comments
 (0)