Skip to content

Commit

Permalink
feat: use new full-size mask image and masking algorithm; fixed #1
Browse files Browse the repository at this point in the history
  • Loading branch information
rikumi committed Jul 19, 2020
1 parent 7bbf6ba commit cf6ece2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iconsur",
"version": "1.5.2",
"version": "1.6.0",
"main": "src/index.js",
"license": "MIT",
"scripts": {
Expand Down
18 changes: 11 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ program.command('set <dir> [otherDirs...]').action(async (dir, otherDirs) => {
}
}

const imageSize = 256;
const iconPadding = Math.floor(imageSize * 0.1);
const imageSize = 1024;
const iconPadding = 100;
const iconSize = imageSize - 2 * iconPadding;
const mask = (await jimp.read(path.join(__dirname, 'mask.png'))).resize(iconSize, iconSize);
const mask = (await jimp.read(path.join(__dirname, 'mask.png'))).resize(imageSize, imageSize);
const region = program.region || 'us';
let resultIcon;
let data = null;
Expand All @@ -125,8 +125,7 @@ program.command('set <dir> [otherDirs...]').action(async (dir, otherDirs) => {
console.log(`If this app is incorrect, specify the correct name with -k or --keyword, or generate an icon locally with option -l or --local`);
const res = await fetch(iconUrl);
const iconData = await res.buffer();
const unmaskedImage = (await jimp.read(iconData)).resize(iconSize, iconSize);
resultIcon = unmaskedImage.mask(mask, 0, 0);
resultIcon = (await jimp.read(iconData)).resize(iconSize, iconSize);
} else {
if (!program.local) {
console.log(`Cannot find iOS App with name: ${appName}`);
Expand Down Expand Up @@ -171,10 +170,15 @@ program.command('set <dir> [otherDirs...]').action(async (dir, otherDirs) => {
}

const scalePosition = iconSize * (1 - originalIconScaleSize) / 2;
resultIcon = (await jimp.create(iconSize, iconSize, program.color || '#ffffff')).composite(originalIcon, scalePosition, scalePosition).mask(mask, 0, 0);
resultIcon = (await jimp.create(iconSize, iconSize)).composite(originalIcon, scalePosition, scalePosition);
}

const image = (await jimp.create(imageSize, imageSize, 0)).composite(resultIcon, iconPadding, iconPadding);
const image = (await jimp.create(imageSize, imageSize, program.color || '#ffffff')).composite(resultIcon, iconPadding, iconPadding);

// The masking algorithm that is both alpha- and color-friendly and full of magic
image.scan(0, 0, imageSize, imageSize, (x, y) => {
image.setPixelColor((mask.getPixelColor(x, y) & image.getPixelColor(x, y)) >>> 0, x, y);
});

if (program.output) {
program.output = String(program.output).replace(/(\..*)?$/, '.png');
Expand Down
Binary file modified src/mask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cf6ece2

Please sign in to comment.