ProductPromotion
Logo

Node.JS

made by https://0x3d.site

GitHub - bokub/gradient-string: :rainbow: Beautiful color gradients in terminal output
:rainbow:  Beautiful color gradients in terminal output - bokub/gradient-string
Visit Site

GitHub - bokub/gradient-string: :rainbow:  Beautiful color gradients in terminal output

GitHub - bokub/gradient-string: :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

gradient-string

Install

$ npm i gradient-string

Usage

import gradient from 'gradient-string';

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

Initialize a gradient

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

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

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

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 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

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 line
const duck = gradient(['green', 'yellow']).multiline(`
  __
<(o )___
 ( ._> /
   ---
`);
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:

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<Color> 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
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

Example result

Dependencies

Who uses gradient-string?

License

MIT © Boris K

Articles
to learn more about the nodejs concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about NodeJS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory