| 1 |
import { ISelectionRenderModel } from 'browser/renderer/shared/Types'; |
| 2 |
import { ICoreBrowserService, IThemeService } from 'browser/services/Services'; |
| 3 |
import { ReadonlyColorSet } from 'browser/Types'; |
| 4 |
import { Attributes, BgFlags, ExtFlags, FgFlags, NULL_CELL_CODE, UnderlineStyle } from 'common/buffer/Constants'; |
| 5 |
import { IDecorationService, IOptionsService } from 'common/services/Services'; |
| 6 |
import { ICellData } from 'common/Types'; |
| 7 |
import { Terminal } from '@xterm/xterm'; |
| 8 |
import { rgba } from 'common/Color'; |
| 9 |
import { treatGlyphAsBackgroundColor } from 'browser/renderer/shared/RendererUtils'; |
| 10 |
|
| 11 |
// Work variables to avoid garbage collection |
| 12 |
let $fg = 0; |
| 13 |
let $bg = 0; |
| 14 |
let $hasFg = false; |
| 15 |
let $hasBg = false; |
| 16 |
let $isSelected = false; |
| 17 |
let $colors: ReadonlyColorSet | undefined; |
| 18 |
let $variantOffset = 0; |
| 19 |
|
| 20 |
export class CellColorResolver { |
| 21 |
/** |
| 22 |
* The shared result of the {@link resolve} call. This is only safe to use immediately after as |
| 23 |
* any other calls will share object. |
| 24 |
*/ |
| 25 |
public readonly result: { fg: number, bg: number, ext: number } = { |
| 26 |
fg: 0, |
| 27 |
bg: 0, |
| 28 |
ext: 0 |
| 29 |
}; |
| 30 |
|
| 31 |
constructor( |
| 32 |
private readonly _terminal: Terminal, |
| 33 |
private readonly _optionService: IOptionsService, |
| 34 |
private readonly _selectionRenderModel: ISelectionRenderModel, |
| 35 |
private readonly _decorationService: IDecorationService, |
| 36 |
private readonly _coreBrowserService: ICoreBrowserService, |
| 37 |
private readonly _themeService: IThemeService |
| 38 |
) { |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Resolves colors for the cell, putting the result into the shared {@link result}. This resolves |
| 43 |
* overrides, inverse and selection for the cell which can then be used to feed into the renderer. |
| 44 |
*/ |
| 45 |
public resolve(cell: ICellData, x: number, y: number, deviceCellWidth: number): void { |
| 46 |
this.result.bg = cell.bg; |
| 47 |
this.result.fg = cell.fg; |
| 48 |
this.result.ext = cell.bg & BgFlags.HAS_EXTENDED ? cell.extended.ext : 0; |
| 49 |
// Get any foreground/background overrides, this happens on the model to avoid spreading |
| 50 |
// override logic throughout the different sub-renderers |
| 51 |
|
| 52 |
// Reset overrides work variables |
| 53 |
$bg = 0; |
| 54 |
$fg = 0; |
| 55 |
$hasBg = false; |
| 56 |
$hasFg = false; |
| 57 |
$isSelected = false; |
| 58 |
$colors = this._themeService.colors; |
| 59 |
$variantOffset = 0; |
| 60 |
|
| 61 |
const code = cell.getCode(); |
| 62 |
if (code !== NULL_CELL_CODE && cell.extended.underlineStyle === UnderlineStyle.DOTTED) { |
| 63 |
const lineWidth = Math.max(1, Math.floor(this._optionService.rawOptions.fontSize * this._coreBrowserService.dpr / 15)); |
| 64 |
$variantOffset = x * deviceCellWidth % (Math.round(lineWidth) * 2); |
| 65 |
} |
| 66 |
|
| 67 |
// Apply decorations on the bottom layer |
| 68 |
this._decorationService.forEachDecorationAtCell(x, y, 'bottom', d => { |
| 69 |
if (d.backgroundColorRGB) { |
| 70 |
$bg = d.backgroundColorRGB.rgba >> 8 & Attributes.RGB_MASK; |
| 71 |
$hasBg = true; |
| 72 |
} |
| 73 |
if (d.foregroundColorRGB) { |
| 74 |
$fg = d.foregroundColorRGB.rgba >> 8 & Attributes.RGB_MASK; |
| 75 |
$hasFg = true; |
| 76 |
} |
| 77 |
}); |
| 78 |
|
| 79 |
// Apply the selection color if needed |
| 80 |
$isSelected = this._selectionRenderModel.isCellSelected(this._terminal, x, y); |
| 81 |
if ($isSelected) { |
| 82 |
// If the cell has a bg color, retain the color by blending it with the selection color |
| 83 |
if ( |
| 84 |
(this.result.fg & FgFlags.INVERSE) || |
| 85 |
(this.result.bg & Attributes.CM_MASK) !== Attributes.CM_DEFAULT |
| 86 |
) { |
| 87 |
// Resolve the standard bg color |
| 88 |
if (this.result.fg & FgFlags.INVERSE) { |
| 89 |
switch (this.result.fg & Attributes.CM_MASK) { |
| 90 |
case Attributes.CM_P16: |
| 91 |
case Attributes.CM_P256: |
| 92 |
$bg = this._themeService.colors.ansi[this.result.fg & Attributes.PCOLOR_MASK].rgba; |
| 93 |
break; |
| 94 |
case Attributes.CM_RGB: |
| 95 |
$bg = ((this.result.fg & Attributes.RGB_MASK) << 8) | 0xFF; |
| 96 |
break; |
| 97 |
case Attributes.CM_DEFAULT: |
| 98 |
default: |
| 99 |
$bg = this._themeService.colors.foreground.rgba; |
| 100 |
} |
| 101 |
} else { |
| 102 |
switch (this.result.bg & Attributes.CM_MASK) { |
| 103 |
case Attributes.CM_P16: |
| 104 |
case Attributes.CM_P256: |
| 105 |
$bg = this._themeService.colors.ansi[this.result.bg & Attributes.PCOLOR_MASK].rgba; |
| 106 |
break; |
| 107 |
case Attributes.CM_RGB: |
| 108 |
$bg = ((this.result.bg & Attributes.RGB_MASK) << 8) | 0xFF; |
| 109 |
break; |
| 110 |
// No need to consider default bg color here as it's not possible |
| 111 |
} |
| 112 |
} |
| 113 |
// Blend with selection bg color |
| 114 |
$bg = rgba.blend( |
| 115 |
$bg, |
| 116 |
((this._coreBrowserService.isFocused ? $colors.selectionBackgroundOpaque : $colors.selectionInactiveBackgroundOpaque).rgba & 0xFFFFFF00) | 0x80 |
| 117 |
) >> 8 & Attributes.RGB_MASK; |
| 118 |
} else { |
| 119 |
$bg = (this._coreBrowserService.isFocused ? $colors.selectionBackgroundOpaque : $colors.selectionInactiveBackgroundOpaque).rgba >> 8 & Attributes.RGB_MASK; |
| 120 |
} |
| 121 |
$hasBg = true; |
| 122 |
|
| 123 |
// Apply explicit selection foreground if present |
| 124 |
if ($colors.selectionForeground) { |
| 125 |
$fg = $colors.selectionForeground.rgba >> 8 & Attributes.RGB_MASK; |
| 126 |
$hasFg = true; |
| 127 |
} |
| 128 |
|
| 129 |
// Overwrite fg as bg if it's a special decorative glyph (eg. powerline) |
| 130 |
if (treatGlyphAsBackgroundColor(cell.getCode())) { |
| 131 |
// Inverse default background should be treated as transparent |
| 132 |
if ( |
| 133 |
(this.result.fg & FgFlags.INVERSE) && |
| 134 |
(this.result.bg & Attributes.CM_MASK) === Attributes.CM_DEFAULT |
| 135 |
) { |
| 136 |
$fg = (this._coreBrowserService.isFocused ? $colors.selectionBackgroundOpaque : $colors.selectionInactiveBackgroundOpaque).rgba >> 8 & Attributes.RGB_MASK; |
| 137 |
} else { |
| 138 |
|
| 139 |
if (this.result.fg & FgFlags.INVERSE) { |
| 140 |
switch (this.result.bg & Attributes.CM_MASK) { |
| 141 |
case Attributes.CM_P16: |
| 142 |
case Attributes.CM_P256: |
| 143 |
$fg = this._themeService.colors.ansi[this.result.bg & Attributes.PCOLOR_MASK].rgba; |
| 144 |
break; |
| 145 |
case Attributes.CM_RGB: |
| 146 |
$fg = ((this.result.bg & Attributes.RGB_MASK) << 8) | 0xFF; |
| 147 |
break; |
| 148 |
// No need to consider default bg color here as it's not possible |
| 149 |
} |
| 150 |
} else { |
| 151 |
switch (this.result.fg & Attributes.CM_MASK) { |
| 152 |
case Attributes.CM_P16: |
| 153 |
case Attributes.CM_P256: |
| 154 |
$fg = this._themeService.colors.ansi[this.result.fg & Attributes.PCOLOR_MASK].rgba; |
| 155 |
break; |
| 156 |
case Attributes.CM_RGB: |
| 157 |
$fg = ((this.result.fg & Attributes.RGB_MASK) << 8) | 0xFF; |
| 158 |
break; |
| 159 |
case Attributes.CM_DEFAULT: |
| 160 |
default: |
| 161 |
$fg = this._themeService.colors.foreground.rgba; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
$fg = rgba.blend( |
| 166 |
$fg, |
| 167 |
((this._coreBrowserService.isFocused ? $colors.selectionBackgroundOpaque : $colors.selectionInactiveBackgroundOpaque).rgba & 0xFFFFFF00) | 0x80 |
| 168 |
) >> 8 & Attributes.RGB_MASK; |
| 169 |
} |
| 170 |
$hasFg = true; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// Apply decorations on the top layer |
| 175 |
this._decorationService.forEachDecorationAtCell(x, y, 'top', d => { |
| 176 |
if (d.backgroundColorRGB) { |
| 177 |
$bg = d.backgroundColorRGB.rgba >> 8 & Attributes.RGB_MASK; |
| 178 |
$hasBg = true; |
| 179 |
} |
| 180 |
if (d.foregroundColorRGB) { |
| 181 |
$fg = d.foregroundColorRGB.rgba >> 8 & Attributes.RGB_MASK; |
| 182 |
$hasFg = true; |
| 183 |
} |
| 184 |
}); |
| 185 |
|
| 186 |
// Convert any overrides from rgba to the fg/bg packed format. This resolves the inverse flag |
| 187 |
// ahead of time in order to use the correct cache key |
| 188 |
if ($hasBg) { |
| 189 |
if ($isSelected) { |
| 190 |
// Non-RGB attributes from model + force non-dim + override + force RGB color mode |
| 191 |
$bg = (cell.bg & ~Attributes.RGB_MASK & ~BgFlags.DIM) | $bg | Attributes.CM_RGB; |
| 192 |
} else { |
| 193 |
// Non-RGB attributes from model + override + force RGB color mode |
| 194 |
$bg = (cell.bg & ~Attributes.RGB_MASK) | $bg | Attributes.CM_RGB; |
| 195 |
} |
| 196 |
} |
| 197 |
if ($hasFg) { |
| 198 |
// Non-RGB attributes from model + force disable inverse + override + force RGB color mode |
| 199 |
$fg = (cell.fg & ~Attributes.RGB_MASK & ~FgFlags.INVERSE) | $fg | Attributes.CM_RGB; |
| 200 |
} |
| 201 |
|
| 202 |
// Handle case where inverse was specified by only one of bg override or fg override was set, |
| 203 |
// resolving the other inverse color and setting the inverse flag if needed. |
| 204 |
if (this.result.fg & FgFlags.INVERSE) { |
| 205 |
if ($hasBg && !$hasFg) { |
| 206 |
// Resolve bg color type (default color has a different meaning in fg vs bg) |
| 207 |
if ((this.result.bg & Attributes.CM_MASK) === Attributes.CM_DEFAULT) { |
| 208 |
$fg = (this.result.fg & ~(Attributes.RGB_MASK | FgFlags.INVERSE | Attributes.CM_MASK)) | (($colors.background.rgba >> 8 & Attributes.RGB_MASK) & Attributes.RGB_MASK) | Attributes.CM_RGB; |
| 209 |
} else { |
| 210 |
$fg = (this.result.fg & ~(Attributes.RGB_MASK | FgFlags.INVERSE | Attributes.CM_MASK)) | this.result.bg & (Attributes.RGB_MASK | Attributes.CM_MASK); |
| 211 |
} |
| 212 |
$hasFg = true; |
| 213 |
} |
| 214 |
if (!$hasBg && $hasFg) { |
| 215 |
// Resolve bg color type (default color has a different meaning in fg vs bg) |
| 216 |
if ((this.result.fg & Attributes.CM_MASK) === Attributes.CM_DEFAULT) { |
| 217 |
$bg = (this.result.bg & ~(Attributes.RGB_MASK | Attributes.CM_MASK)) | (($colors.foreground.rgba >> 8 & Attributes.RGB_MASK) & Attributes.RGB_MASK) | Attributes.CM_RGB; |
| 218 |
} else { |
| 219 |
$bg = (this.result.bg & ~(Attributes.RGB_MASK | Attributes.CM_MASK)) | this.result.fg & (Attributes.RGB_MASK | Attributes.CM_MASK); |
| 220 |
} |
| 221 |
$hasBg = true; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// Release object |
| 226 |
$colors = undefined; |
| 227 |
|
| 228 |
// Use the override if it exists |
| 229 |
this.result.bg = $hasBg ? $bg : this.result.bg; |
| 230 |
this.result.fg = $hasFg ? $fg : this.result.fg; |
| 231 |
|
| 232 |
// Reset overrides variantOffset |
| 233 |
this.result.ext &= ~ExtFlags.VARIANT_OFFSET; |
| 234 |
this.result.ext |= ($variantOffset << 29) & ExtFlags.VARIANT_OFFSET; |
| 235 |
} |
| 236 |
} |