Skip to content

Commit 697fcf5

Browse files
committed
ignore SVG default colors
1 parent aef10b2 commit 697fcf5

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

src/renderers/svg.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ class SVGRenderer{
1515
var currentX = this.options.marginLeft;
1616

1717
this.prepareSVG();
18+
19+
// Create Background IF not (SVG default) `transparent`
20+
if(!this.options.background.match(/transparent|#[a-f\d]{3}0|#[a-f\d]{6}0{2}/i)){
21+
this.drawRect(0, 0, "100%", "100%", this.svg).setAttribute("fill", this.options.background);
22+
}
23+
1824
for(let i = 0; i < this.encodings.length; i++){
1925
var encoding = this.encodings[i];
2026
var encodingOptions = merge(this.options, encoding.options);
2127

2228
var group = this.createGroup(currentX, encodingOptions.marginTop, this.svg);
23-
group.setAttribute("fill", encodingOptions.lineColor);
29+
30+
// Set color IF not (SVG default) `black`
31+
if(!encodingOptions.lineColor.match(/black|#0{3}f?|#0{6}(?:f{2})?/i)){
32+
group.setAttribute("fill", encodingOptions.lineColor);
33+
}
2434

2535
this.drawSvgBarcode(group, encodingOptions, encoding);
2636
this.drawSVGText(group, encodingOptions, encoding);
@@ -31,20 +41,14 @@ class SVGRenderer{
3141

3242
prepareSVG(){
3343
// Clear the SVG
34-
while (this.svg.firstChild){
35-
this.svg.removeChild(this.svg.firstChild);
36-
}
44+
this.svg.replaceChildren();
3745

3846
calculateEncodingAttributes(this.encodings, this.options);
3947
var totalWidth = getTotalWidthOfEncodings(this.encodings);
4048
var maxHeight = getMaximumHeightOfEncodings(this.encodings);
4149

4250
var width = totalWidth + this.options.marginLeft + this.options.marginRight;
4351
this.setSvgAttributes(width, maxHeight);
44-
45-
if(this.options.background){
46-
this.drawRect(0, 0, "100%", "100%", this.svg).setAttribute("fill", this.options.background);
47-
}
4852
}
4953

5054
drawSvgBarcode(parent, options, encoding){

0 commit comments

Comments
 (0)