From 2b97d28987a3e0bfe7f4e54703b27ff96ff8fa74 Mon Sep 17 00:00:00 2001 From: Patrick Gunderson Date: Fri, 6 Jun 2014 14:12:17 -0700 Subject: [PATCH 1/2] added support for custom foreground and background colors --- colors.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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; From afc607ee2343fa40b2fa07700d4407289e94eb04 Mon Sep 17 00:00:00 2001 From: Patrick Gunderson Date: Fri, 6 Jun 2014 14:15:44 -0700 Subject: [PATCH 2/2] updated example with custom color --- ReadMe.md | 4 ++++ 1 file changed, 4 insertions(+) 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