mirror of
https://github.com/embarklabs/neo-blessed.git
synced 2025-02-26 09:35:28 +00:00
refactor blend.
This commit is contained in:
parent
f5f2b71562
commit
0b6fceec23
186
lib/widget.js
186
lib/widget.js
@ -4004,7 +4004,7 @@ Element.prototype.render = function() {
|
||||
for (y = yi; y < yl; y++) {
|
||||
if (!lines[y]) break;
|
||||
for (x = xi; x < xl; x++) {
|
||||
lines[y][x][0] = blend(attr, lines[y][x][0]);
|
||||
lines[y][x][0] = this._blend(attr, lines[y][x][0]);
|
||||
lines[y][x][1] = ch;
|
||||
lines[y].dirty = true;
|
||||
}
|
||||
@ -4093,7 +4093,7 @@ Element.prototype.render = function() {
|
||||
cell = lines[y][x];
|
||||
if (!cell) break;
|
||||
if (this.style.transparent) {
|
||||
lines[y][x][0] = blend(attr, lines[y][x][0]);
|
||||
lines[y][x][0] = this._blend(attr, lines[y][x][0]);
|
||||
if (content[ci]) lines[y][x][1] = ch;
|
||||
lines[y].dirty = true;
|
||||
} else {
|
||||
@ -4133,7 +4133,7 @@ Element.prototype.render = function() {
|
||||
}
|
||||
|
||||
if (this.style.transparent) {
|
||||
lines[y][x][0] = blend(attr, lines[y][x][0]);
|
||||
lines[y][x][0] = this._blend(attr, lines[y][x][0]);
|
||||
if (content[ci]) lines[y][x][1] = ch;
|
||||
lines[y].dirty = true;
|
||||
} else {
|
||||
@ -4365,8 +4365,8 @@ Element.prototype.render = function() {
|
||||
x = xl;
|
||||
for (; x < xl + 2; x++) {
|
||||
if (!lines[y][x]) continue;
|
||||
// lines[y][x][0] = blend(this.dattr, lines[y][x][0]);
|
||||
lines[y][x][0] = blend(lines[y][x][0]);
|
||||
// lines[y][x][0] = this._blend(this.dattr, lines[y][x][0]);
|
||||
lines[y][x][0] = this._blend(lines[y][x][0]);
|
||||
lines[y].dirty = true;
|
||||
}
|
||||
}
|
||||
@ -4376,8 +4376,8 @@ Element.prototype.render = function() {
|
||||
if (!lines[y]) continue;
|
||||
for (x = xi + 1; x < xl; x++) {
|
||||
if (!lines[y][x]) continue;
|
||||
// lines[y][x][0] = blend(this.dattr, lines[y][x][0]);
|
||||
lines[y][x][0] = blend(lines[y][x][0]);
|
||||
// lines[y][x][0] = this._blend(this.dattr, lines[y][x][0]);
|
||||
lines[y][x][0] = this._blend(lines[y][x][0]);
|
||||
lines[y].dirty = true;
|
||||
}
|
||||
}
|
||||
@ -4439,6 +4439,94 @@ Screen.prototype._dockBorders = function() {
|
||||
|
||||
Element.prototype._render = Element.prototype.render;
|
||||
|
||||
/**
|
||||
* Blending and Shadows
|
||||
*/
|
||||
|
||||
Element.prototype._blend = function blend(attr, attr2) {
|
||||
var bg = attr & 0x1ff;
|
||||
if (attr2 != null) {
|
||||
var bg2 = attr2 & 0x1ff;
|
||||
if (bg === 0x1ff) bg = 0;
|
||||
if (bg2 === 0x1ff) bg2 = 0;
|
||||
bg = colors.mixColors(bg, bg2);
|
||||
} else {
|
||||
if (blend._cache[bg] != null) {
|
||||
bg = blend._cache[bg];
|
||||
// } else if (bg < 8) {
|
||||
// bg += 8;
|
||||
} else if (bg >= 8 && bg <= 15) {
|
||||
bg -= 8;
|
||||
} else {
|
||||
var name = colors.ncolors[bg];
|
||||
if (name) {
|
||||
for (var i = 0; i < colors.ncolors.length; i++) {
|
||||
if (name === colors.ncolors[i] && i !== bg) {
|
||||
var c = colors.vcolors[bg];
|
||||
var nc = colors.vcolors[i];
|
||||
if (nc[0] + nc[1] + nc[2] < c[0] + c[1] + c[2]) {
|
||||
blend._cache[bg] = i;
|
||||
bg = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attr &= ~0x1ff;
|
||||
attr |= bg;
|
||||
|
||||
var fg = (attr >> 9) & 0x1ff;
|
||||
if (attr2 != null) {
|
||||
var fg2 = (attr2 >> 9) & 0x1ff;
|
||||
// 0, 7, 188, 231, 251
|
||||
if (fg === 0x1ff) {
|
||||
// XXX workaround
|
||||
fg = 248;
|
||||
} else {
|
||||
if (fg === 0x1ff) fg = 7;
|
||||
if (fg2 === 0x1ff) fg2 = 7;
|
||||
fg = colors.mixColors(fg, fg2);
|
||||
}
|
||||
} else {
|
||||
if (blend._cache[fg] != null) {
|
||||
fg = blend._cache[fg];
|
||||
// } else if (fg < 8) {
|
||||
// fg += 8;
|
||||
} else if (fg >= 8 && fg <= 15) {
|
||||
fg -= 8;
|
||||
} else {
|
||||
var name = colors.ncolors[fg];
|
||||
if (name) {
|
||||
for (var i = 0; i < colors.ncolors.length; i++) {
|
||||
if (name === colors.ncolors[i] && i !== fg) {
|
||||
var c = colors.vcolors[fg];
|
||||
var nc = colors.vcolors[i];
|
||||
if (nc[0] + nc[1] + nc[2] < c[0] + c[1] + c[2]) {
|
||||
blend._cache[fg] = i;
|
||||
fg = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attr &= ~(0x1ff << 9);
|
||||
attr |= fg << 9;
|
||||
|
||||
return attr;
|
||||
}
|
||||
|
||||
Element.prototype._blend._cache = {};
|
||||
|
||||
/**
|
||||
* Content Methods
|
||||
*/
|
||||
|
||||
Element.prototype.insertLine = function(i, line) {
|
||||
if (typeof line === 'string') line = line.split('\n');
|
||||
|
||||
@ -8965,90 +9053,6 @@ function getAngle(lines, x, y) {
|
||||
return angleTable[angle] || ch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Blending and Shadows
|
||||
*/
|
||||
|
||||
function blend(attr, attr2) {
|
||||
var bg = attr & 0x1ff;
|
||||
if (attr2 != null) {
|
||||
var bg2 = attr2 & 0x1ff;
|
||||
if (bg === 0x1ff) bg = 0;
|
||||
if (bg2 === 0x1ff) bg2 = 0;
|
||||
bg = colors.mixColors(bg, bg2);
|
||||
} else {
|
||||
if (blend._cache[bg] != null) {
|
||||
bg = blend._cache[bg];
|
||||
// } else if (bg < 8) {
|
||||
// bg += 8;
|
||||
} else if (bg >= 8 && bg <= 15) {
|
||||
bg -= 8;
|
||||
} else {
|
||||
var name = colors.ncolors[bg];
|
||||
if (name) {
|
||||
for (var i = 0; i < colors.ncolors.length; i++) {
|
||||
if (name === colors.ncolors[i] && i !== bg) {
|
||||
var c = colors.vcolors[bg];
|
||||
var nc = colors.vcolors[i];
|
||||
if (nc[0] + nc[1] + nc[2] < c[0] + c[1] + c[2]) {
|
||||
blend._cache[bg] = i;
|
||||
bg = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attr &= ~0x1ff;
|
||||
attr |= bg;
|
||||
|
||||
var fg = (attr >> 9) & 0x1ff;
|
||||
if (attr2 != null) {
|
||||
var fg2 = (attr2 >> 9) & 0x1ff;
|
||||
// 0, 7, 188, 231, 251
|
||||
if (fg === 0x1ff) {
|
||||
// XXX workaround
|
||||
fg = 248;
|
||||
} else {
|
||||
if (fg === 0x1ff) fg = 7;
|
||||
if (fg2 === 0x1ff) fg2 = 7;
|
||||
fg = colors.mixColors(fg, fg2);
|
||||
}
|
||||
} else {
|
||||
if (blend._cache[fg] != null) {
|
||||
fg = blend._cache[fg];
|
||||
// } else if (fg < 8) {
|
||||
// fg += 8;
|
||||
} else if (fg >= 8 && fg <= 15) {
|
||||
fg -= 8;
|
||||
} else {
|
||||
var name = colors.ncolors[fg];
|
||||
if (name) {
|
||||
for (var i = 0; i < colors.ncolors.length; i++) {
|
||||
if (name === colors.ncolors[i] && i !== fg) {
|
||||
var c = colors.vcolors[fg];
|
||||
var nc = colors.vcolors[i];
|
||||
if (nc[0] + nc[1] + nc[2] < c[0] + c[1] + c[2]) {
|
||||
blend._cache[fg] = i;
|
||||
fg = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attr &= ~(0x1ff << 9);
|
||||
attr |= fg << 9;
|
||||
|
||||
return attr;
|
||||
}
|
||||
|
||||
blend._cache = {};
|
||||
|
||||
/**
|
||||
* Helpers
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user