2 * Copyright (c) 2018 The xterm.js authors. All rights reserved.
6 import { IAttributeData, IColorRGB } from 'common/Types';
7 import { Attributes, FgFlags, BgFlags } from 'common/buffer/Constants';
9 export class AttributeData implements IAttributeData {
10 static toColorRGB(value: number): IColorRGB {
12 value >>> Attributes.RED_SHIFT & 255,
13 value >>> Attributes.GREEN_SHIFT & 255,
17 static fromColorRGB(value: IColorRGB): number {
18 return (value[0] & 255) << Attributes.RED_SHIFT | (value[1] & 255) << Attributes.GREEN_SHIFT | value[2] & 255;
21 public clone(): IAttributeData {
22 const newObj = new AttributeData();
29 public fg: number = 0;
30 public bg: number = 0;
33 public isInverse(): number { return this.fg & FgFlags.INVERSE; }
34 public isBold(): number { return this.fg & FgFlags.BOLD; }
35 public isUnderline(): number { return this.fg & FgFlags.UNDERLINE; }
36 public isBlink(): number { return this.fg & FgFlags.BLINK; }
37 public isInvisible(): number { return this.fg & FgFlags.INVISIBLE; }
38 public isItalic(): number { return this.bg & BgFlags.ITALIC; }
39 public isDim(): number { return this.bg & BgFlags.DIM; }
42 public getFgColorMode(): number { return this.fg & Attributes.CM_MASK; }
43 public getBgColorMode(): number { return this.bg & Attributes.CM_MASK; }
44 public isFgRGB(): boolean { return (this.fg & Attributes.CM_MASK) === Attributes.CM_RGB; }
45 public isBgRGB(): boolean { return (this.bg & Attributes.CM_MASK) === Attributes.CM_RGB; }
46 public isFgPalette(): boolean { return (this.fg & Attributes.CM_MASK) === Attributes.CM_P16 || (this.fg & Attributes.CM_MASK) === Attributes.CM_P256; }
47 public isBgPalette(): boolean { return (this.bg & Attributes.CM_MASK) === Attributes.CM_P16 || (this.bg & Attributes.CM_MASK) === Attributes.CM_P256; }
48 public isFgDefault(): boolean { return (this.fg & Attributes.CM_MASK) === 0; }
49 public isBgDefault(): boolean { return (this.bg & Attributes.CM_MASK) === 0; }
52 public getFgColor(): number {
53 switch (this.fg & Attributes.CM_MASK) {
54 case Attributes.CM_P16:
55 case Attributes.CM_P256: return this.fg & Attributes.PCOLOR_MASK;
56 case Attributes.CM_RGB: return this.fg & Attributes.RGB_MASK;
57 default: return -1; // CM_DEFAULT defaults to -1
60 public getBgColor(): number {
61 switch (this.bg & Attributes.CM_MASK) {
62 case Attributes.CM_P16:
63 case Attributes.CM_P256: return this.bg & Attributes.PCOLOR_MASK;
64 case Attributes.CM_RGB: return this.bg & Attributes.RGB_MASK;
65 default: return -1; // CM_DEFAULT defaults to -1