console-palette
v1.0.1
Published
A light weight versatile Node.js library for customizing the style and color of the console output.
Downloads
133
Maintainers
Readme
Console Palette
Console Palette is a lightweight Node.js library that simplifies working with ANSI color codes in terminal output. Customize your terminal text with colors, backgrounds, and styles using easy-to-use utility functions.
Features
- Predefined ANSI color and style methods for foreground and background. These can be passed as a string in the
custom
method — See section Available ANSI Codes below. - Custom color conversion from HEX and RGB values to ANSI.
- Chaining and combining styles for complex output customization.
Installation
npm install console-palette
Usage
Import the library
const cp = require('./console-palette.js'); // for Common JS
// or
import cp from './console-palette.js'; // for ES6
Basic Provided Color Usage
console.log(cp.red('This text is red!'));
console.log(cp.bgBlue('This text has a blue background!'));
console.log(cp.brightYellow('This is bright yellow!'));
Applying Styles
let bold = cp.bold('Bold Text');
let italic = cp.italic('Italic and');
let underlined = cp.underline('Underlined Text');
console.log(bold + " " + italic + " " + underlined);
// **Bold** *Italic* and Underlined Text
Using the custom
method for colors and style
You can also use custom HEX or RGB colors for more flexibility.
Provided color strings (see list below)
console.log(cp.custom('hello world', { color: 'brightYellow' }));
HEX color codes
console.log(cp.custom('Custom HEX color', { color: '#FF5733' }));
RGB string or object inputs
console.log(cp.custom('This text should be red', { color: { r: 255, g: 0, b: 0 } }));
console.log(cp.custom('This text should be red', { color: 'rgb(255, 0, 0)' }));
Custom Background Colors
console.log(cp.custom('Custom Background Color using hex', { background: '#34c3ff' }));
console.log(cp.custom('Custom Background Color RGB', { background: 'rgb(255, 0, 0)' }));
console.log(cp.custom('Custom Background with provided colors', { background: 'pink' }));
Custom styles
Separate multiple styles with the custom method by commas.
console.log(cp.custom('Hello World', {
style: 'bold,underline,invert'
}));
Combining Styles, Colors, and Backgrounds
You can combine styles, foreground colors, and backgrounds for advanced formatting:
console.log(cp.custom('Styled Text', {
color: 'green',
background: 'pink',
style: 'bold,underline'
}));
Chain Styles and Colors
You can combine multiple styles, colors, and backgrounds by nesting them or chaining directly through the custom
function.
Chaining with Predefined Colors and Styles
console.log(cp.red(cp.bold('This text is bold and red!')));
console.log(cp.bgBlue(cp.underline(cp.white('This text is underlined and has a blue background!'))));
Utility Functions
hexToAnsi(hex)
: Converts HEX color to ANSI code for foreground.rgbToAnsi(rgb)
: Converts an RGB object to ANSI code for foreground.rgbStringToAnsi(rgbString)
: Converts an RGB string to ANSI code for foreground.hexToAnsiBackground(hex)
: Converts HEX color to ANSI background.rgbToAnsiBackground(rgb)
: Converts RGB object to ANSI background.rgbStringToAnsiBackground(rgbString)
: Converts RGB string to ANSI background.
Available ANSI Codes
- Foreground Colors:
black
,red
,green
,yellow
,blue
,magenta
,cyan
,white
, and bright variants (brightRed
,brightGreen
, etc.). - Background Colors:
bgBlack
,bgRed
,bgGreen
,bgYellow
,bgBlue
,bgMagenta
,bgCyan
,bgWhite
, and bright variants. - Styles:
bold
,underline
,italic
,strikethrough
,inverse
.