- Notifications
You must be signed in to change notification settings - Fork21
🌈 Beautiful color gradients in terminal output
License
bokub/gradient-string
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Beautiful color gradients in terminal output
$ npm i gradient-string
importgradientfrom'gradient-string';console.log(gradient(['cyan','pink'])('Hello world!'));
// Provide an array of colorsconstcoolGradient=gradient(['#FF0000','#00FF00','#0000FF']);
The colors are parsed with TinyColor,multiple formats are accepted.
constcoolGradient=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]);
constcoolString=coolGradient('This is a fancy string!');console.log(coolString);
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! 🌈'));
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 themultiline()
method of a gradient to ensure that the colors are vertically aligned.
importgradient,{rainbow}from'gradient-string';// Use the same gradient on every lineconstduck=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');
There are also more advanced options for gradient customization, such as custom color stops, or choice of color interpolation
By default, the gradient color stops are distributed equidistantly.
You can specify the position of each color stop (between0
and1
), using the following syntax:
letcoolGradient=gradient([{color:'#d8e0de',pos:0},{color:'#255B53',pos:0.8},{color:'#000000',pos:1},]);
When creating a gradient, you can provide a second parameter to choose how the colors will be generated.
Here is the fullgradient
API:
Type:Array<Color>
Colors of the gradient.Multiple formats are accepted.
Type:String
String you want to color.
Type:Object
(optional)
Type:string
The gradient can be generated using RGB or HSV interpolation. HSV usually produces brighter colors.interpolation
can be set torgb
for RGB interpolation, orhsv
for HSV interpolation.
Defaults torgb
. Case-insensitive
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 eithershort
orlong
, depending on if you want to take the shortest or the longest way between two colors.
Defaults toshort
. Case-insensitive
conststr='■'.repeat(48);// Standard RGB gradientconststandardRGBGradient=gradient(['red','green']);// Short HSV gradient: red -> yellow -> greenconstshortHSVGradient=gradient(['red','green'],{interpolation:'hsv'});// Long HSV gradient: red -> magenta -> blue -> cyan -> greenconstlongHSVGradient=gradient(['red','green'],{interpolation:'hsv',hsvSpin:'long'});console.log(standardRGBGradient(str));console.log(shortHSVGradient(str));console.log(longHSVGradient(str));
- chalk - Output colored text to terminal
- tinygradient - Generate gradients
- Shopify inShopify CLI
- Turoborepo in@turbo/workspaces and@turbo/codemod
- Tencent inCloudBase Framework
- Microsoft in@lage-run/reporters
- Fireship inthis YouTube video, where he shows how he builtjavascript-millionaire
- Magic UI inMagic UI CLI
- Myself inchalk-animation, the animated version of gradient-string
- Sindre Sorhus inink-gradient, theInk version of gradient-string
- And
more in open-source projects, who downloaded gradient-stringmore than
times!
MIT ©Boris K
About
🌈 Beautiful color gradients in terminal output