remove old color and flag shims

This commit is contained in:
Jörg Breitbart
2019-01-17 01:31:59 +01:00
parent 8d850d6c8b
commit df739ab814
3 changed files with 5 additions and 126 deletions
-116
View File
@@ -5,64 +5,8 @@
import { CharData, IBufferLine, ICellData, IColorRGB, IAttributeData } from './Types';
import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, WHITESPACE_CELL_CHAR, CHAR_DATA_ATTR_INDEX } from './Buffer';
import { stringFromCodePoint } from './core/input/TextDecoder';
import { FLAGS } from './renderer/Types';
import { DEFAULT_ANSI_COLORS } from './renderer/ColorManager';
/**
* TODO:
* The below color-related code can be removed when true color is implemented.
* It's only purpose is to match true color requests with the closest matching
* ANSI color code.
*/
const matchColorCache: {[colorRGBHash: number]: number} = {};
// http://stackoverflow.com/questions/1633828
function matchColorDistance(r1: number, g1: number, b1: number, r2: number, g2: number, b2: number): number {
return Math.pow(30 * (r1 - r2), 2)
+ Math.pow(59 * (g1 - g2), 2)
+ Math.pow(11 * (b1 - b2), 2);
}
function matchColor(r1: number, g1: number, b1: number): number {
const hash = (r1 << 16) | (g1 << 8) | b1;
if (matchColorCache[hash] !== null && matchColorCache[hash] !== undefined) {
return matchColorCache[hash];
}
let ldiff = Infinity;
let li = -1;
let i = 0;
let c: number;
let r2: number;
let g2: number;
let b2: number;
let diff: number;
for (; i < DEFAULT_ANSI_COLORS.length; i++) {
c = DEFAULT_ANSI_COLORS[i].rgba;
r2 = c >>> 24;
g2 = c >>> 16 & 0xFF;
b2 = c >>> 8 & 0xFF;
// assume that alpha is 0xFF
diff = matchColorDistance(r1, g1, b1, r2, g2, b2);
if (diff === 0) {
li = i;
break;
}
if (diff < ldiff) {
ldiff = diff;
li = i;
}
}
return matchColorCache[hash] = li;
}
/**
* buffer memory layout:
*
@@ -246,66 +190,6 @@ export class AttributeData implements IAttributeData {
default: return -1; // CM_DEFAULT defaults to -1
}
}
public getOldFlags(): number {
let flags = 0;
if (this.isBold()) {
flags |= FLAGS.BOLD;
}
if (this.isUnderline()) {
flags |= FLAGS.UNDERLINE;
}
if (this.isBlink()) {
flags |= FLAGS.BLINK;
}
if (this.isDim()) {
flags |= FLAGS.DIM;
}
if (this.isInvisible()) {
flags |= FLAGS.INVISIBLE;
}
if (this.isInverse()) {
flags |= FLAGS.INVERSE;
}
if (this.isItalic()) {
flags |= FLAGS.ITALIC;
}
return flags;
}
public getOldFgColor(): number {
let color = this.getFgColor();
if (color === -1) {
return 256;
}
if (this.isFgRGB()) {
color = matchColor(
(this.fg & Attributes.RED_MASK) >> Attributes.RED_SHIFT,
(this.fg & Attributes.GREEN_MASK) >> Attributes.GREEN_SHIFT,
(this.fg & Attributes.BLUE_MASK) >> Attributes.BLUE_SHIFT
);
if (color === -1) {
color = 256;
}
}
return color;
}
public getOldBgColor(): number {
let color = this.getBgColor();
if (color === -1) {
return 256;
}
if (this.isBgRGB()) {
color = matchColor(
(this.bg & Attributes.RED_MASK) >> Attributes.RED_SHIFT,
(this.bg & Attributes.GREEN_MASK) >> Attributes.GREEN_SHIFT,
(this.bg & Attributes.BLUE_MASK) >> Attributes.BLUE_SHIFT
);
if (color === -1) {
color = 256;
}
}
return color;
}
}
/**
+5 -5
View File
@@ -357,14 +357,14 @@ describe('InputHandler', () => {
expect(term.buffer.translateBufferLineToString(0, true)).to.equal('');
expect(term.buffer.translateBufferLineToString(1, true)).to.equal(' TEST');
// Text color of 'TEST' should be red
expect((term.buffer.lines.get(1).loadCell(4, new CellData()).getOldFgColor())).to.equal(1);
expect((term.buffer.lines.get(1).loadCell(4, new CellData()).getFgColor())).to.equal(1);
});
it('should handle DECSET/DECRST 1047 (alt screen buffer)', () => {
handler.parse('\x1b[?1047h\r\n\x1b[31mJUNK\x1b[?1047lTEST');
expect(term.buffer.translateBufferLineToString(0, true)).to.equal('');
expect(term.buffer.translateBufferLineToString(1, true)).to.equal(' TEST');
// Text color of 'TEST' should be red
expect((term.buffer.lines.get(1).loadCell(4, new CellData()).getOldFgColor())).to.equal(1);
expect((term.buffer.lines.get(1).loadCell(4, new CellData()).getFgColor())).to.equal(1);
});
it('should handle DECSET/DECRST 1048 (alt screen cursor)', () => {
handler.parse('\x1b[?1048h\r\n\x1b[31mJUNK\x1b[?1048lTEST');
@@ -373,7 +373,7 @@ describe('InputHandler', () => {
// Text color of 'TEST' should be default
expect(term.buffer.lines.get(0).loadCell(0, new CellData()).fg).to.equal(DEFAULT_ATTR_DATA.fg);
// Text color of 'JUNK' should be red
expect((term.buffer.lines.get(1).loadCell(0, new CellData()).getOldFgColor())).to.equal(1);
expect((term.buffer.lines.get(1).loadCell(0, new CellData()).getFgColor())).to.equal(1);
});
it('should handle DECSET/DECRST 1049 (alt screen buffer+cursor)', () => {
handler.parse('\x1b[?1049h\r\n\x1b[31mJUNK\x1b[?1049lTEST');
@@ -390,12 +390,12 @@ describe('InputHandler', () => {
handler.parse('\x1b[?1049h\x1b[uTEST');
expect(term.buffer.translateBufferLineToString(1, true)).to.equal('TEST');
// Text color of 'TEST' should be red
expect((term.buffer.lines.get(1).loadCell(0, new CellData()).getOldFgColor())).to.equal(1);
expect((term.buffer.lines.get(1).loadCell(0, new CellData()).getFgColor())).to.equal(1);
});
it('should handle DECSET/DECRST 1049 - clears alt buffer with erase attributes', () => {
handler.parse('\x1b[42m\x1b[?1049h');
// Buffer should be filled with green background
expect(term.buffer.lines.get(20).loadCell(10, new CellData()).getOldBgColor()).to.equal(2);
expect(term.buffer.lines.get(20).loadCell(10, new CellData()).getBgColor()).to.equal(2);
});
});
-5
View File
@@ -544,11 +544,6 @@ export interface IAttributeData {
// colors
getFgColor(): number;
getBgColor(): number;
// shim for old API
getOldFlags(): number;
getOldFgColor(): number;
getOldBgColor(): number;
}
/** Cell data */