colorgen/colorgen.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-03-12 19:01:06 +01:00
#!/usr/bin/env node
import { program } from 'commander';
2023-03-19 14:28:35 +01:00
import Colorgen from './src/Colorgen.js';
2023-03-12 19:01:06 +01:00
program.command('gen <color>')
.description('Generates multiple colors from a given color')
2023-03-13 02:27:47 +01:00
.option('-a, --amount <n>', 'Amount of darken', 12.5)
.option('-c, --complementary', 'Generate complementary colors instead of analogous')
2023-03-12 19:01:06 +01:00
.action((color, options) => {
const amount = options.amount;
2023-03-19 14:28:35 +01:00
const colorgen = new Colorgen(color);
colorgen.amount = amount;
2023-03-13 02:27:47 +01:00
if (options.complementary) {
2023-03-19 14:28:35 +01:00
complementary(colorgen.complementary())
2023-03-13 02:27:47 +01:00
} else {
2023-03-19 14:28:35 +01:00
analogous(colorgen.analogous());
2023-03-13 02:27:47 +01:00
}
2023-03-12 19:01:06 +01:00
});
program.parse(process.argv);
2023-03-19 14:28:35 +01:00
function _logColor(color, name) {
2023-03-12 19:01:06 +01:00
const { r, g, b } = color.toRgb()
2023-03-13 02:27:47 +01:00
console.log(`\x1b[48;2;${r};${g};${b}m ${color.toHexString()} \x1b[0m [${name.toUpperCase()}]`);
}
2023-03-19 14:28:35 +01:00
function _logColors(color, type = 'primary') {
_logColor(color.normal, type);
_logColor(color.dark, `dark ${type}`);
_logColor(color.darker, `dark ${type}`);
}
2023-03-13 02:27:47 +01:00
2023-03-19 14:28:35 +01:00
function analogous(colors) {
_logPrimary(colors.primary);
2023-03-13 02:27:47 +01:00
2023-03-19 14:28:35 +01:00
_logSecondary(colors.secondaries[0]);
_logSecondary(colors.secondaries[1]);
_logSecondary(colors.secondaries[2]);
_logSecondary(colors.secondaries[3]);
2023-03-13 02:27:47 +01:00
}
2023-03-19 14:28:35 +01:00
function complementary(colors) {
_logPrimary(colors.primary);
2023-03-13 02:27:47 +01:00
2023-03-19 14:28:35 +01:00
_logSecondary(colors.secondaries[0]);
2023-03-13 02:27:47 +01:00
}
2023-03-19 14:28:35 +01:00
function _logPrimary(color) {
_logColors(color);
_separator();
2023-03-13 02:27:47 +01:00
}
2023-03-19 14:28:35 +01:00
function _logSecondary(color) {
_logColors(color, 'secondary');
_separator();
2023-03-13 02:27:47 +01:00
}
2023-03-19 14:28:35 +01:00
function _separator() {
2023-03-13 02:27:47 +01:00
console.log('------------------------------');
2023-03-12 19:01:06 +01:00
}