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

Added inverted option. #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ If you want to display small output, provide `opts` with `small`:
console.log(qrcode)
});

If you want to invert the output, provide `opts` with `small`:

qrcode.generate('This will be a inverted QRCode, yeey!', inverted: true});


# Command-Line

## Install
Expand Down
22 changes: 17 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var QRCode = require('./../vendor/QRCode'),
white = "\033[47m \033[0m",
toCell = function (isBlack) {
return isBlack ? black : white;
}
toCellInverted = function (isBlack) {
return isBlack ? white : black;
},
repeat = function (color) {
return {
Expand Down Expand Up @@ -52,9 +55,17 @@ module.exports = {
BLACK_ALL: ' ',
};

if(opts.inverted) {
var clone = Object.assign({}, platte);
platte.WHITE_ALL = clone.BLACK_ALL;
platte.WHITE_BLACK = clone.BLACK_WHITE;
platte.BLACK_WHITE = clone.WHITE_BLACK;
platte.BLACK_ALL = clone.WHITE_ALL;
}

var borderTop = repeat(platte.BLACK_WHITE).times(moduleCount + 3);
var borderBottom = repeat(platte.WHITE_BLACK).times(moduleCount + 3);
output += borderTop + '\n';
output += (!opts.inverted ? borderTop : repeat(platte.WHITE_ALL).times(moduleCount + 3)) + '\n';

for (var row = 0; row < moduleCount; row += 2) {
output += platte.WHITE_ALL;
Expand All @@ -78,13 +89,14 @@ module.exports = {
output += borderBottom;
}
} else {
var border = repeat(white).times(qrcode.getModuleCount() + 3);
var flip = opts && opts.inverted;
var border = repeat(flip ? black : white).times(qrcode.getModuleCount() + 3);

output += border + '\n';
qrcode.modules.forEach(function (row) {
output += white;
output += row.map(toCell).join('');
output += white + '\n';
output += flip ? black : white;
output += row.map( (opts && opts.inverted) ? toCellInverted : toCell).join('');
output += (flip ? black : white) + '\n';
});
output += border;
}
Expand Down