Reputation: 1093
In this answer, the users describes in details how to color the text in the console when using node.js. The official documentation is even posted in a comment to the answer.
Unfortunately, this only shows us how to use 8 colors for the text, and the same 8 colors for the background. In practive, since any text will be invisible on the same background color, this means we can only use 7 colors unless we are willing to change the background often.
FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"
What I am looking for, is a way to get more colors for the console. It can be with an external module or library, can be official or not, etc.
Specifically, the colors Orange, Purple, Pink and Brown are very common, and I assume that there is some way to get them.
Of course, the ideal situation would be some way to provide an RGB directly, so I can make my own shades of colors too, but I'll accept any answer that provides access to at least 4 more colors, because I need 11-12 at minimum for something I'm doing.
How can I get more colors for the console in Node.Js?
Upvotes: 4
Views: 5427
Reputation: 880
You can use the tiny and fast ansis lib to use the pre-defined set of ANSI 256 or RGB truecolor in console.
Usage examples:
// import used base styles, colors and functions
import ansis, { red, green, blue, bold, inverse, hex } from 'ansis';
// template string
red`text`;
green`text`;
blue`text`;
// chained
red.bold`text`;
bold.yellowBright`text`;
// nested
red`red ${green`green ${blue.italic`blue italic`} green`} red`;
// foreground color
ansis.ansi(96).bold('bold Bright Cyan');
// background color
ansis.bgAnsi(105)('Bright Magenta');
// truecolor
hex('#FF75D1').bgYellow.bold`text`;
Upvotes: 0
Reputation: 3419
Those are standard Linux terminal colors, For more, you can see here https://bixense.com/clicolors/
Upvotes: 1
Reputation: 10096
You can use chalk for this:
First, make sure that you enable Truecolor for chalk, so that you can use all the colors you want to use:
const chalk = require("chalk"),
ctx = new chalk.constructor({level: 3}); // 3 for Truecolor: https://github.com/chalk/chalk#chalklevel
After that you can use the Extended Colors from CSS, like Orange, Purple, Pink and Brown:
console.log(ctx.keyword('orange')('Orange!'))
console.log(ctx.keyword('purple')('Purple!'))
console.log(ctx.keyword('pink')('Pink!'))
console.log(ctx.keyword('brown')('Brown 💩'))
Running that in a console that also supports Truecolor, results in this:
You can also specify an RGB string with the rgb()
function:
console.log(ctx.rgb(255, 136, 0)('Orange!'))
Upvotes: 2