Skip to content

Commit

Permalink
Add autoColorDark, autoColorLight options
Browse files Browse the repository at this point in the history
 + Add `autoColorDark`, `autoColorLight` options
# Optimize codes
  • Loading branch information
ushelp committed Jan 2, 2021
1 parent fb2d6ef commit 0c0aec2
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 61 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ typings/
.next

# Temp
temp
temp

# Test
test
Binary file modified demo/q2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/q2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/q3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/q3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/q4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion demo/qrcode.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
116 changes: 72 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* 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)
*
* Version 4.1.0
* Version 4.2.0
*
* @author [ [email protected] ]
*
Expand Down Expand Up @@ -1099,14 +1099,13 @@ Drawing.prototype.draw = function(oQRCode) {

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

this._htOption.width = nWidth * nCount;
this._htOption.height = nHeight * nCount + _htOption.titleHeight;
_htOption.width = nWidth * nCount;
_htOption.height = nHeight * nCount + _htOption.titleHeight;

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

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

if (this._htOption._drawer == 'svg') {
if (_htOption._drawer == 'svg') {
this._oContext = new C2S({
document: win.document,
XMLSerializer: win.XMLSerializer,
Expand All @@ -1122,28 +1121,33 @@ Drawing.prototype.draw = function(oQRCode) {

var _oContext = this._oContext;

var autoColorDark = "rgba(0, 0, 0, .6)";
var autoColorLight = "rgba(255, 255, 255, .7)";
var autoColorDark = _htOption.autoColorDark;
var autoColorLight = _htOption.autoColorLight;
var notAutoColorLight = "rgba(0,0,0,0)";

// JPG
if (_htOption.format == 'JPG') {

if (_htOption.quietZoneColor == "rgba(0,0,0,0)") {
_htOption.quietZoneColor = '#ffffff';
}

_htOption.logoBackgroundTransparent = false;

autoColorDark = _htOption.colorDark;
autoColorLight = _htOption.colorLight;
notAutoColorLight = _htOption.colorLight;

_oContext.fillStyle = "#ffffff";
_oContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
if (_htOption.backgroundImage) {
_oContext.fillStyle = _htOption.colorLight;
_oContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
} else {
if (_htOption.quietZoneColor == "rgba(0,0,0,0)" || _htOption.quietZoneColor == "transparent") {
_htOption.quietZoneColor = '#ffffff';
}
_oContext.fillStyle = _htOption.colorLight;
_oContext.fillRect(0, 0, this._canvas.width, this._canvas.height);

}
} else {
_oContext.lineWidth = 0;
_oContext.fillStyle = this._htOption.colorLight;
_oContext.fillStyle = _htOption.colorLight;
_oContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
}

Expand Down Expand Up @@ -1186,7 +1190,6 @@ Drawing.prototype.draw = function(oQRCode) {
}

function drawQrcode(oQRCode) {

for (var row = 0; row < nCount; row++) {
for (var col = 0; col < nCount; col++) {
var nLeft = col * nWidth + _htOption.quietZone;
Expand All @@ -1198,6 +1201,51 @@ Drawing.prototype.draw = function(oQRCode) {

var nowDotScale = _htOption.dotScale;

_oContext.lineWidth = 0;
// Color handler
var dColor;
var lColor;
if (eye) {
dColor = _htOption[eye.type] || _htOption[eye.type.substring(
0, 2)] ||
_htOption.colorDark;
lColor = _htOption.colorLight;
} else {
if (_htOption.backgroundImage) {

lColor = "rgba(0,0,0,0)";
if (row == 6) {
dColor = _htOption.timing_H || _htOption.timing || _htOption.colorDark;
} else if (col == 6) {
dColor = _htOption.timing_V || _htOption.timing ||
_htOption.colorDark;
} else {

if (_htOption.autoColor) {
dColor = _htOption.autoColorDark;
lColor = _htOption.autoColorLight;
} else {
dColor = _htOption.colorDark;
}
}

} else {
if (row == 6) {
dColor = _htOption.timing_H || _htOption.timing || _htOption.colorDark;
} else if (col == 6) {
dColor = _htOption.timing_V || _htOption.timing ||
_htOption.colorDark;
} else {
dColor = _htOption.colorDark;
}
lColor = _htOption.colorLight;
}
}
_oContext.strokeStyle = bIsDark ? dColor :
lColor;
_oContext.fillStyle = bIsDark ? dColor :
lColor;

if (eye) {
// Is eye
bIsDark = eye.isDarkBlock;
Expand All @@ -1210,31 +1258,9 @@ Drawing.prototype.draw = function(oQRCode) {
nowDotScale = 1;
}

// PX_XX, PX, colorDark, colorLight
var eyeColorDark = _htOption[type] || _htOption[type.substring(0, 2)] || _htOption.colorDark;
_oContext.lineWidth = 0;
_oContext.strokeStyle = bIsDark ? eyeColorDark : _htOption.colorLight;
_oContext.fillStyle = bIsDark ? eyeColorDark : _htOption.colorLight;

_oContext.fillRect(nLeft + nWidth * (1 - nowDotScale) / 2, _htOption.titleHeight + nTop + nHeight * (1 - nowDotScale) / 2, nWidth * nowDotScale, nHeight * nowDotScale);
_oContext.fillRect(nLeft + nWidth * (1 - nowDotScale) / 2, _htOption.titleHeight + nTop +
nHeight * (1 - nowDotScale) / 2, nWidth * nowDotScale, nHeight * nowDotScale);
} else {
_oContext.lineWidth = 0;
_oContext.strokeStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight;
_oContext.fillStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight;

if (_htOption.backgroundImage) {

if (_htOption.autoColor) {
_oContext.strokeStyle = bIsDark ? autoColorDark : autoColorLight;
_oContext.fillStyle = bIsDark ? autoColorDark : autoColorLight;
} else {
_oContext.fillStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight;
_oContext.strokeStyle = _oContext.fillStyle;
}
} else {
_oContext.strokeStyle = _oContext.fillStyle;

}

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

// ==== Event Handler
onRenderingStart: undefined,
Expand Down Expand Up @@ -1667,7 +1695,7 @@ function QRCode(vOption) {

// Save to image file or svg file
QRCode.prototype._toSave = function(saveOptions) {
var _oDrawing = new Drawing(this._htOption);
var _oDrawing = new Drawing(Object.assign({}, this._htOption));
_oDrawing.makeOptions = saveOptions;

try {
Expand Down Expand Up @@ -1717,7 +1745,7 @@ QRCode.prototype._toData = function(drawer) {
}
this._htOption._drawer = drawer;

var _oDrawing = new Drawing(this._htOption);
var _oDrawing = new Drawing(Object.assign({}, this._htOption));
_oDrawing.makeOptions = defOptions;

try {
Expand Down
4 changes: 2 additions & 2 deletions index.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easyqrcodejs-nodejs",
"version": "4.1.0",
"version": "4.2.0",
"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)",
"main": "index.min.js",
"scripts": {},
Expand Down
28 changes: 16 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,18 @@ var qrcode = new QRCode(options);
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H, // L, M, Q, H

// ====== dotScale
/*
dotScale: 1, // For body block, must be greater than 0, less than or equal to 1. default is 1
// ====== dotScale
/*
dotScale: 1, // For body block, must be greater than 0, less than or equal to 1. default is 1
dotScaleTiming: 1, // Dafault for timing block , must be greater than 0, less than or equal to 1. default is 1
dotScaleTiming_H: undefined, // For horizontal timing block, must be greater than 0, less than or equal to 1. default is 1
dotScaleTiming_V: undefined, // For vertical timing block, must be greater than 0, less than or equal to 1. default is 1
dotScaleTiming: 1, // Dafault for timing block , must be greater than 0, less than or equal to 1. default is 1
dotScaleTiming_H: undefined, // For horizontal timing block, must be greater than 0, less than or equal to 1. default is 1
dotScaleTiming_V: undefined, // For vertical timing block, must be greater than 0, less than or equal to 1. default is 1
dotScaleA: 1, // Dafault for alignment block, must be greater than 0, less than or equal to 1. default is 1
dotScaleAO: undefined, // For alignment outer block, must be greater than 0, less than or equal to 1. default is 1
dotScaleAI: undefined, // For alignment inner block, must be greater than 0, less than or equal to 1. default is 1
*/
dotScaleA: 1, // Dafault for alignment block, must be greater than 0, less than or equal to 1. default is 1
dotScaleAO: undefined, // For alignment outer block, must be greater than 0, less than or equal to 1. default is 1
dotScaleAI: undefined, // For alignment inner block, must be greater than 0, less than or equal to 1. default is 1
*/

// ====== Quiet Zone
/*
Expand All @@ -186,7 +186,9 @@ var qrcode = new QRCode(options);
/*
backgroundImage: '', // Background Image
backgroundImageAlpha: 1, // Background image transparency, value between 0 and 1. default is 1.
autoColor: false,
autoColor: false, // Automatic color adjustment(for data block)
autoColorDark: "rgba(0, 0, 0, .6)", // Automatic color: dark CSS color
autoColorLight: "rgba(255, 255, 255, .7)", // Automatic color: light CSS color
*/

// ====== Colorful
Expand Down Expand Up @@ -284,7 +286,9 @@ var qrcode = new QRCode(options);
| Backgroud Image options| ---|--- |---|---|
| **backgroundImage** | N | String | `undefined` | Background Image Path or Base64 encoded image. If use relative address, relative to `easy.qrcode.min.js` |
| **backgroundImageAlpha** | N | Number | `1.0` | Background image transparency. Ranges: `0-1.0` |
| **autoColor** | N | Boolean | `false` | Automatic color adjustment |
| **autoColor** | N | Boolean | `false` | Automatic color adjustment(for data block) |
| **autoColorDark** | N | String | `rgba(0, 0, 0, .6)` | Automatic color: dark CSS color |
| **autoColorLight** | N | String | `rgba(255, 255, 255, .7)` | Automatic color: light CSS color |
| Posotion Pattern Color options| --- | ---|---|---|
| **PO** | N | String | `undefined` | Global Posotion Outer CSS color. if not set, the defaut is `colorDark` |
| **PI** | N | String | `undefined` | Global Posotion Inner CSS color. if not set, the defaut is `colorDark` |
Expand Down

0 comments on commit 0c0aec2

Please sign in to comment.