Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get more colors for node.js console

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?

like image 420
Kaito Kid Avatar asked Oct 29 '25 17:10

Kaito Kid


2 Answers

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:

enter image description here

You can also specify an RGB string with the rgb() function:

console.log(ctx.rgb(255, 136, 0)('Orange!'))
like image 152
Luca Kiebel Avatar answered Oct 31 '25 08:10

Luca Kiebel


Those are standard Linux terminal colors, For more, you can see here https://bixense.com/clicolors/

like image 24
Robin He Avatar answered Oct 31 '25 08:10

Robin He



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!