better color reduction.

This commit is contained in:
Christopher Jeffrey 2013-07-14 06:43:17 -05:00
parent d5f3874105
commit 5753d7b6ab
2 changed files with 36 additions and 63 deletions

View File

@ -189,14 +189,24 @@ var colorNames = exports.colorNames = {
//}); //});
exports.convert = function(color) { exports.convert = function(color) {
var val = colorNames[color]; if (typeof color === 'string') {
if (typeof val === 'string') val = val.replace(/[\- ]/g, ''); color = color.replace(/[\- ]/g, '');
if (val == null) val = color;
if (val == null) val = -1;
if (typeof val === 'string') {
val = exports.matchColor(val);
} }
var val = colorNames[color];
if (val == null) val = color;
if (val == null) val = -1;
if (typeof val === 'string') {
val = val[0] === '#'
? exports.matchColor(val)
: -1;
}
if (val === -1) return 0x1ff; if (val === -1) return 0x1ff;
return val; return val;
}; };

View File

@ -10,7 +10,8 @@
var EventEmitter = require('events').EventEmitter var EventEmitter = require('events').EventEmitter
, path = require('path') , path = require('path')
, fs = require('fs'); , fs = require('fs')
, colors = require('./colors');
/** /**
* Node * Node
@ -727,9 +728,7 @@ Screen.prototype.draw = function(start, end) {
} }
if (bgColor !== 0x1ff) { if (bgColor !== 0x1ff) {
if (this.tput) { bgColor = this._reduceColor(bgColor);
bgColor = this._reduceColor(bgColor);
}
if (bgColor < 16) { if (bgColor < 16) {
if (bgColor < 8) { if (bgColor < 8) {
bgColor += 40; bgColor += 40;
@ -744,9 +743,7 @@ Screen.prototype.draw = function(start, end) {
} }
if (fgColor !== 0x1ff) { if (fgColor !== 0x1ff) {
if (this.tput) { fgColor = this._reduceColor(fgColor);
fgColor = this._reduceColor(fgColor);
}
if (fgColor < 16) { if (fgColor < 16) {
if (fgColor < 8) { if (fgColor < 8) {
fgColor += 30; fgColor += 30;
@ -799,10 +796,8 @@ Screen.prototype.draw = function(start, end) {
Screen.prototype._reduceColor = function(col) { Screen.prototype._reduceColor = function(col) {
if (this.tput) { if (this.tput) {
if (col >= 16 && this.tput.colors <= 16) { if (col >= 16 && this.tput.colors <= 16) {
//col = Screen.ccolors[col]; col = colors.ccolors[col];
if (col >= 244) col = colors.white; if (col == null) col = 0x1ff;
else if (col >= 232) col = colors.black;
else col = colors.blue;
} else if (col >= 8 && this.tput.colors <= 8) { } else if (col >= 8 && this.tput.colors <= 8) {
col -= 8; col -= 8;
} else if (col >= 2 && this.tput.colors <= 2) { } else if (col >= 2 && this.tput.colors <= 2) {
@ -1047,11 +1042,11 @@ Screen.prototype.setEffects = function(el, fel, over, out, effects, temp) {
Object.keys(effects).forEach(function(key) { Object.keys(effects).forEach(function(key) {
var val = effects[key]; var val = effects[key];
if (typeof val === 'string') { if (typeof val === 'string') {
effects[key] = convert(val); effects[key] = colors.convert(val);
} else if (val && typeof val === 'object' && !Array.isArray(val)) { } else if (val && typeof val === 'object' && !Array.isArray(val)) {
Object.keys(effects[key]).forEach(function(k) { Object.keys(effects[key]).forEach(function(k) {
var v = effects[key][k]; var v = effects[key][k];
effects[key][k] = convert(v); effects[key][k] = colors.convert(v);
}); });
} }
}); });
@ -1120,8 +1115,8 @@ function Element(options) {
// this.position.padding = options.padding || 0; // this.position.padding = options.padding || 0;
// this.position.margin = options.margin || 0; // this.position.margin = options.margin || 0;
this.fg = convert(options.fg); this.fg = colors.convert(options.fg);
this.bg = convert(options.bg); this.bg = colors.convert(options.bg);
this.bold = options.bold; this.bold = options.bold;
this.underline = options.underline; this.underline = options.underline;
this.blink = options.blink; this.blink = options.blink;
@ -1137,8 +1132,8 @@ function Element(options) {
this.border = options.border; this.border = options.border;
if (this.border) { if (this.border) {
this.border.type = this.border.type || 'bg'; this.border.type = this.border.type || 'bg';
this.border.fg = convert(this.border.fg); this.border.fg = colors.convert(this.border.fg);
this.border.bg = convert(this.border.bg); this.border.bg = colors.convert(this.border.bg);
this.border.ch = this.border.ch || ' '; this.border.ch = this.border.ch || ' ';
} }
@ -2058,8 +2053,8 @@ function Line(options) {
options.border = { options.border = {
type: 'bg', type: 'bg',
bg: convert(options.bg), bg: colors.convert(options.bg),
fg: convert(options.fg), fg: colors.convert(options.fg),
ch: !options.type || options.type === 'ascii' ch: !options.type || options.type === 'ascii'
? orientation === 'horizontal' ? '─' : '│' ? orientation === 'horizontal' ? '─' : '│'
: options.ch || ' ' : options.ch || ' '
@ -2095,8 +2090,8 @@ function ScrollableBox(options) {
this.scrollbar = options.scrollbar; this.scrollbar = options.scrollbar;
if (this.scrollbar) { if (this.scrollbar) {
this.scrollbar.fg = convert(this.scrollbar.fg); this.scrollbar.fg = colors.convert(this.scrollbar.fg);
this.scrollbar.bg = convert(this.scrollbar.bg); this.scrollbar.bg = colors.convert(this.scrollbar.bg);
this.scrollbar.ch = this.scrollbar.ch || ' '; this.scrollbar.ch = this.scrollbar.ch || ' ';
} }
} }
@ -2160,8 +2155,8 @@ function List(options) {
this.ritems = []; this.ritems = [];
this.selected = 0; this.selected = 0;
this.selectedBg = convert(options.selectedBg); this.selectedBg = colors.convert(options.selectedBg);
this.selectedFg = convert(options.selectedFg); this.selectedFg = colors.convert(options.selectedFg);
this.selectedBold = options.selectedBold; this.selectedBold = options.selectedBold;
this.selectedUnderline = options.selectedUnderline; this.selectedUnderline = options.selectedUnderline;
this.selectedBlink = options.selectedBlink; this.selectedBlink = options.selectedBlink;
@ -2951,8 +2946,8 @@ function ProgressBar(options) {
this.filled = +this.filled.slice(0, -1); this.filled = +this.filled.slice(0, -1);
} }
this.ch = options.ch || ' '; this.ch = options.ch || ' ';
this.barFg = convert(options.barFg); this.barFg = colors.convert(options.barFg);
this.barBg = convert(options.barBg); this.barBg = colors.convert(options.barBg);
this.orientation = options.orientation || 'horizontal'; this.orientation = options.orientation || 'horizontal';
} }
@ -4044,38 +4039,6 @@ function hsort(obj) {
}); });
} }
var colors = {
default: -1,
bg: -1,
fg: -1,
black: 0,
red: 1,
green: 2,
yellow: 3,
blue: 4,
magenta: 5,
cyan: 6,
white: 7,
lightblack: 8,
lightred: 9,
lightgreen: 10,
lightyellow: 11,
lightblue: 12,
lightmagenta: 13,
lightcyan: 14,
lightwhite: 15
};
function convert(color) {
var val = colors[color];
if (typeof val === 'string') val = val.replace(/[\- ]/g, '');
if (val == null) val = color;
if (val == null) val = -1;
//if (typeof val === 'string') val = Screen._findColor(val);
if (val === -1) return 0x1ff;
return val;
}
function sattr(obj, fg, bg) { function sattr(obj, fg, bg) {
return ((((obj.invisible ? 16 : 0) << 18) return ((((obj.invisible ? 16 : 0) << 18)
| ((obj.inverse ? 8 : 0) << 18) | ((obj.inverse ? 8 : 0) << 18)