Add rgb and hex transformer

This commit is contained in:
Wessel T
2019-06-26 17:58:37 +02:00
parent 97d26d4407
commit 568f37049a
3 changed files with 208 additions and 168 deletions

View File

@@ -14,6 +14,10 @@ Pikmin.bind(pikmin, console);
pikmin.info(Symbol.iterator);
pikmin.info({ 'test': 123 });
pikmin.info(Pikmin.colors.hex([ 255, 255, 255 ]));
pikmin.info(Pikmin.colors.rgb('#FFFFFF'));
pikmin.info(Pikmin.colors.convert([ 0, 0, 0 ]));
pikmin.info(Pikmin.colors.convert('#000000'));
pikmin.error('This is an error!');
require('./global.js')();

26
index.d.ts vendored
View File

@@ -3,8 +3,7 @@ import { WriteStream, PathLike } from "fs";
/**
* The pikmin namespace. Start here!
*/
declare namespace Pikmin
{
declare namespace Pikmin {
/** The loggers collection */
export const loggers: Pikmin.Collection<Pikmin.instance>;
@@ -29,8 +28,7 @@ declare namespace Pikmin
export function unbind(i: Pikmin.instance): void;
/** The instance itself */
export class instance
{
export class instance {
[x: string]: (m: any) => void;
/**
@@ -75,8 +73,7 @@ declare namespace Pikmin
/**
* The console transport that logs anything to the terminal
*/
export class ConsoleTransport implements Transport
{
export class ConsoleTransport implements Transport {
constructor(options: Pikmin.InstanceOptions & { process: NodeJS.Process });
/** The type of the transport, implemented by the Transport interface */
@@ -97,8 +94,7 @@ declare namespace Pikmin
destroy(): this;
}
export class FileTransport implements Transport
{
export class FileTransport implements Transport {
constructor(options: { process: NodeJS.Process, path: string | PathLike, flags?: '-a' });
/** The type of the transport, implemented by the Transport interface */
@@ -119,9 +115,8 @@ declare namespace Pikmin
destroy(): this;
}
/** The colors interface, run by `Pikmin.colors` */
export interface Colors
{
/** The colors interface, run by `colors` */
export interface Colors {
black: Color;
red: Color;
green: Color;
@@ -161,20 +156,21 @@ declare namespace Pikmin
blink: Color;
inverse: Color;
strikethrough: Color;
hex: (hex: string) => string | undefined;
rgb: (val: number[]) => string | undefined;
convert: (value: string | number[], to: 'hex' | 'rgb') => string | number[] | undefined;
strip: (val: string) => string;
supported: boolean;
}
export interface InstanceOptions
{
export interface InstanceOptions {
name?: string;
format?: string;
autogen?: boolean;
transports?: Pikmin.Transport[];
}
export interface Transport
{
export interface Transport {
type: 'CONSOLE' | 'WEBHOOK' | 'FILE';
name: string;
parent: any | undefined;

View File

@@ -1,7 +1,47 @@
const s = require('./styles.js');
const toHex = (number) => {
number = parseInt(number, 10);
if (isNaN(number)) return '00';
number = Math.max(0, Math.min(number, 255));
return `${'0123456789ABCDEF'.charAt((number - number % 16) / 16)}${'0123456789ABCDEF'.charAt((number % 16))}`;
};
const convert = (content = 'FFFFFF', to) => {
if (to === 'hex' || (content instanceof Array && content.length > 2)) return `${toHex(content[0])}${toHex(content[1])}${toHex(content[2])}`;
else if (to === 'rgb' || content.startsWith('#') || (content instanceof String && content.length === 3 || content.length === 6)) {
let rgb = [];
if (content.length < 1) content = '000000';
if (content.charAt(0) === '#') content = content.substring(1, content.length);
if (content.length !== 6 && content.length != 3) return undefined;
if (content.length === 3) {
rgb.push(content.substring(0, 1));
rgb.push(content.substring(1, 2));
rgb.push(content.substring(2, 3));
rgb[0] = rgb[0] + rgb[0];
rgb[1] = rgb[1] + rgb[1];
rgb[2] = rgb[2] + rgb[2];
} else {
rgb.push(content.substring(0, 2));
rgb.push(content.substring(2, 4));
rgb.push(content.substring(4, 6));
}
rgb[0] = parseInt(rgb[2], 16);
rgb[1] = parseInt(rgb[2], 16);
rgb[2] = parseInt(rgb[2], 16);
return rgb;
} else return undefined;
};
for (const key in s.colors) exports[key] = (val) => { return this.supported ? `\x1b[${s.colors[key]}m${val}\x1b[0m` : val; };
for (const key in s.styles) exports[key] = (val) => { return this.supported ? `\x1b[${s.styles[key]}m${val}\x1b[0m` : val; };
exports.strip = (val) => { return (`${val}`).replace(/\x1B\[\d+m/g, ''); };
exports.hex = (hex = 'FFFFFF') => convert(hex, 'hex');
exports.rgb = (rgb = [255, 255, 255]) => convert(rgb, 'hex');
exports.strip = (value) => { return (`${value}`).replace(/\x1B\[\d+m/g, '') };
exports.convert = (content = 'FFFFFF', to) => convert(content, to);
exports.supported = (process.env.FORCE_COLOR || process.platform === 'win32' || (process.stdout.isTTY && process.env.TERM && process.env.TERM !== 'dumb'));