简体   繁体   English

如何为 node.js 控制台获取更多颜色

[英]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.这个答案中,用户详细描述了在使用 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.不幸的是,这仅向我们展示了如何为文本使用 8 种颜色,并为背景使用相同的 8 种颜色。 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.实际上,由于任何文本在相同的背景颜色上都是不可见的,这意味着我们只能使用 7 种颜色,除非我们愿意经常更改背景。

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.当然,理想的情况是直接提供 RGB 的某种方式,因此我也可以制作自己的颜色阴影,但我会接受任何提供至少 4 种以上颜色的答案,因为我需要 11-12至少对于我正在做的事情。

How can I get more colors for the console in Node.Js?如何在 Node.Js 中为控制台获取更多颜色?

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:首先,确保为粉笔启用 Truecolor,以便您可以使用所有要使用的颜色:

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:之后,您可以使用 CSS 中的扩展颜色,例如橙色、紫色、粉红色和棕色:

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:在也支持 Truecolor 的控制台中运行它,结果如下:

在此处输入图片说明

You can also specify an RGB string with the rgb() function:您还可以使用rgb()函数指定 RGB 字符串:

console.log(ctx.rgb(255, 136, 0)('Orange!'))

这些是标准的 Linux 终端颜色,有关更多信息,您可以在这里查看https://bixense.com/clicolors/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM