diff --git a/ReadMe.md b/ReadMe.md index 0eda52db..26d04777 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -34,6 +34,10 @@ console.log('hello'.green); // outputs green text console.log('i like cake and pies'.underline.red) // outputs red underlined text console.log('inverse the color'.inverse); // inverses the color console.log('OMG Rainbows!'.rainbow); // rainbow (ignores spaces) + +console.log('It's T-Mobile Magenta!'.color(0xea,0x0a,0x8e)) // supports RGB +console.log('It's T-Mobile Magenta!'.color("#ea0a8e")) // also supports HEX codes + ``` # Creating Custom themes diff --git a/colors.js b/colors.js index 7a537d8d..8ae043f2 100644 --- a/colors.js +++ b/colors.js @@ -51,6 +51,32 @@ var addProperty = function (color, func) { String.prototype.__defineGetter__(color, func); }; +// +// Add custom color support +// Accepts R,G,B or hex color +// +String.prototype.color = function(r,g,b){ + // if g is undefined, assume r is a hex value + if (g === undefined){ + var hex = parseInt(r, 16); + r = hex >> 16; + g = hex >> 8 & 0xff; + b = hex & 0xff; + } + return "\x1B[38;2;"+r+";"+g+";"+b+"m" + this + '\x1B[39m'; +}; + +String.prototype.bgcolor = function(r,g,b){ + // if g is undefined, assume r is a hex value + if (g === undefined){ + var hex = parseInt(r, 16); + r = hex >> 16; + g = hex >> 8 & 0xff; + b = hex & 0xff; + } + return "\x1B[48;2;"+r+";"+g+";"+b+"m" + this + '\x1B[49m'; +}; + function stylize(str, style) { var styles;