如何在终端打印出有颜色的字

8-bit color

syntax:

ESC[ 38;5;⟨n⟩ m Select foreground color
ESC[ 48;5;⟨n⟩ m Select background color
  • ESCnon-printable control character escape,具体的值是 \x1b

e.g.(Nodejs)

const bg = 255;
const text = 'hello world';

// setting background
console.log(`\x1b[48;5;${i}m${text}`);

// reset setting
console.log(`\x1b[0m`);

// setting background and foreground
console.log(`\x1b[48;5;255m\x1b[38;5;222m${text}`);

打印所有 8-bit 颜色:

24-bit color

e.g.

Some color references

A simple chalk tool

more info

Last updated

Was this helpful?