const coolString = coolGradient('This is a fancy string!');console.log(coolString);
Built-in gradients
Usage
import { rainbow, pastel } from 'gradient-string';// Use the pastel built-in gradientconsole.log(pastel('I love gradient-string!'));// Use the rainbow built-in gradientconsole.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.
import gradient, { rainbow } from 'gradient-string';// Use the same gradient on every lineconst duck = gradient(['green', 'yellow']).multiline(` __<(o )___ ( ._> / ---`);console.log(duck);// Works with aliasesrainbow.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:
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
const str = '■'.repeat(48);// Standard RGB gradientconst standardRGBGradient = gradient(['red', 'green']);// Short HSV gradient: red -> yellow -> greenconst shortHSVGradient = gradient(['red', 'green'], { interpolation: 'hsv' });// Long HSV gradient: red -> magenta -> blue -> cyan -> greenconst longHSVGradient = gradient(['red', 'green'], { interpolation: 'hsv', hsvSpin: 'long' });console.log(standardRGBGradient(str));console.log(shortHSVGradient(str));