Moving logic to a file.

This commit is contained in:
Jorge Bolois 2023-03-19 14:28:35 +01:00
parent bf9053d45f
commit 524a96f1eb
3 changed files with 75 additions and 40 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env node
import { program } from 'commander';
import tinycolor from 'tinycolor2';
import Colorgen from './src/Colorgen.js';
program.command('gen <color>')
.description('Generates multiple colors from a given color')
@ -8,60 +8,52 @@ program.command('gen <color>')
.option('-c, --complementary', 'Generate complementary colors instead of analogous')
.action((color, options) => {
const amount = options.amount;
const primary = tinycolor(color);
const colorgen = new Colorgen(color);
colorgen.amount = amount;
if (options.complementary) {
complementary(primary, amount)
complementary(colorgen.complementary())
} else {
analogous(primary, amount);
analogous(colorgen.analogous());
}
});
program.parse(process.argv);
function logColor(color, name) {
function _logColor(color, name) {
const { r, g, b } = color.toRgb()
console.log(`\x1b[48;2;${r};${g};${b}m ${color.toHexString()} \x1b[0m [${name.toUpperCase()}]`);
}
function analogous(primary, amount) {
_primary(primary, amount);
_separator();
const analogous = primary.analogous(12, 90);
_secondary(analogous[2], amount);
_separator();
_secondary(analogous[5], amount);
_separator();
_secondary(analogous[8], amount);
_separator();
_secondary(analogous[11], amount);
function _logColors(color, type = 'primary') {
_logColor(color.normal, type);
_logColor(color.dark, `dark ${type}`);
_logColor(color.darker, `dark ${type}`);
}
function complementary(primary, amount) {
_primary(primary, amount);
function analogous(colors) {
_logPrimary(colors.primary);
_logSecondary(colors.secondaries[0]);
_logSecondary(colors.secondaries[1]);
_logSecondary(colors.secondaries[2]);
_logSecondary(colors.secondaries[3]);
}
function complementary(colors) {
_logPrimary(colors.primary);
_logSecondary(colors.secondaries[0]);
}
function _logPrimary(color) {
_logColors(color);
_separator();
const secondary = primary.complement();
_secondary(secondary, amount);
}
function _primary(color, amount) {
logColor(color, 'primary');
const darkPrimary = color.clone().darken(amount);
logColor(darkPrimary, 'dark primary');
const darkerPrimary = darkPrimary.clone().darken(amount);
logColor(darkerPrimary, 'darker primary');
}
function _secondary(color, amount) {
logColor(color, 'secondary');
const darkSecondary = color.clone().darken(amount);
logColor(darkSecondary, 'dark secondary');
const darkerSecondary = darkSecondary.clone().darken(amount);
logColor(darkerSecondary, 'darker secondary');
function _logSecondary(color) {
_logColors(color, 'secondary');
_separator();
}
function _separator() {

View File

@ -1,7 +1,7 @@
{
"name": "@midefos/colorgen",
"type": "module",
"version": "0.1.1",
"version": "0.1.2",
"description": "Color generator",
"bin": {
"colorgen": "./colorgen.js"

43
src/Colorgen.js Normal file
View File

@ -0,0 +1,43 @@
import tinycolor from "tinycolor2";
export default class Colorgen {
constructor(primary) {
this.primary = tinycolor(primary);
this.amount = 12.5;
}
analogous() {
const primary = this._generateColor(this.primary);
const secondaries = [];
const analogous = this.primary.analogous(12, 90);
secondaries.push(this._generateColor(analogous[2]));
secondaries.push(this._generateColor(analogous[5]));
secondaries.push(this._generateColor(analogous[8]));
secondaries.push(this._generateColor(analogous[11]));
return { primary, secondaries };
}
complementary() {
const primary = this._generateColor(this.primary);
const secondaries = [];
const complementary = this.primary.complement();
secondaries.push(this._generateColor(complementary));
return { primary, secondaries };
}
_generateDark(color) {
return color.clone().darken(this.amount);
}
_generateColor(normal) {
const dark = this._generateDark(normal);
const darker = this._generateDark(dark);
return { normal, dark, darker };
}
}