Skip to content

Commit 0c0aec2

Browse files
committed
Add autoColorDark, autoColorLight options
+ Add `autoColorDark`, `autoColorLight` options # Optimize codes
1 parent fb2d6ef commit 0c0aec2

File tree

11 files changed

+96
-61
lines changed

11 files changed

+96
-61
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ typings/
6161
.next
6262

6363
# Temp
64-
temp
64+
temp
65+
66+
# Test
67+
test

demo/q2.jpg

-5.11 KB
Loading

demo/q2.png

-61 Bytes
Loading

demo/q3.jpg

-3.6 KB
Loading

demo/q3.png

-207 Bytes
Loading

demo/q4.jpg

-3.12 KB
Loading

demo/qrcode.svg

Lines changed: 1 addition & 1 deletion
Loading

index.js

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)
55
*
6-
* Version 4.1.0
6+
* Version 4.2.0
77
*
88
* @author [ [email protected] ]
99
*
@@ -1099,14 +1099,13 @@ Drawing.prototype.draw = function(oQRCode) {
10991099

11001100
_htOption.quietZone = Math.round(_htOption.quietZone);
11011101

1102-
this._htOption.width = nWidth * nCount;
1103-
this._htOption.height = nHeight * nCount + _htOption.titleHeight;
1102+
_htOption.width = nWidth * nCount;
1103+
_htOption.height = nHeight * nCount + _htOption.titleHeight;
11041104

1105+
this._canvas.width = _htOption.width + _htOption.quietZone * 2;
1106+
this._canvas.height = _htOption.height + _htOption.quietZone * 2;
11051107

1106-
this._canvas.width = this._htOption.width + this._htOption.quietZone * 2;
1107-
this._canvas.height = this._htOption.height + this._htOption.quietZone * 2;
1108-
1109-
if (this._htOption._drawer == 'svg') {
1108+
if (_htOption._drawer == 'svg') {
11101109
this._oContext = new C2S({
11111110
document: win.document,
11121111
XMLSerializer: win.XMLSerializer,
@@ -1122,28 +1121,33 @@ Drawing.prototype.draw = function(oQRCode) {
11221121

11231122
var _oContext = this._oContext;
11241123

1125-
var autoColorDark = "rgba(0, 0, 0, .6)";
1126-
var autoColorLight = "rgba(255, 255, 255, .7)";
1124+
var autoColorDark = _htOption.autoColorDark;
1125+
var autoColorLight = _htOption.autoColorLight;
11271126
var notAutoColorLight = "rgba(0,0,0,0)";
11281127

11291128
// JPG
11301129
if (_htOption.format == 'JPG') {
11311130

1132-
if (_htOption.quietZoneColor == "rgba(0,0,0,0)") {
1133-
_htOption.quietZoneColor = '#ffffff';
1134-
}
1135-
11361131
_htOption.logoBackgroundTransparent = false;
11371132

11381133
autoColorDark = _htOption.colorDark;
11391134
autoColorLight = _htOption.colorLight;
11401135
notAutoColorLight = _htOption.colorLight;
11411136

1142-
_oContext.fillStyle = "#ffffff";
1143-
_oContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
1137+
if (_htOption.backgroundImage) {
1138+
_oContext.fillStyle = _htOption.colorLight;
1139+
_oContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
1140+
} else {
1141+
if (_htOption.quietZoneColor == "rgba(0,0,0,0)" || _htOption.quietZoneColor == "transparent") {
1142+
_htOption.quietZoneColor = '#ffffff';
1143+
}
1144+
_oContext.fillStyle = _htOption.colorLight;
1145+
_oContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
1146+
1147+
}
11441148
} else {
11451149
_oContext.lineWidth = 0;
1146-
_oContext.fillStyle = this._htOption.colorLight;
1150+
_oContext.fillStyle = _htOption.colorLight;
11471151
_oContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
11481152
}
11491153

@@ -1186,7 +1190,6 @@ Drawing.prototype.draw = function(oQRCode) {
11861190
}
11871191

11881192
function drawQrcode(oQRCode) {
1189-
11901193
for (var row = 0; row < nCount; row++) {
11911194
for (var col = 0; col < nCount; col++) {
11921195
var nLeft = col * nWidth + _htOption.quietZone;
@@ -1198,6 +1201,51 @@ Drawing.prototype.draw = function(oQRCode) {
11981201

11991202
var nowDotScale = _htOption.dotScale;
12001203

1204+
_oContext.lineWidth = 0;
1205+
// Color handler
1206+
var dColor;
1207+
var lColor;
1208+
if (eye) {
1209+
dColor = _htOption[eye.type] || _htOption[eye.type.substring(
1210+
0, 2)] ||
1211+
_htOption.colorDark;
1212+
lColor = _htOption.colorLight;
1213+
} else {
1214+
if (_htOption.backgroundImage) {
1215+
1216+
lColor = "rgba(0,0,0,0)";
1217+
if (row == 6) {
1218+
dColor = _htOption.timing_H || _htOption.timing || _htOption.colorDark;
1219+
} else if (col == 6) {
1220+
dColor = _htOption.timing_V || _htOption.timing ||
1221+
_htOption.colorDark;
1222+
} else {
1223+
1224+
if (_htOption.autoColor) {
1225+
dColor = _htOption.autoColorDark;
1226+
lColor = _htOption.autoColorLight;
1227+
} else {
1228+
dColor = _htOption.colorDark;
1229+
}
1230+
}
1231+
1232+
} else {
1233+
if (row == 6) {
1234+
dColor = _htOption.timing_H || _htOption.timing || _htOption.colorDark;
1235+
} else if (col == 6) {
1236+
dColor = _htOption.timing_V || _htOption.timing ||
1237+
_htOption.colorDark;
1238+
} else {
1239+
dColor = _htOption.colorDark;
1240+
}
1241+
lColor = _htOption.colorLight;
1242+
}
1243+
}
1244+
_oContext.strokeStyle = bIsDark ? dColor :
1245+
lColor;
1246+
_oContext.fillStyle = bIsDark ? dColor :
1247+
lColor;
1248+
12011249
if (eye) {
12021250
// Is eye
12031251
bIsDark = eye.isDarkBlock;
@@ -1210,31 +1258,9 @@ Drawing.prototype.draw = function(oQRCode) {
12101258
nowDotScale = 1;
12111259
}
12121260

1213-
// PX_XX, PX, colorDark, colorLight
1214-
var eyeColorDark = _htOption[type] || _htOption[type.substring(0, 2)] || _htOption.colorDark;
1215-
_oContext.lineWidth = 0;
1216-
_oContext.strokeStyle = bIsDark ? eyeColorDark : _htOption.colorLight;
1217-
_oContext.fillStyle = bIsDark ? eyeColorDark : _htOption.colorLight;
1218-
1219-
_oContext.fillRect(nLeft + nWidth * (1 - nowDotScale) / 2, _htOption.titleHeight + nTop + nHeight * (1 - nowDotScale) / 2, nWidth * nowDotScale, nHeight * nowDotScale);
1261+
_oContext.fillRect(nLeft + nWidth * (1 - nowDotScale) / 2, _htOption.titleHeight + nTop +
1262+
nHeight * (1 - nowDotScale) / 2, nWidth * nowDotScale, nHeight * nowDotScale);
12201263
} else {
1221-
_oContext.lineWidth = 0;
1222-
_oContext.strokeStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight;
1223-
_oContext.fillStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight;
1224-
1225-
if (_htOption.backgroundImage) {
1226-
1227-
if (_htOption.autoColor) {
1228-
_oContext.strokeStyle = bIsDark ? autoColorDark : autoColorLight;
1229-
_oContext.fillStyle = bIsDark ? autoColorDark : autoColorLight;
1230-
} else {
1231-
_oContext.fillStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight;
1232-
_oContext.strokeStyle = _oContext.fillStyle;
1233-
}
1234-
} else {
1235-
_oContext.strokeStyle = _oContext.fillStyle;
1236-
1237-
}
12381264

12391265
if (row == 6) {
12401266
// Timing Pattern
@@ -1532,7 +1558,9 @@ function QRCode(vOption) {
15321558
// ==== Backgroud Image
15331559
backgroundImage: undefined, // Background Image
15341560
backgroundImageAlpha: 1, // Background image transparency, value between 0 and 1. default is 1.
1535-
autoColor: false,
1561+
autoColor: false, // Automatic color adjustment(for data block)
1562+
autoColorDark: "rgba(0, 0, 0, .6)", // Automatic color: dark CSS color
1563+
autoColorLight: "rgba(255, 255, 255, .7)", // Automatic: color light CSS color
15361564

15371565
// ==== Event Handler
15381566
onRenderingStart: undefined,
@@ -1667,7 +1695,7 @@ function QRCode(vOption) {
16671695

16681696
// Save to image file or svg file
16691697
QRCode.prototype._toSave = function(saveOptions) {
1670-
var _oDrawing = new Drawing(this._htOption);
1698+
var _oDrawing = new Drawing(Object.assign({}, this._htOption));
16711699
_oDrawing.makeOptions = saveOptions;
16721700

16731701
try {
@@ -1717,7 +1745,7 @@ QRCode.prototype._toData = function(drawer) {
17171745
}
17181746
this._htOption._drawer = drawer;
17191747

1720-
var _oDrawing = new Drawing(this._htOption);
1748+
var _oDrawing = new Drawing(Object.assign({}, this._htOption));
17211749
_oDrawing.makeOptions = defOptions;
17221750

17231751
try {

index.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "easyqrcodejs-nodejs",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"description": "NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)",
55
"main": "index.min.js",
66
"scripts": {},

0 commit comments

Comments
 (0)