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.
- 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.
npm install console-palette
const cp = require('./console-palette.js'); // for Common JS
// or
import cp from './console-palette.js'; // for ES6
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!'));
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
You can also use custom HEX or RGB colors for more flexibility.
console.log(cp.custom('hello world', { color: 'brightYellow' }));
console.log(cp.custom('Custom HEX color', { color: '#FF5733' }));
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)' }));
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' }));
Separate multiple styles with the custom method by commas.
console.log(cp.custom('Hello World', {
style: 'bold,underline,invert'
}));
You can combine styles, foreground colors, and backgrounds for advanced formatting:
console.log(cp.custom('Styled Text', {
color: 'green',
background: 'pink',
style: 'bold,underline'
}));
You can combine multiple styles, colors, and backgrounds by nesting them or chaining directly through the custom
function.
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!'))));
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.
- 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
.