• Stars
    star
    1,030
  • Rank 44,742 (Top 0.9 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🌈 Beautiful color gradients in terminal output

gradient-string

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

Beautiful color gradients in terminal output

gradient-string

Install

$ npm i gradient-string

Usage

const gradient = require('gradient-string');

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

Initialize a gradient

// Using varargs
let coolGradient = gradient('red', 'green', 'blue');

// Using array
let coolGradient = gradient(['#FF0000', '#00FF00', '#0000FF']);

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

let 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

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

Built-in gradients

Usage

const gradient = require('gradient-string');

// Use the rainbow gradient
console.log(gradient.rainbow('I love gradient-strings!'))

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.

const gradient = require('gradient-string');

// Use the same gradient on every line
let duck = gradient('orange', 'yellow').multiline([
    "  __",
    "<(o )___",
    " ( ._> /",
    "  `---'",
].join('\n'));
console.log(duck);

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

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

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 using a gradient, you can actually add a second parameter to choose how the colors will be generated.

Here is the full gradient API:

myGradient(text, [options])

text

Type: string
String you want to color.

options

Type: Object

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 insentitive

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 redToGreen = gradient('red', 'green');
const str = '■'.repeat(48);

// Standard RGB gradient
console.log(redToGreen(str)); 

// Short HSV gradient: red -> yellow -> green
console.log(redToGreen(str, {interpolation: 'hsv'}));

// Long HSV gradient: red -> magenta -> blue -> cyan -> green
console.log(redToGreen(str, {interpolation: 'hsv', hsvSpin: 'long'}));
Result

Example result

Typescript

Typescript definitions of gradient-string are available on DefinitelyTyped

npm i @types/gradient-string

Dependencies

License

MIT © Boris K

More Repositories

1

chalk-animation

🎬 Colorful animations in terminal output
JavaScript
2,124
star
2

vanity-eth

💎 Browser-based ETH vanity address generator
Vue
730
star
3

lyo

📦 Node.js to browser - The easy way
JavaScript
621
star
4

rgb-light-card

💡 A Lovelace custom card for RGB lights
JavaScript
414
star
5

nopaste

📋 Client-side paste service
JavaScript
362
star
6

linky

🔌 CLI tool to retrieve Linky smart meters data
TypeScript
216
star
7

ha-linky

📊 Home Assistant add-on for Linky smart meters
TypeScript
187
star
8

home-assistant-extension

📌 Home Assistant browser extension
Vue
169
star
9

git-history-editor

✏️ Online commit editor, intuitive and easy to use
HTML
160
star
10

github-stats-box

📌 Update a gist to contain your GitHub stats
JavaScript
139
star
11

gradient-badge

🍭 Badge generator with color gradient support
JavaScript
70
star
12

i3-config

🐍 A backup of my i3 config
Shell
59
star
13

conso-api

⚡ API for Linky smart meters
Vue
49
star
14

wakatime-to-toggl

📩 Push your WakaTime data to Toggl
JavaScript
35
star
15

linky-cli

⛔️ DEPRECATED - A CLI tool to retrieve your Linky power consumption
JavaScript
19
star
16

stravanity

👟 Easy to beat Strava segments
Vue
17
star
17

ha-history-stats

📈 Statistics about your Home Assistant history
Python
14
star
18

prettier-config

💜 My personal Prettier config & workflow
10
star
19

brackets-gofmt

📑 An extension for Brackets IDE to format Go code.
JavaScript
2
star
20

vanity-eth-cli

Works, but has nothing awesome
Go
1
star
21

blog-comments

1
star
22

bokub

📖 My profile's README
1
star
23

lyo-service

🌄 Lyo builder as a service
JavaScript
1
star