Comparing version 0.4.3 to 0.5.0
@@ -17,14 +17,19 @@ var shim = require('./shim'); | ||
var colorPalette = { | ||
'#000000': 'black', | ||
'#ff0000': 'red', | ||
'#00ff00': 'green', | ||
'#ffff00': 'yellow', | ||
'#0000ff': 'blue', | ||
'#ff00ff': 'magenta', | ||
'#00ffff': 'cyan', | ||
'#ffffff': 'white', | ||
'#808080': 'gray' | ||
var colorPaletteByNumberOfColors = { | ||
16: { | ||
'#000000': 'black', | ||
'#ff0000': 'red', | ||
'#00ff00': 'green', | ||
'#ffff00': 'yellow', | ||
'#0000ff': 'blue', | ||
'#ff00ff': 'magenta', | ||
'#00ffff': 'cyan', | ||
'#ffffff': 'white', | ||
'#808080': 'gray' | ||
}, | ||
256: {} | ||
}; | ||
var diffPaletteByNumberOfColors = {}; | ||
function convertColorToObject(color) { | ||
@@ -55,4 +60,36 @@ if (color.length < 6) { | ||
var colorDiffPalette = shim.map(getKeys(colorPalette), convertColorToObject); | ||
diffPaletteByNumberOfColors[16] = shim.map(getKeys(colorPaletteByNumberOfColors[16]), convertColorToObject); | ||
diffPaletteByNumberOfColors[256] = [].concat(diffPaletteByNumberOfColors[16]); | ||
var nextAnsiColorNumber = 16; | ||
function registerNext256PaletteEntry(obj) { | ||
diffPaletteByNumberOfColors[256].push(obj); | ||
colorPaletteByNumberOfColors[256][toHexColor(obj)] = nextAnsiColorNumber; | ||
nextAnsiColorNumber += 1; | ||
} | ||
for (var r = 0 ; r < 6 ; r += 1) { | ||
for (var g = 0 ; g < 6 ; g += 1) { | ||
for (var b = 0 ; b < 6 ; b += 1) { | ||
registerNext256PaletteEntry({ | ||
R: Math.round(r * 256/6), | ||
G: Math.round(g * 256/6), | ||
B: Math.round(b * 256/6) | ||
}); | ||
} | ||
} | ||
} | ||
forEach([ | ||
0x08, 0x12, 0x1c, 0x26, 0x30, 0x3a, 0x44, 0x4e, 0x58, 0x60, 0x66, 0x76, | ||
0x80, 0x8a, 0x94, 0x9e, 0xa8, 0xb2, 0xbc, 0xc6, 0xd0, 0xda, 0xe4, 0xee | ||
], function (value) { | ||
registerNext256PaletteEntry({R: value, G: value, B: value}); | ||
}); | ||
function findClosestColorInDiffPalette(color, numberOfColors) { | ||
var colorObject = colorDiff.closest(convertColorToObject(color), diffPaletteByNumberOfColors[numberOfColors]); | ||
return colorPaletteByNumberOfColors[numberOfColors][toHexColor(colorObject)]; | ||
} | ||
var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i; | ||
@@ -64,18 +101,30 @@ AnsiSerializer.prototype.text = function (content) { | ||
forEach(styles, function (styleName) { | ||
if (!ansiStyles[styleName] && rgbRegexp.test(styleName)) { | ||
if (ansiStyles[styleName]) { | ||
content = ansiStyles[styleName].open + content + ansiStyles[styleName].close; | ||
} else if (rgbRegexp.test(styleName)) { | ||
var originalStyleName = styleName; | ||
var backgroundColor = styleName.substring(0, 2) === 'bg'; | ||
var color = backgroundColor ? styleName.substring(2) : styleName; | ||
var colorObject = colorDiff.closest(convertColorToObject(color), colorDiffPalette); | ||
var closestColor = colorPalette[toHexColor(colorObject)]; | ||
if (backgroundColor) { | ||
styleName = 'bg' + firstUp(closestColor); | ||
var isBackgroundColor = styleName.substring(0, 2) === 'bg'; | ||
var colorName = isBackgroundColor ? styleName.substring(2) : styleName; | ||
var color16Hex = toHexColor(colorDiff.closest(convertColorToObject(colorName), diffPaletteByNumberOfColors[16])); | ||
var closestColor16 = colorPaletteByNumberOfColors[16][color16Hex]; | ||
var color256Hex = toHexColor(colorDiff.closest(convertColorToObject(colorName), diffPaletteByNumberOfColors[256])); | ||
var closest256ColorIndex = colorPaletteByNumberOfColors[256][color256Hex]; | ||
var closestColor16 = findClosestColorInDiffPalette(colorName, 16); | ||
var closest256ColorIndex = findClosestColorInDiffPalette(isBackgroundColor ? styleName.substring(2) : styleName, 256); | ||
if (isBackgroundColor) { | ||
styleName = 'bg' + firstUp(closestColor16); | ||
} else { | ||
styleName = closestColor; | ||
styleName = closestColor16; | ||
} | ||
ansiStyles[originalStyleName] = ansiStyles[styleName]; | ||
var open = ansiStyles[styleName].open; | ||
var close = ansiStyles[styleName].close; | ||
if (color16Hex !== color256Hex) { | ||
open += '\x1b[' + (isBackgroundColor ? 48 : 38) + ';5;' + closest256ColorIndex + 'm'; | ||
} | ||
ansiStyles[originalStyleName] = {open: open, close: close}; | ||
content = open + content + close; | ||
} | ||
if (ansiStyles[styleName]) { | ||
content = ansiStyles[styleName].open + content + ansiStyles[styleName].close; | ||
} | ||
}); | ||
@@ -82,0 +131,0 @@ } |
@@ -41,14 +41,19 @@ /*! | ||
var colorPalette = { | ||
'#000000': 'black', | ||
'#ff0000': 'red', | ||
'#00ff00': 'green', | ||
'#ffff00': 'yellow', | ||
'#0000ff': 'blue', | ||
'#ff00ff': 'magenta', | ||
'#00ffff': 'cyan', | ||
'#ffffff': 'white', | ||
'#808080': 'gray' | ||
var colorPaletteByNumberOfColors = { | ||
16: { | ||
'#000000': 'black', | ||
'#ff0000': 'red', | ||
'#00ff00': 'green', | ||
'#ffff00': 'yellow', | ||
'#0000ff': 'blue', | ||
'#ff00ff': 'magenta', | ||
'#00ffff': 'cyan', | ||
'#ffffff': 'white', | ||
'#808080': 'gray' | ||
}, | ||
256: {} | ||
}; | ||
var diffPaletteByNumberOfColors = {}; | ||
function convertColorToObject(color) { | ||
@@ -79,4 +84,36 @@ if (color.length < 6) { | ||
var colorDiffPalette = shim.map(getKeys(colorPalette), convertColorToObject); | ||
diffPaletteByNumberOfColors[16] = shim.map(getKeys(colorPaletteByNumberOfColors[16]), convertColorToObject); | ||
diffPaletteByNumberOfColors[256] = [].concat(diffPaletteByNumberOfColors[16]); | ||
var nextAnsiColorNumber = 16; | ||
function registerNext256PaletteEntry(obj) { | ||
diffPaletteByNumberOfColors[256].push(obj); | ||
colorPaletteByNumberOfColors[256][toHexColor(obj)] = nextAnsiColorNumber; | ||
nextAnsiColorNumber += 1; | ||
} | ||
for (var r = 0 ; r < 6 ; r += 1) { | ||
for (var g = 0 ; g < 6 ; g += 1) { | ||
for (var b = 0 ; b < 6 ; b += 1) { | ||
registerNext256PaletteEntry({ | ||
R: Math.round(r * 256/6), | ||
G: Math.round(g * 256/6), | ||
B: Math.round(b * 256/6) | ||
}); | ||
} | ||
} | ||
} | ||
forEach([ | ||
0x08, 0x12, 0x1c, 0x26, 0x30, 0x3a, 0x44, 0x4e, 0x58, 0x60, 0x66, 0x76, | ||
0x80, 0x8a, 0x94, 0x9e, 0xa8, 0xb2, 0xbc, 0xc6, 0xd0, 0xda, 0xe4, 0xee | ||
], function (value) { | ||
registerNext256PaletteEntry({R: value, G: value, B: value}); | ||
}); | ||
function findClosestColorInDiffPalette(color, numberOfColors) { | ||
var colorObject = colorDiff.closest(convertColorToObject(color), diffPaletteByNumberOfColors[numberOfColors]); | ||
return colorPaletteByNumberOfColors[numberOfColors][toHexColor(colorObject)]; | ||
} | ||
var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i; | ||
@@ -88,18 +125,30 @@ AnsiSerializer.prototype.text = function (content) { | ||
forEach(styles, function (styleName) { | ||
if (!ansiStyles[styleName] && rgbRegexp.test(styleName)) { | ||
if (ansiStyles[styleName]) { | ||
content = ansiStyles[styleName].open + content + ansiStyles[styleName].close; | ||
} else if (rgbRegexp.test(styleName)) { | ||
var originalStyleName = styleName; | ||
var backgroundColor = styleName.substring(0, 2) === 'bg'; | ||
var color = backgroundColor ? styleName.substring(2) : styleName; | ||
var colorObject = colorDiff.closest(convertColorToObject(color), colorDiffPalette); | ||
var closestColor = colorPalette[toHexColor(colorObject)]; | ||
if (backgroundColor) { | ||
styleName = 'bg' + firstUp(closestColor); | ||
var isBackgroundColor = styleName.substring(0, 2) === 'bg'; | ||
var colorName = isBackgroundColor ? styleName.substring(2) : styleName; | ||
var color16Hex = toHexColor(colorDiff.closest(convertColorToObject(colorName), diffPaletteByNumberOfColors[16])); | ||
var closestColor16 = colorPaletteByNumberOfColors[16][color16Hex]; | ||
var color256Hex = toHexColor(colorDiff.closest(convertColorToObject(colorName), diffPaletteByNumberOfColors[256])); | ||
var closest256ColorIndex = colorPaletteByNumberOfColors[256][color256Hex]; | ||
var closestColor16 = findClosestColorInDiffPalette(colorName, 16); | ||
var closest256ColorIndex = findClosestColorInDiffPalette(isBackgroundColor ? styleName.substring(2) : styleName, 256); | ||
if (isBackgroundColor) { | ||
styleName = 'bg' + firstUp(closestColor16); | ||
} else { | ||
styleName = closestColor; | ||
styleName = closestColor16; | ||
} | ||
ansiStyles[originalStyleName] = ansiStyles[styleName]; | ||
var open = ansiStyles[styleName].open; | ||
var close = ansiStyles[styleName].close; | ||
if (color16Hex !== color256Hex) { | ||
open += '\x1b[' + (isBackgroundColor ? 48 : 38) + ';5;' + closest256ColorIndex + 'm'; | ||
} | ||
ansiStyles[originalStyleName] = {open: open, close: close}; | ||
content = open + content + close; | ||
} | ||
if (ansiStyles[styleName]) { | ||
content = ansiStyles[styleName].open + content + ansiStyles[styleName].close; | ||
} | ||
}); | ||
@@ -106,0 +155,0 @@ } |
{ | ||
"name": "magicpen", | ||
"version": "0.4.3", | ||
"version": "0.5.0", | ||
"description": "Styled output in both consoles and browsers", | ||
@@ -5,0 +5,0 @@ "main": "./lib/MagicPen.js", |
@@ -335,3 +335,3 @@ /*global describe, it, beforeEach*/ | ||
expect(pen.toString('ansi'), 'to equal', | ||
'\x1B[33mHello world\x1B[39m'); | ||
'\x1b[33m\x1b[38;5;192mHello world\x1b[39m'); | ||
}); | ||
@@ -342,3 +342,3 @@ | ||
expect(pen.toString('ansi'), 'to equal', | ||
'\x1B[40mHello world\x1B[49m'); | ||
'\x1b[40m\x1b[48;5;236mHello world\x1b[49m' ); | ||
}); | ||
@@ -345,0 +345,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
153766
2275