Skip to content

Commit

Permalink
all my homies hate sharp. Sharp dependencies removed and reqrite of m…
Browse files Browse the repository at this point in the history
…ultiple files using it changed to use magickwand (already using dependencies) and removed external use of magickwand ()resizeIamge.js, addIconsBackground.js, dds to from png js files) removed python scripts
  • Loading branch information
ghostboats committed May 15, 2024
1 parent b10c1de commit afaa516
Show file tree
Hide file tree
Showing 58 changed files with 48 additions and 16,527 deletions.
54 changes: 31 additions & 23 deletions commands/addIconBackground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const vscode = require('vscode');
const sharp = require('sharp');
const path = require('path');
const fs = require('fs').promises; // Use promises version of fs for async/await compatibility
const fs = require('fs').promises;
const { Magick, MagickCore } = require('magickwand.js');

async function addIconBackground(uri) {
const inputPath = uri.fsPath;
Expand All @@ -10,17 +10,14 @@ async function addIconBackground(uri) {
try {
const files = await fs.readdir(backgroundsDir);
const pngFiles = files.filter(file => file.endsWith('.png'));
// Ensure each option has an isCustom property for consistent handling
const backgroundOptions = pngFiles.map(file => ({ label: file, isCustom: false }));
backgroundOptions.push({ label: 'Custom Background...', isCustom: true });

// Prompt user for background selection
const selectedBackground = await vscode.window.showQuickPick(backgroundOptions, { placeHolder: 'Select a background' });
if (!selectedBackground) return;

let backgroundPath;
if (selectedBackground.isCustom) {
// Handle custom background selection
const customUri = await vscode.window.showOpenDialog({
openLabel: 'Use Background',
canSelectMany: false,
Expand All @@ -29,42 +26,53 @@ async function addIconBackground(uri) {
if (customUri && customUri[0]) {
backgroundPath = customUri[0].fsPath;
} else {
return; // No file selected, exit the function
return;
}
} else {
// Use selected predefined background
backgroundPath = path.join(backgroundsDir, selectedBackground.label);
}

const outputPath = inputPath.replace(/\.\w+$/, `_with_background.png`);

// Load both images to compare sizes
const iconImage = sharp(inputPath);
const background = sharp(backgroundPath);
const [iconMetadata, backgroundMetadata] = await Promise.all([iconImage.metadata(), background.metadata()]);
const iconImage = new Magick.Image;
await iconImage.readAsync(inputPath);
const background = new Magick.Image;
await background.readAsync(backgroundPath);

const iconSize = await iconImage.sizeAsync();
const backgroundSize = await background.sizeAsync();
const iconWidth = iconSize.width();
const iconHeight = iconSize.height();
let backgroundWidth = backgroundSize.width();
let backgroundHeight = backgroundSize.height();

if (iconMetadata.width !== backgroundMetadata.width || iconMetadata.height !== backgroundMetadata.height) {
if (iconWidth !== backgroundWidth || iconHeight !== backgroundHeight) {
const resizeBackground = await vscode.window.showInformationMessage(
'The background and icon sizes do not match. Resize the background to match the icon?',
'Yes', 'No'
);
if (resizeBackground === 'Yes') {
await background.resize(iconMetadata.width, iconMetadata.height);
await background.resizeAsync(`${iconWidth}x${iconHeight}`);
const backgroundSizeResize = await background.sizeAsync();
backgroundWidth = backgroundSizeResize.width();
backgroundHeight = backgroundSizeResize.height();
} else {
return;
}
}

// Composite the icon over the resized background
background
.composite([{ input: await iconImage.toBuffer(), gravity: 'centre' }])
.toFile(outputPath)
.then(() => {
vscode.window.showInformationMessage(`Background added: ${outputPath}`);
})
.catch(err => {
vscode.window.showErrorMessage(`Error adding background: ${err}`);
});
const xOffset = Math.floor((backgroundWidth - iconWidth) / 2);
const yOffset = Math.floor((backgroundHeight - iconHeight) / 2);

console.log(`Calculated xOffset: ${xOffset}, yOffset: ${yOffset}`);

// Create a Geometry object for the composite operation
const geometry = new Magick.Geometry(iconWidth, iconHeight, xOffset, yOffset);
console.log('Attempting to composite with geometry:', geometry.toString());
await background.compositeAsync(iconImage, geometry, MagickCore.OverCompositeOp);

await background.writeAsync(outputPath);
vscode.window.showInformationMessage(`Background added: ${outputPath}`);
} catch (err) {
vscode.window.showErrorMessage(`Failed to read backgrounds directory or process images: ${err}`);
}
Expand Down
25 changes: 12 additions & 13 deletions commands/resizeImage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const sharp = require('sharp');
const vscode = require('vscode');
const { Magick } = require('magickwand.js');

async function resizeImage(uri, width = null, height = null) {
console.log('‾‾resizeImage‾‾');
Expand All @@ -24,22 +24,21 @@ async function resizeImage(uri, width = null, height = null) {

const outputPath = inputPath.replace(/\.\w+$/, `_resized_${width}x${height}.png`);

sharp(inputPath)
.resize(width, height)
.toFile(outputPath)
.then(() => {
vscode.window.showInformationMessage(`Image resized to ${width}x${height}: ${outputPath}`);
})
.catch(err => {
vscode.window.showErrorMessage(`Error resizing image: ${err}`);
});
try {
let image = new Magick.Image();
await image.readAsync(inputPath);
await image.scaleAsync(`${width}x${height}`);
await image.writeAsync(outputPath);
vscode.window.showInformationMessage(`Image resized to ${width}x${height}: ${outputPath}`);
} catch (err) {
vscode.window.showErrorMessage(`Error resizing image: ${err}`);
}
console.log('__resizeImage__');
}

module.exports = {
resizeImageTooltip: (uri) => resizeImage(uri, 380, 380),
resizeImageController: (uri) => resizeImage(uri, 144, 144),
resizeImageHotbar: (uri) => resizeImage(uri, 64, 64),
resizeImageCustom: resizeImage // Using the same function for custom resizing
// ... other exports ...
};
resizeImageCustom: resizeImage
};
75 changes: 1 addition & 74 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions node_modules/color-string/LICENSE

This file was deleted.

62 changes: 0 additions & 62 deletions node_modules/color-string/README.md

This file was deleted.

Loading

0 comments on commit afaa516

Please sign in to comment.