A cored, fixed, documented and optimized version of the popular colors.js. Can be
used as a drop-in replacement, also works correctly in the browser, provides a CSS mode and has been compiled through
Closure Compiler using advanced optimizations. Additionally, nearly every issue and pull request on the original has
been incorporated.
Installation
npm install colour
Usage
This package extends the global String prototype with additional getters that apply terminal colors to your texts.
Available styles are:
- Emphasis: bold, italic, underline, inverse
- Colors: yellow, cyan, white, magenta, green, red, grey, blue
- Sequencers: rainbow, zebra, random
Example
var colour = require('colour');
console.log('hello'.green);
console.log('i like cake and pies'.underline.red)
console.log('inverse the color'.inverse);
console.log('OMG Rainbows!'.rainbow);
Or: As a drop-in replacement for colors
var colors = require('colour');
...
Custom themes
Its also possible to define your own themes by creating new getters on the String object. Example:
var colour = require('colour');
colour.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: ['yellow', 'underline'],
debug: 'blue',
error: 'red bold'
});
console.log("this is an error".error);
console.log("this is a warning".warn);
console.log(colour.green("this is green"));
Console, browser and browser-css mode
var colour = require('colour');
...
colour.mode = 'none';
colour.mode = 'console';
colour.mode = 'browser';
colour.mode = 'browser-css';
Uninstalling / reinstalling on the global scope
If you have a reason to use a fresh String prototype in your application, you may also revert all extensions made.
var colour = require('colour');
...
colour.uninstall();
...
colour.install();
...
More features
- node.js/CommonJS compatible
- RequireJS/AMD compatible
- Browser/shim compatible
- Closure Compiler externs included
- Zero dependencies
Credits
Based on work started by Marak (Marak Squires), cloudhead (Alexis Sellier), mmalecki (Maciej Małecki), nicoreed (Nico
Reed), morganrallen (Morgan Allen), JustinCampbell (Justin Campbell) and ded (Dustin Diaz).
License
The MIT-License (MIT)