-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
98 lines (80 loc) · 3.15 KB
/
Copy pathcli.js
File metadata and controls
98 lines (80 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
const { program } = require('commander');
const { readFileSync, writeFileSync, existsSync } = require('fs');
const { basename, extname, dirname, join } = require('path');
const { processImageSync, detectBackgroundColor } = require('./index.js');
const packageJson = require('./package.json');
program
.name('bgone')
.description('Remove solid background colors from images')
.version(packageJson.version)
.argument('<input>', 'The input image file')
.argument('[output]', 'Output image file (defaults to input-bgone.png)')
.option('-b, --bg <color>', 'Background color to remove (hex, e.g. #ffffff or fff)')
.option('-f, --fg <colors...>', 'Foreground colors (hex or "auto" for deduction)')
.option('-s, --strict', 'Strict mode - only use specified foreground colors', false)
.option('-t, --threshold <value>', 'Color closeness threshold (0.0-1.0)', parseFloat)
.option('--trim', 'Trim output to content bounding box', false)
.option('--detect', 'Only detect and print background color, do not process')
.action((input, output, options) => {
if (!existsSync(input)) {
console.error(`Error: Input file not found: ${input}`);
process.exit(1);
}
const inputBuffer = readFileSync(input);
if (options.detect) {
const bgColor = detectBackgroundColor(inputBuffer);
const hex = `#${bgColor.r.toString(16).padStart(2, '0')}${bgColor.g.toString(16).padStart(2, '0')}${bgColor.b.toString(16).padStart(2, '0')}`;
console.log(`Detected background color: ${hex} (rgb(${bgColor.r}, ${bgColor.g}, ${bgColor.b}))`);
return;
}
const outputPath = output || generateOutputPath(input);
console.log(`Processing: ${input}`);
if (options.bg) {
console.log(` Background: ${options.bg}`);
} else {
const bgColor = detectBackgroundColor(inputBuffer);
const hex = `#${bgColor.r.toString(16).padStart(2, '0')}${bgColor.g.toString(16).padStart(2, '0')}${bgColor.b.toString(16).padStart(2, '0')}`;
console.log(` Background: ${hex} (auto-detected)`);
}
if (options.fg) {
console.log(` Foreground: ${options.fg.join(', ')}`);
}
if (options.strict) {
console.log(` Mode: strict`);
}
if (options.threshold !== undefined) {
console.log(` Threshold: ${options.threshold}`);
}
if (options.trim) {
console.log(` Trim: enabled`);
}
try {
const result = processImageSync({
input: inputBuffer,
backgroundColor: options.bg,
foregroundColors: options.fg,
strictMode: options.strict,
threshold: options.threshold,
trim: options.trim,
});
writeFileSync(outputPath, result);
console.log(`Output: ${outputPath}`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
});
function generateOutputPath(input) {
const dir = dirname(input);
const ext = extname(input);
const name = basename(input, ext);
let outputPath = join(dir, `${name}-bgone.png`);
let counter = 1;
while (existsSync(outputPath)) {
outputPath = join(dir, `${name}-bgone-${counter}.png`);
counter++;
}
return outputPath;
}
program.parse();