From 79c6bdcfd804a00cf61a0313364b4ed633045486 Mon Sep 17 00:00:00 2001 From: Joshua Appelman Date: Mon, 14 Jul 2014 00:23:49 +0200 Subject: [PATCH] Adds 256 color support. --- index.js | 9 +++++++++ package.json | 1 + test.js | 11 +++++++++++ 3 files changed, 21 insertions(+) diff --git a/index.js b/index.js index ac1f168..0b0e5b6 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ 'use strict'; var escapeStringRegexp = require('escape-string-regexp'); +var ansi8bit = require('ansi-8-bit'); var ansiStyles = require('ansi-styles'); var stripAnsi = require('strip-ansi'); var hasAnsi = require('has-ansi'); @@ -84,6 +85,14 @@ function init() { defineProps(chalk, init()); +chalk.foreground = function (r, g, b, str) { + return ansi8bit.fg.getRgb(r, g, b) + str + ansi8bit.reset; +}; + +chalk.background = function (r, g, b, str) { + return ansi8bit.bg.getRgb(r, g, b) + str + ansi8bit.reset; +}; + chalk.styles = ansiStyles; chalk.hasColor = hasAnsi; chalk.stripColor = stripAnsi; diff --git a/package.json b/package.json index d77624a..4e268b5 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "text" ], "dependencies": { + "ansi-8-bit": "^1.0.1", "ansi-styles": "^1.1.0", "escape-string-regexp": "^1.0.1", "has-ansi": "^0.1.0", diff --git a/test.js b/test.js index 3e073a6..29399cf 100644 --- a/test.js +++ b/test.js @@ -86,3 +86,14 @@ describe('chalk.stripColor()', function () { assert.equal(chalk.stripColor(chalk.underline.red.bgGreen('foo')), 'foo'); }); }); + +describe('chalk.foreground() and chalk.background()', function () { + it('should style according to their given rgb values', function () { + assert.equal(chalk.foreground(2, 5, 3, 'foobar'), '\u001b[;38;5;121mfoobar\u001b[0m'); + }); + it('should play nice with other chalk styles', function () { + assert.equal(chalk.background(1, 3, 3, chalk.red("Unicorns, attack!")), '\u001b[;48;5;73m\u001b[31mUnicorns, attack!\u001b[39m\u001b[0m'); + }); +}); + +