mirror of
https://github.com/wavetermdev/xterm.js.git
synced 2026-08-05 13:43:48 -07:00
cleanup rgb channel conversion
This commit is contained in:
+8
-8
@@ -188,14 +188,14 @@ export enum BgFlags {
|
||||
}
|
||||
|
||||
export class AttributeData implements IAttributeData {
|
||||
static toRGB(value: number): IColorRGB {
|
||||
static toColorRGB(value: number): IColorRGB {
|
||||
return [
|
||||
value >>> Attributes.RED_SHIFT & 255,
|
||||
value >>> Attributes.GREEN_SHIFT & 255,
|
||||
value & 255
|
||||
];
|
||||
}
|
||||
static fromRGB(value: IColorRGB): number {
|
||||
static fromColorRGB(value: IColorRGB): number {
|
||||
return (value[0] & 255) << Attributes.RED_SHIFT | (value[1] & 255) << Attributes.GREEN_SHIFT | value[2] & 255;
|
||||
}
|
||||
|
||||
@@ -230,19 +230,19 @@ export class AttributeData implements IAttributeData {
|
||||
public isBgDefault(): boolean { return (this.bg & Attributes.CM_MASK) === 0; }
|
||||
|
||||
// colors
|
||||
public getFgColor(channels: boolean = false): number | IColorRGB {
|
||||
public getFgColor(): number {
|
||||
switch (this.fg & Attributes.CM_MASK) {
|
||||
case Attributes.CM_P16:
|
||||
case Attributes.CM_P256: return this.fg & Attributes.PCOLOR_MASK;
|
||||
case Attributes.CM_RGB: return (channels) ? AttributeData.toRGB(this.fg & Attributes.RGB_MASK) : this.fg & Attributes.RGB_MASK;
|
||||
case Attributes.CM_RGB: return this.fg & Attributes.RGB_MASK;
|
||||
default: return -1; // CM_DEFAULT defaults to -1
|
||||
}
|
||||
}
|
||||
public getBgColor(channels: boolean = false): number | IColorRGB {
|
||||
public getBgColor(): number {
|
||||
switch (this.bg & Attributes.CM_MASK) {
|
||||
case Attributes.CM_P16:
|
||||
case Attributes.CM_P256: return this.bg & Attributes.PCOLOR_MASK;
|
||||
case Attributes.CM_RGB: return (channels) ? AttributeData.toRGB(this.bg & Attributes.RGB_MASK) : this.bg & Attributes.RGB_MASK;
|
||||
case Attributes.CM_RGB: return this.bg & Attributes.RGB_MASK;
|
||||
default: return -1; // CM_DEFAULT defaults to -1
|
||||
}
|
||||
}
|
||||
@@ -273,7 +273,7 @@ export class AttributeData implements IAttributeData {
|
||||
return flags;
|
||||
}
|
||||
public getOldFgColor(): number {
|
||||
let color = this.getFgColor() as number;
|
||||
let color = this.getFgColor();
|
||||
if (color === -1) {
|
||||
return 256;
|
||||
}
|
||||
@@ -290,7 +290,7 @@ export class AttributeData implements IAttributeData {
|
||||
return color;
|
||||
}
|
||||
public getOldBgColor(): number {
|
||||
let color = this.getBgColor() as number;
|
||||
let color = this.getBgColor();
|
||||
if (color === -1) {
|
||||
return 256;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import { MockInputHandlingTerminal, TestTerminal } from './ui/TestUtils.test';
|
||||
import { DEFAULT_ATTR_DATA } from './Buffer';
|
||||
import { Terminal } from './Terminal';
|
||||
import { IBufferLine } from './Types';
|
||||
import { CellData, Attributes } from './BufferLine';
|
||||
import { CellData, Attributes, AttributeData } from './BufferLine';
|
||||
|
||||
describe('InputHandler', () => {
|
||||
describe('save and restore cursor', () => {
|
||||
@@ -486,9 +486,9 @@ describe('InputHandler', () => {
|
||||
term.writeSync(`\x1b[38;2;1;2;3;48;2;4;5;6m`);
|
||||
assert.equal(term.curAttrData.getFgColormode(), Attributes.CM_RGB);
|
||||
assert.equal(term.curAttrData.getFgColor(), 1 << 16 | 2 << 8 | 3);
|
||||
assert.deepEqual(term.curAttrData.getFgColor(true), [1, 2, 3]);
|
||||
assert.deepEqual(AttributeData.toColorRGB(term.curAttrData.getFgColor()), [1, 2, 3]);
|
||||
assert.equal(term.curAttrData.getBgColormode(), Attributes.CM_RGB);
|
||||
assert.deepEqual(term.curAttrData.getBgColor(true), [4, 5, 6]);
|
||||
assert.deepEqual(AttributeData.toColorRGB(term.curAttrData.getBgColor()), [4, 5, 6]);
|
||||
// reset to DEFAULT
|
||||
term.writeSync(`\x1b[39;49m`);
|
||||
assert.equal(term.curAttrData.getFgColormode(), 0);
|
||||
@@ -499,7 +499,7 @@ describe('InputHandler', () => {
|
||||
it('should zero missing RGB values', () => {
|
||||
term.writeSync(`\x1b[38;2;1;2;3m`);
|
||||
term.writeSync(`\x1b[38;2;5m`);
|
||||
assert.deepEqual(term.curAttrData.getFgColor(true), [5, 0, 0]);
|
||||
assert.deepEqual(AttributeData.toColorRGB(term.curAttrData.getFgColor()), [5, 0, 0]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+3
-7
@@ -14,7 +14,7 @@ import { IDisposable } from 'xterm';
|
||||
import { Disposable } from './common/Lifecycle';
|
||||
import { concat, utf32ToString } from './common/TypedArrayUtils';
|
||||
import { StringToUtf32, stringFromCodePoint } from './core/input/TextDecoder';
|
||||
import { CellData, Attributes, FgFlags, BgFlags } from './BufferLine';
|
||||
import { CellData, Attributes, FgFlags, BgFlags, AttributeData } from './BufferLine';
|
||||
|
||||
/**
|
||||
* Map collect to glevel. Used in `selectCharset`.
|
||||
@@ -1651,9 +1651,7 @@ export class InputHandler extends Disposable implements IInputHandler {
|
||||
i += 2;
|
||||
attr.fg |= Attributes.CM_RGB;
|
||||
attr.fg &= ~Attributes.RGB_MASK;
|
||||
attr.fg |= (params[i] & 0xFF) << Attributes.RED_SHIFT;
|
||||
attr.fg |= (params[i + 1] & 0xFF) << Attributes.GREEN_SHIFT;
|
||||
attr.fg |= (params[i + 2] & 0xFF) << Attributes.BLUE_SHIFT;
|
||||
attr.fg |= AttributeData.fromColorRGB([params[i], params[i + 1], params[i + 2]]);
|
||||
i += 2;
|
||||
} else if (params[i + 1] === 5) {
|
||||
i += 2;
|
||||
@@ -1667,9 +1665,7 @@ export class InputHandler extends Disposable implements IInputHandler {
|
||||
i += 2;
|
||||
attr.bg |= Attributes.CM_RGB;
|
||||
attr.bg &= ~Attributes.RGB_MASK;
|
||||
attr.bg |= (params[i] & 0xFF) << Attributes.RED_SHIFT;
|
||||
attr.bg |= (params[i + 1] & 0xFF) << Attributes.GREEN_SHIFT;
|
||||
attr.bg |= (params[i + 2] & 0xFF) << Attributes.BLUE_SHIFT;
|
||||
attr.bg |= AttributeData.fromColorRGB([params[i], params[i + 1], params[i + 2]]);
|
||||
i += 2;
|
||||
} else if (params[i + 1] === 5) {
|
||||
i += 2;
|
||||
|
||||
+2
-2
@@ -542,8 +542,8 @@ export interface IAttributeData {
|
||||
isBgDefault(): boolean;
|
||||
|
||||
// colors
|
||||
getFgColor(channels?: boolean): number | IColorRGB;
|
||||
getBgColor(channels?: boolean): number | IColorRGB;
|
||||
getFgColor(): number;
|
||||
getBgColor(): number;
|
||||
|
||||
// shim for old API
|
||||
getOldFlags(): number;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import { NULL_CELL_CODE, WHITESPACE_CELL_CHAR } from '../../Buffer';
|
||||
import { IBufferLine } from '../../Types';
|
||||
import { INVERTED_DEFAULT_COLOR } from '../atlas/Types';
|
||||
import { CellData } from '../../BufferLine';
|
||||
import { CellData, AttributeData } from '../../BufferLine';
|
||||
|
||||
export const BOLD_CLASS = 'xterm-bold';
|
||||
export const ITALIC_CLASS = 'xterm-italic';
|
||||
@@ -78,15 +78,15 @@ export class DomRendererRowFactory {
|
||||
|
||||
charElement.textContent = this._cell.chars || WHITESPACE_CELL_CHAR;
|
||||
|
||||
const swapColor = !!this._cell.isInverse();
|
||||
const swapColor = this._cell.isInverse();
|
||||
|
||||
// fg
|
||||
if (this._cell.isFgRGB()) {
|
||||
let style = charElement.getAttribute('style') || '';
|
||||
style += `${swapColor ? 'background-' : ''}color:rgb(${(this._cell.getFgColor(true) as number[]).join(',')});`;
|
||||
style += `${swapColor ? 'background-' : ''}color:rgb(${(AttributeData.toColorRGB(this._cell.getFgColor())).join(',')});`;
|
||||
charElement.setAttribute('style', style);
|
||||
} else if (this._cell.isFgPalette()) {
|
||||
let fg = this._cell.getFgColor() as number;
|
||||
let fg = this._cell.getFgColor();
|
||||
if (this._cell.isBold() && fg < 8 && !swapColor) {
|
||||
fg += 8;
|
||||
}
|
||||
@@ -98,7 +98,7 @@ export class DomRendererRowFactory {
|
||||
// bg
|
||||
if (this._cell.isBgRGB()) {
|
||||
let style = charElement.getAttribute('style') || '';
|
||||
style += `${swapColor ? '' : 'background-'}color:rgb(${(this._cell.getBgColor(true) as number[]).join(',')});`;
|
||||
style += `${swapColor ? '' : 'background-'}color:rgb(${(AttributeData.toColorRGB(this._cell.getBgColor())).join(',')});`;
|
||||
charElement.setAttribute('style', style);
|
||||
} else if (this._cell.isBgPalette()) {
|
||||
charElement.classList.add(`xterm-${swapColor ? 'f' : 'b'}g-${this._cell.getBgColor()}`);
|
||||
|
||||
Reference in New Issue
Block a user