2

How can I set terminal font color in node js. I found some modules, but there have limited colors, I want to use any color.

For example. Something like this.

console.log("text", "#87a213");

On this question`s answer are 3 modules, but in these modules colors are limited. I need to print with any color.
Thank You.

Community
  • 1
  • 1
Gor
  • 2,808
  • 6
  • 25
  • 46
  • possible duplicate of [How to change node.js's console font color?](http://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color) – Ben Fortune Sep 09 '15 at 08:10
  • @BenFortune I checked this question. It cant solve my problem, because they are using limited colors – Gor Sep 09 '15 at 08:18

3 Answers3

2

You can also use a super simple mixin to add colors support to String prototype :

// Node String Colors Support (https://git.io/colors)
// Usage console.log("Hello!".green())
const _c = require('util').inspect.colors;
Object.keys(_c).forEach(c =>String.prototype[c] = s =>`\x1b[${_c[c][0]}m${s}\x1b[${_c[c][1]}m`);

Alternative with no util dependency: (not recommended)

// Node String Colors Support. (no util version) (https://git.io/colors)
// Usage console.log("Hello!".green())
const _c = {bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]};
Object.keys(_c).forEach(c =>String.prototype[c] = s =>`\x1b[${_c[c][0]}m${s}\x1b[${_c[c][1]}m`); 

Global version, if you don't want to alter String prototype (this is much much safer)

// Node String Colors Support. (global version) (https://git.io/colors)
// Usage console.log(green("Hello world!")
const _c = require('util').inspect.colors;
Object.keys(_c).forEach(c =>global[c] = s =>`\x1b[${_c[c][0]}m${s}\x1b[${_c[c][1]}m`);
Pooya
  • 862
  • 7
  • 10
1

You can use ansi-256-colors package. But it does not support any color. But it supports wide range of colors.

To install:

npm install --save ansi-256-colors

To use:

console.log(colors.fg.getRgb(2,3,4) + colors.bg.getRgb(4,4,4) + 'Hello world!' + colors.reset);
shan1024
  • 1,389
  • 7
  • 17
  • My main goal is to read jpg image, and then print it to terminal , so I need to read each pixel values, and then print a symbol with this color. It is because I need to print any color. Maybe you know a method better? Thnak you – Gor Sep 09 '15 at 08:35
  • Printing a character per each pixel will not be useful, because if the image is 800x600, there will be 800 pixels in a row. Printing 800 characters in a row seem like too much. Instead, try to divide the image into a grid and approximate a color(rgb values) for each cell. Then print a character in that color. Do this for all cells. – shan1024 Sep 09 '15 at 08:50
0

I've been using the package colors https://www.npmjs.com/package/colors which is very easy to use because it extends String, so for every string you can simply do .red

var colors = require('colors');

console.log('hello'.green); // outputs green text 
console.log('i like cake and pies'.underline.red) // outputs red underlined text 
console.log('inverse the color'.inverse); // inverses the color 
console.log('OMG Rainbows!'.rainbow); // rainbow 
console.log('Run the trap'.trap); // Drops the bass 
DevAlien
  • 2,456
  • 15
  • 17