Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

gradient-string

> 编程语言
Open source

:rainbow: Beautiful color gradients in terminal output

1.2K stars0 likes1 views
WebsiteGitHub

About

:rainbow: Beautiful color gradients in terminal output

gradient-string

Build Status Version Codecov Downloads code style: prettier Mentioned in Awesome Node.js

Beautiful color gradients in terminal output

Install

$ npm i gradient-string

Usage

javascript
import gradient from 'gradient-string';

console.log(gradient(['cyan', 'pink'])('Hello world!'));

Initialize a gradient

javascript
// Provide an array of colors
const coolGradient = gradient(['#FF0000', '#00FF00', '#0000FF']);

The colors are parsed with TinyColor, multiple formats are accepted.

javascript
const coolGradient = gradient([
  tinycolor('#FFBB65'), // tinycolor object
  { r: 0, g: 255, b: 0 }, // RGB object
  { h: 240, s: 1, v: 1, a: 1 }, // HSVa object
  'rgb(120, 120, 0)', // RGB CSS string
  'gold', // named color
]);

Use a gradient

javascript
const coolString = coolGradient('This is a fancy string!');
console.log(coolString);

Built-in gradients

Usage

javascript
import { rainbow, pastel } from 'gradient-string';

// Use the pastel built-in gradient
console.log(pastel('I love gradient-string!'));

// Use the rainbow built-in gradient
console.log(rainbow('It is so pretty! '));

Available built-in gradients

Multi line gradients

In some cases, you may want to apply the same horizontal gradient on each line of a long text (or a piece of ASCII art).

You can use the multiline() method of a gradient to ensure that the colors are vertically aligned.

javascript
import gradient, { rainbow } from 'gradient-string';

// Use the same gradient on every line
const duck = gradient(['green', 'yellow']).multiline(`
  __
 /
   ---
`);
console.log(duck);

// Works with aliases
rainbow.multiline('Multi line\nstring');

// Works with advanced options (read below)
gradient(['cyan', 'pink'], { interpolation: 'hsv' }).multiline('Multi line\nstring');

Advanced gradients

There are also more advanced options for gradient customization, such as custom color stops, or choice of color interpolation

Custom color stops

By default, the gradient color stops are distributed equidistantly.

You can specify the position of each color stop (between 0 and 1), using the following syntax:

javascript
let coolGradient = gradient([
  { color: '#d8e0de', pos: 0 },
  { color: '#255B53', pos: 0.8 },
  { color: '#000000', pos: 1 },
]);

Color interpolation

When creating a gradient, you can provide a second parameter to choose how the colors will be generated.

Here is the full gradient API:

gradient([colors], options?)(text)

colors

Type: Array

Colors of the gradient. Multiple formats are accepted.

text

Type: String

String you want to color.

options

Type: Object (optional)

interpolation

Type: string

The gradient can be generated using RGB or HSV interpolation. HSV usually produces brighter colors. interpolation can be set to rgb for RGB interpolation, orhsv for HSV interpolation.

Defaults to rgb. Case-insensitive

hsvSpin

Type: string

Used only in the case of HSV interpolation.

Because hue can be considered as a circle, there are two ways to go from a color to another color.

hsvSpin can be either short or long, depending on if you want to take the shortest or the longest way between two colors.

Defaults to short. Case-insensitive

Example

Code
javascript
const str = '■'.repeat(48);

// Standard RGB gradient
const standardRGBGradient = gradient(['red', 'green']);

// Short HSV gradient: red -> yellow -> green
const shortHSVGradient = gradient(['red', 'green'], { interpolation: 'hsv' });

// Long HSV gradient: red -> magenta -> blue -> cyan -> green
const longHSVGradient = gradient(['red', 'green'], { interpolation: 'hsv', hsvSpin: 'long' });

console.log(standardRGBGradient(str));
console.log(shortHSVGradient(str));
console.log(longHSVGradient(str));
Result

Dependencies

  • chalk - Output colored text to terminal
  • tinygradient - Generate gradients

Who uses gradient-string?

  • Shopify in Shopify CLI
  • Turoborepo in @turbo/workspaces and @turbo/codemod
  • Tencent in CloudBase Framework
  • Microsoft in @lage-run/reporters
  • Fireship in this YouTube video, where he shows how he built javascript-millionaire
  • Magic UI in Magic UI CLI
  • Myself in chalk-animation, the animated version of gradient-string
  • Sindre Sorhus in ink-gradient, the Ink version of gradient-string
  • And more in open-source projects, who downloaded gradient-string more than times!

License

MIT © Boris K

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptcolorconsolegradientnodejs

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言