4

How do you set colors using either rgb or hex representation using the very popular colors module? https://npmjs.org/package/colors

I read ove documentation and don't see an option, however, it seems like such a popular module would give you this control, no?

var colors = require('colors');

colors.setTheme({
  my_green: '#5A9664', // doesn't work
  output: 'green',
});

    console.log("A string that wants to be a color.".my_green); 

update, putting one of my comments in question so it's not burried: The colors module supports Browser mode, which clearly opens up the door for #hex colors within the html tags. The setTheme doesn't support an ANSI input array, and only takes the first half. (The module would have to be smarter to wrap the string in ansi.)

Thanks.

4

3 回答 3

3

控制台的颜色格式不能接受这些 HEX 颜色值:#XXXXXX您需要检查这些文件: https ://github.com/Marak/colors.js/blob/master/colors.js并传递一个ANSI 颜色格式值

正如开发人员所描述的:

'bold'      : ['\x1B[1m',  '\x1B[22m'],
'italic'    : ['\x1B[3m',  '\x1B[23m'],
'underline' : ['\x1B[4m',  '\x1B[24m'],
'inverse'   : ['\x1B[7m',  '\x1B[27m'],
'strikethrough' : ['\x1B[9m',  '\x1B[29m'],
//grayscale
'white'     : ['\x1B[37m', '\x1B[39m'],
'grey'      : ['\x1B[90m', '\x1B[39m'],
'black'     : ['\x1B[30m', '\x1B[39m'],
//colors
'blue'      : ['\x1B[34m', '\x1B[39m'],
'cyan'      : ['\x1B[36m', '\x1B[39m'],
'green'     : ['\x1B[32m', '\x1B[39m'],
'magenta'   : ['\x1B[35m', '\x1B[39m'],
'red'       : ['\x1B[31m', '\x1B[39m'],
'yellow'    : ['\x1B[33m', '\x1B[39m']
于 2013-05-08T16:51:02.943 回答
1

您只能覆盖现有的主题属性并将预定义的颜色设置为特定的键

从自述文件:

colors.setTheme({
  silly: 'rainbow',
  input: 'grey',
  verbose: 'cyan',
  prompt: 'grey',
  info: 'green',
  data: 'grey',
  help: 'cyan',
  warn: 'yellow',
  debug: 'blue',
  error: 'red'
});

例如,您可以设置promptgreen但不能设置prompt为自定义十六进制颜色

于 2013-05-08T16:42:22.193 回答
1

是的,您可以在控制台中使用十六进制颜色!这个例子是在 OSX 上的 Node 中完成的。

let hex = 'AA4296'; // magenta-ish

let red = parseInt(hex.substr(0, 2), 16);
let green = parseInt(hex.substr(2, 2), 16);
let blue = parseInt(hex.substr(4,2), 16);

let fgColorString = `\x1b[38;2;${red};${green};${blue}m`;
let bgColorString = `\x1b[48;2;${red};${green};${blue}m`;
let resetFormatString = `\x1b[0m`;

console.log(`${fgColorString}Magenta Foreground`);
console.log(`${resetFormatString}Back to Normal Colors`);
console.log(`${bgColorString}Magenta Background`);
console.log(`${resetFormatString}Back to Normal Again`);
于 2019-12-18T15:58:56.387 回答