lazily convert colors. other misc color fixes.

This commit is contained in:
Christopher Jeffrey 2013-07-14 06:57:15 -05:00
parent 5753d7b6ab
commit d0f99be764
4 changed files with 39 additions and 43 deletions

View File

@ -3,8 +3,19 @@
*/ */
// Try to match a hex code to a terminal color as best as possible. // Try to match a hex code to a terminal color as best as possible.
// This entire function assumes the terminal user is using the
// default xterm colors.
exports.matchColor = function(col) { exports.matchColor = function(col) {
if (col[0] !== '#') return col; if (col[0] !== '#') {
return col;
}
if (col.length === 4) {
col = col[0]
+ col[1] + col[1]
+ col[2] + col[2]
+ col[3] + col[3];
}
if (exports._cache[col] != null) { if (exports._cache[col] != null) {
return exports._cache[col]; return exports._cache[col];
@ -49,26 +60,6 @@ exports.matchColor = function(col) {
exports._cache = {}; exports._cache = {};
// Default VGA-like colors
exports.def = [
'#000000',
'#ee0000',
'#00ee00',
'#eeee00',
'#0000ee',
'#ee00ee',
'#00eeee',
'#eeeeee',
'#111111',
'#ff0000',
'#00ff00',
'#ffff00',
'#0000ff',
'#ff00ff',
'#00ffff',
'#ffffff'
];
// XTerm Colors // XTerm Colors
// These were actually tough to track down. The xterm source only uses color // These were actually tough to track down. The xterm source only uses color
// keywords. The X11 source needed to be examined to find the actual values. // keywords. The X11 source needed to be examined to find the actual values.
@ -164,6 +155,7 @@ exports.ccolors = (function() {
var colorNames = exports.colorNames = { var colorNames = exports.colorNames = {
default: -1, default: -1,
normal: -1,
bg: -1, bg: -1,
fg: -1, fg: -1,
black: 0, black: 0,

View File

@ -1373,7 +1373,7 @@ Tput.prototype.parseACS = function() {
} }
// See: ~/ncurses/ncurses/tinfo/lib_acs.c: L208 // See: ~/ncurses/ncurses/tinfo/lib_acs.c: L208
Object.keys(acsc).forEach(function(ch) { Object.keys(Tput.acsc).forEach(function(ch) {
var acs_chars = self.strings.acs_chars || '' var acs_chars = self.strings.acs_chars || ''
, i = acs_chars.indexOf(ch) , i = acs_chars.indexOf(ch)
, next = acs_chars[i + 1]; , next = acs_chars[i + 1];
@ -1382,8 +1382,8 @@ Tput.prototype.parseACS = function() {
return; return;
} }
self.acsc[ch] = acsc[next]; self.acsc[ch] = Tput.acsc[next];
self.acscr[acsc[next]] = ch; self.acscr[Tput.acsc[next]] = ch;
}); });
}; };
@ -2115,7 +2115,7 @@ Tput.strings = [
// DEC Special Character and Line Drawing Set. // DEC Special Character and Line Drawing Set.
// Taken from tty.js. // Taken from tty.js.
var acsc = { // (0 Tput.acsc = { // (0
'`': '\u25c6', // '◆' '`': '\u25c6', // '◆'
'a': '\u2592', // '▒' 'a': '\u2592', // '▒'
'b': '\u0009', // '\t' 'b': '\u0009', // '\t'
@ -2150,7 +2150,7 @@ var acsc = { // (0
}; };
// ['b', 'c', 'd', 'e', 'h', 'i'].forEach(function(ch) { // ['b', 'c', 'd', 'e', 'h', 'i'].forEach(function(ch) {
// delete acsc[ch]; // delete Tput.acsc[ch];
// }); // });
/** /**

View File

@ -797,7 +797,7 @@ 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 = colors.ccolors[col]; col = colors.ccolors[col];
if (col == null) col = 0x1ff; if (col == null) col = 0;
} 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) {
@ -1042,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] = colors.convert(val); effects[key] = 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] = colors.convert(v); effects[key][k] = v;
}); });
} }
}); });
@ -1115,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 = colors.convert(options.fg); this.fg = options.fg;
this.bg = colors.convert(options.bg); this.bg = 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;
@ -1132,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 = colors.convert(this.border.fg); this.border.fg = this.border.fg;
this.border.bg = colors.convert(this.border.bg); this.border.bg = this.border.bg;
this.border.ch = this.border.ch || ' '; this.border.ch = this.border.ch || ' ';
} }
@ -2053,8 +2053,8 @@ function Line(options) {
options.border = { options.border = {
type: 'bg', type: 'bg',
bg: colors.convert(options.bg), bg: options.bg,
fg: colors.convert(options.fg), fg: options.fg,
ch: !options.type || options.type === 'ascii' ch: !options.type || options.type === 'ascii'
? orientation === 'horizontal' ? '─' : '│' ? orientation === 'horizontal' ? '─' : '│'
: options.ch || ' ' : options.ch || ' '
@ -2090,8 +2090,8 @@ function ScrollableBox(options) {
this.scrollbar = options.scrollbar; this.scrollbar = options.scrollbar;
if (this.scrollbar) { if (this.scrollbar) {
this.scrollbar.fg = colors.convert(this.scrollbar.fg); this.scrollbar.fg = this.scrollbar.fg;
this.scrollbar.bg = colors.convert(this.scrollbar.bg); this.scrollbar.bg = this.scrollbar.bg;
this.scrollbar.ch = this.scrollbar.ch || ' '; this.scrollbar.ch = this.scrollbar.ch || ' ';
} }
} }
@ -2155,8 +2155,8 @@ function List(options) {
this.ritems = []; this.ritems = [];
this.selected = 0; this.selected = 0;
this.selectedBg = colors.convert(options.selectedBg); this.selectedBg = options.selectedBg;
this.selectedFg = colors.convert(options.selectedFg); this.selectedFg = 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;
@ -2946,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 = colors.convert(options.barFg); this.barFg = options.barFg;
this.barBg = colors.convert(options.barBg); this.barBg = options.barBg;
this.orientation = options.orientation || 'horizontal'; this.orientation = options.orientation || 'horizontal';
} }
@ -4045,14 +4045,16 @@ function sattr(obj, fg, bg) {
| ((obj.blink ? 4 : 0) << 18) | ((obj.blink ? 4 : 0) << 18)
| ((obj.underline ? 2 : 0) << 18)) | ((obj.underline ? 2 : 0) << 18))
| ((obj.bold ? 1 : 0) << 18) | ((obj.bold ? 1 : 0) << 18)
| (fg << 9)) | (colors.convert(fg) << 9))
| bg; | colors.convert(bg);
} }
/** /**
* Expose * Expose
*/ */
exports.colors = colors;
exports.Screen = exports.screen = Screen; exports.Screen = exports.screen = Screen;
exports.Box = exports.box = Box; exports.Box = exports.box = Box;
exports.Text = exports.text = Text; exports.Text = exports.text = Text;

View File

@ -10,6 +10,8 @@ screen.append(new blessed.Text({
width: '100%', width: '100%',
//bg: 'blue', //bg: 'blue',
content: '{green-fg}Welcome{/green-fg} to my {red-fg,ul}program{/red-fg,ul}', content: '{green-fg}Welcome{/green-fg} to my {red-fg,ul}program{/red-fg,ul}',
bg: '#0000ff',
// bg: blessed.colors.matchColor('#0000ff'),
tags: true, tags: true,
align: 'center' align: 'center'
})); }));