Repl-Rainbow
Hop aboard the rainbow. Have some colors. I found all existing libs out there either not adequate for my needs and usually lacking most of the tools I wanted.
Visual Introduction
Basic Usage
The api is designed to be very easy to use. The base exports is the Rainbow function which attempts to determine from given inputs what type of color you're providing. All colors are translated to xterm-256 colors internally first. The returned value is a function that can then be called on text to escape it. In this way the Ansi function objects act as both storage for settings and as text transformers.
var R = require('repl-rainbow');
var red = R('#f00').fg('#fff').under().pad();
console.log(red('Check me out'));
Utilities
- Rainbow.gradient(colors, lengthPer): Generate multiple interpolated gradients for all colors in the provided array. Length per is how many items each gradient should have, defaulting to 15.
- Rainbow.spectrum(): Preset gradient generator that will produce the spectrum, no configuration needed!
- Rainbow.random(text): Ansi escapes the given text with a random color.
Detailed Usage
-
closest(n): return the n
closest colors using HSL to measure.
-
gradient(c,n): returns an array of n
length with colors interpolating to c
using HSL to measure.
-
basic(bg): convert to the nearest of the basic 16 colors for downgrading. Set bg
to true to get the bg escape
-
style(): Set multiple other styles at once, like italic, etc.
-
child(): Return a new function that inherits from this one so it can be customized
-
fg(): Specify another color as the foreground for this. Causes this color to become the bg.
-
bg(): Specify another color as the background, making this the foreground. /* bg and fg are mutually exclusive */
-
ital(): Toggles this italic
property.
-
inv(): Toggles this inverse
property.
-
under(): Toggles this underline
property.
-
pad(n): Pads text when escaped to n
. If no n
is provided then it will cycle from 0 to 4 and back eac .pad().
-
rgb(): return an rgb array for the color
-
hsl(): return an hsl array for the color
-
ansi(): return the ansi code for the color
-
hex(): return the HTML hex code for the color
Arrays
The return type for operations that return sets is ColorSet
. A ColorSet is just an array with a bit of extra functionality. All the Array functions that return new arrays are wrapped so that you always get back a ColorSet. This allows you to do multiple filters and mappings and still have a ColorSet.
The following properties do the same as the single item one, except to every item in the set.
hsl, rgb, ansi, fg, bg, ital, inv, under, pad
The following are wrapped version of the Array.prototype function, but always returning a ColorSet instead of an Array
map, sort, concat, slice
Custom API
- unique(): Since there can be multiple instances for any given color, this function will return a unique set based on ansi escape number.
- chunk(size): Create a chunked array where the properties of the input array are split based on the given chunk size. Returns a ColorSet containing multiple ColorSets.
- flatten(): Flatten a multi-dimensional set of ColorSets down a single ColorSet.