Move color to common, work with IColor

IColor is needed for the minimum contrast ratio function currently.
This commit is contained in:
Daniel Imms
2022-05-09 14:00:39 -07:00
parent 63eb9a4555
commit 0c6877f46f
19 changed files with 46 additions and 51 deletions
@@ -11,7 +11,8 @@ import { fill } from 'common/TypedArrayUtils';
import { slice } from './TypedArray'; import { slice } from './TypedArray';
import { NULL_CELL_CODE, WHITESPACE_CELL_CODE, Attributes, FgFlags } from 'common/buffer/Constants'; import { NULL_CELL_CODE, WHITESPACE_CELL_CODE, Attributes, FgFlags } from 'common/buffer/Constants';
import { Terminal, IBufferLine } from 'xterm'; import { Terminal, IBufferLine } from 'xterm';
import { IColorSet, IColor } from 'browser/Types'; import { IColor } from 'common/Types';
import { IColorSet } from 'browser/Types';
import { IRenderDimensions } from 'browser/renderer/Types'; import { IRenderDimensions } from 'browser/renderer/Types';
import { AttributeData } from 'common/buffer/AttributeData'; import { AttributeData } from 'common/buffer/AttributeData';
@@ -8,7 +8,8 @@ import { IRenderModel, IWebGLVertexArrayObject, IWebGL2RenderingContext, ISelect
import { fill } from 'common/TypedArrayUtils'; import { fill } from 'common/TypedArrayUtils';
import { Attributes, FgFlags } from 'common/buffer/Constants'; import { Attributes, FgFlags } from 'common/buffer/Constants';
import { Terminal } from 'xterm'; import { Terminal } from 'xterm';
import { IColorSet, IColor } from 'browser/Types'; import { IColor } from 'common/Types';
import { IColorSet } from 'browser/Types';
import { IRenderDimensions } from 'browser/renderer/Types'; import { IRenderDimensions } from 'browser/renderer/Types';
import { RENDER_MODEL_BG_OFFSET, RENDER_MODEL_FG_OFFSET, RENDER_MODEL_INDICIES_PER_CELL } from './RenderModel'; import { RENDER_MODEL_BG_OFFSET, RENDER_MODEL_FG_OFFSET, RENDER_MODEL_INDICIES_PER_CELL } from './RenderModel';
@@ -6,7 +6,8 @@
import { ICharAtlasConfig } from './Types'; import { ICharAtlasConfig } from './Types';
import { Attributes } from 'common/buffer/Constants'; import { Attributes } from 'common/buffer/Constants';
import { Terminal, FontWeight } from 'xterm'; import { Terminal, FontWeight } from 'xterm';
import { IColorSet, IColor } from 'browser/Types'; import { IColorSet } from 'browser/Types';
import { IColor } from 'common/Types';
const NULL_COLOR: IColor = { const NULL_COLOR: IColor = {
css: '', css: '',
@@ -8,10 +8,10 @@ import { DIM_OPACITY, TEXT_BASELINE } from 'browser/renderer/atlas/Constants';
import { IRasterizedGlyph, IBoundingBox, IRasterizedGlyphSet } from '../Types'; import { IRasterizedGlyph, IBoundingBox, IRasterizedGlyphSet } from '../Types';
import { DEFAULT_COLOR, Attributes } from 'common/buffer/Constants'; import { DEFAULT_COLOR, Attributes } from 'common/buffer/Constants';
import { throwIfFalsy } from '../WebglUtils'; import { throwIfFalsy } from '../WebglUtils';
import { IColor } from 'browser/Types'; import { IColor } from 'common/Types';
import { IDisposable } from 'xterm'; import { IDisposable } from 'xterm';
import { AttributeData } from 'common/buffer/AttributeData'; import { AttributeData } from 'common/buffer/AttributeData';
import { channels, rgba } from 'browser/Color'; import { channels, rgba } from 'common/Color';
import { tryDrawCustomChar } from 'browser/renderer/CustomGlyphs'; import { tryDrawCustomChar } from 'browser/renderer/CustomGlyphs';
import { isPowerlineGlyph } from 'browser/renderer/RendererUtils'; import { isPowerlineGlyph } from 'browser/renderer/RendererUtils';
+2 -1
View File
@@ -3,7 +3,8 @@
* @license MIT * @license MIT
*/ */
import { IColor, IColorContrastCache } from 'browser/Types'; import { IColorContrastCache } from 'browser/Types';
import { IColor } from 'common/Types';
export class ColorContrastCache implements IColorContrastCache { export class ColorContrastCache implements IColorContrastCache {
private _color: { [bg: number]: { [fg: number]: IColor | null | undefined } | undefined } = {}; private _color: { [bg: number]: { [fg: number]: IColor | null | undefined } | undefined } = {};
+3 -3
View File
@@ -3,11 +3,11 @@
* @license MIT * @license MIT
*/ */
import { IColorManager, IColor, IColorSet, IColorContrastCache } from 'browser/Types'; import { IColorManager, IColorSet, IColorContrastCache } from 'browser/Types';
import { ITheme } from 'common/services/Services'; import { ITheme } from 'common/services/Services';
import { channels, color, css } from 'browser/Color'; import { channels, color, css } from 'common/Color';
import { ColorContrastCache } from 'browser/ColorContrastCache'; import { ColorContrastCache } from 'browser/ColorContrastCache';
import { ColorIndex } from 'common/Types'; import { ColorIndex, IColor } from 'common/Types';
interface IRestoreColorSet { interface IRestoreColorSet {
+1 -1
View File
@@ -52,7 +52,7 @@ import { MouseService } from 'browser/services/MouseService';
import { Linkifier2 } from 'browser/Linkifier2'; import { Linkifier2 } from 'browser/Linkifier2';
import { CoreBrowserService } from 'browser/services/CoreBrowserService'; import { CoreBrowserService } from 'browser/services/CoreBrowserService';
import { CoreTerminal } from 'common/CoreTerminal'; import { CoreTerminal } from 'common/CoreTerminal';
import { color, rgba } from 'browser/Color'; import { color, rgba } from 'common/Color';
import { CharacterJoinerService } from 'browser/services/CharacterJoinerService'; import { CharacterJoinerService } from 'browser/services/CharacterJoinerService';
import { toRgbString } from 'common/input/XParseColor'; import { toRgbString } from 'common/input/XParseColor';
import { BufferDecorationRenderer } from 'browser/Decorations/BufferDecorationRenderer'; import { BufferDecorationRenderer } from 'browser/Decorations/BufferDecorationRenderer';
+1 -7
View File
@@ -5,11 +5,10 @@
import { IDecorationOptions, IDecoration, IDisposable, IMarker, ISelectionPosition } from 'xterm'; import { IDecorationOptions, IDecoration, IDisposable, IMarker, ISelectionPosition } from 'xterm';
import { IEvent } from 'common/EventEmitter'; import { IEvent } from 'common/EventEmitter';
import { ICoreTerminal, CharData, ITerminalOptions } from 'common/Types'; import { ICoreTerminal, CharData, ITerminalOptions, IColor } from 'common/Types';
import { IMouseService, IRenderService } from './services/Services'; import { IMouseService, IRenderService } from './services/Services';
import { IBuffer } from 'common/buffer/Types'; import { IBuffer } from 'common/buffer/Types';
import { IFunctionIdentifier, IParams } from 'common/parser/Types'; import { IFunctionIdentifier, IParams } from 'common/parser/Types';
import { createDecorator } from 'common/services/ServiceRegistry';
export interface ITerminal extends IPublicTerminal, ICoreTerminal { export interface ITerminal extends IPublicTerminal, ICoreTerminal {
element: HTMLElement | undefined; element: HTMLElement | undefined;
@@ -113,11 +112,6 @@ export interface IColorManager {
onOptionsChange(key: string): void; onOptionsChange(key: string): void;
} }
export interface IColor {
css: string;
rgba: number; // 32-bit int with rgba in each byte
}
export interface IColorSet { export interface IColorSet {
foreground: IColor; foreground: IColor;
background: IColor; background: IColor;
+3 -3
View File
@@ -4,18 +4,18 @@
*/ */
import { IRenderDimensions, IRenderLayer } from 'browser/renderer/Types'; import { IRenderDimensions, IRenderLayer } from 'browser/renderer/Types';
import { ICellData } from 'common/Types'; import { ICellData, IColor } from 'common/Types';
import { DEFAULT_COLOR, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_CODE, Attributes } from 'common/buffer/Constants'; import { DEFAULT_COLOR, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_CODE, Attributes } from 'common/buffer/Constants';
import { IGlyphIdentifier } from 'browser/renderer/atlas/Types'; import { IGlyphIdentifier } from 'browser/renderer/atlas/Types';
import { DIM_OPACITY, INVERTED_DEFAULT_COLOR, TEXT_BASELINE } from 'browser/renderer/atlas/Constants'; import { DIM_OPACITY, INVERTED_DEFAULT_COLOR, TEXT_BASELINE } from 'browser/renderer/atlas/Constants';
import { BaseCharAtlas } from 'browser/renderer/atlas/BaseCharAtlas'; import { BaseCharAtlas } from 'browser/renderer/atlas/BaseCharAtlas';
import { acquireCharAtlas } from 'browser/renderer/atlas/CharAtlasCache'; import { acquireCharAtlas } from 'browser/renderer/atlas/CharAtlasCache';
import { AttributeData } from 'common/buffer/AttributeData'; import { AttributeData } from 'common/buffer/AttributeData';
import { IColorSet, IColor } from 'browser/Types'; import { IColorSet } from 'browser/Types';
import { CellData } from 'common/buffer/CellData'; import { CellData } from 'common/buffer/CellData';
import { IBufferService, IOptionsService } from 'common/services/Services'; import { IBufferService, IOptionsService } from 'common/services/Services';
import { isPowerlineGlyph, throwIfFalsy } from 'browser/renderer/RendererUtils'; import { isPowerlineGlyph, throwIfFalsy } from 'browser/renderer/RendererUtils';
import { channels, color, rgba } from 'browser/Color'; import { channels, color, rgba } from 'common/Color';
import { removeElementFromParent } from 'browser/Dom'; import { removeElementFromParent } from 'browser/Dom';
import { tryDrawCustomChar } from 'browser/renderer/CustomGlyphs'; import { tryDrawCustomChar } from 'browser/renderer/CustomGlyphs';
@@ -9,9 +9,9 @@ import { BaseCharAtlas } from 'browser/renderer/atlas/BaseCharAtlas';
import { DEFAULT_ANSI_COLORS } from 'browser/ColorManager'; import { DEFAULT_ANSI_COLORS } from 'browser/ColorManager';
import { LRUMap } from 'browser/renderer/atlas/LRUMap'; import { LRUMap } from 'browser/renderer/atlas/LRUMap';
import { isFirefox, isSafari } from 'common/Platform'; import { isFirefox, isSafari } from 'common/Platform';
import { IColor } from 'browser/Types'; import { IColor } from 'common/Types';
import { throwIfFalsy } from 'browser/renderer/RendererUtils'; import { throwIfFalsy } from 'browser/renderer/RendererUtils';
import { color } from 'browser/Color'; import { color } from 'common/Color';
// In practice we're probably never going to exhaust a texture this large. For debugging purposes, // In practice we're probably never going to exhaust a texture this large. For debugging purposes,
// however, it can be useful to set this to a really tiny value, to verify that LRU eviction works. // however, it can be useful to set this to a really tiny value, to verify that LRU eviction works.
+1 -1
View File
@@ -11,7 +11,7 @@ import { IColorSet, ILinkifierEvent, ILinkifier, ILinkifier2 } from 'browser/Typ
import { ICharSizeService } from 'browser/services/Services'; import { ICharSizeService } from 'browser/services/Services';
import { IOptionsService, IBufferService, IInstantiationService, IDecorationService } from 'common/services/Services'; import { IOptionsService, IBufferService, IInstantiationService, IDecorationService } from 'common/services/Services';
import { EventEmitter, IEvent } from 'common/EventEmitter'; import { EventEmitter, IEvent } from 'common/EventEmitter';
import { color } from 'browser/Color'; import { color } from 'common/Color';
import { removeElementFromParent } from 'browser/Dom'; import { removeElementFromParent } from 'browser/Dom';
const TERMINAL_CLASS_PREFIX = 'xterm-dom-renderer-owner-'; const TERMINAL_CLASS_PREFIX = 'xterm-dom-renderer-owner-';
@@ -11,7 +11,7 @@ import { BufferLine, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
import { IBufferLine } from 'common/Types'; import { IBufferLine } from 'common/Types';
import { CellData } from 'common/buffer/CellData'; import { CellData } from 'common/buffer/CellData';
import { MockCoreService, MockDecorationService, MockOptionsService } from 'common/TestUtils.test'; import { MockCoreService, MockDecorationService, MockOptionsService } from 'common/TestUtils.test';
import { css } from 'browser/Color'; import { css } from 'common/Color';
import { MockCharacterJoinerService } from 'browser/TestUtils.test'; import { MockCharacterJoinerService } from 'browser/TestUtils.test';
describe('DomRendererRowFactory', () => { describe('DomRendererRowFactory', () => {
@@ -3,13 +3,13 @@
* @license MIT * @license MIT
*/ */
import { IBufferLine, ICellData } from 'common/Types'; import { IBufferLine, ICellData, IColor } from 'common/Types';
import { INVERTED_DEFAULT_COLOR } from 'browser/renderer/atlas/Constants'; import { INVERTED_DEFAULT_COLOR } from 'browser/renderer/atlas/Constants';
import { NULL_CELL_CODE, WHITESPACE_CELL_CHAR, Attributes } from 'common/buffer/Constants'; import { NULL_CELL_CODE, WHITESPACE_CELL_CHAR, Attributes } from 'common/buffer/Constants';
import { CellData } from 'common/buffer/CellData'; import { CellData } from 'common/buffer/CellData';
import { ICoreService, IDecorationService, IOptionsService } from 'common/services/Services'; import { ICoreService, IDecorationService, IOptionsService } from 'common/services/Services';
import { color, rgba } from 'browser/Color'; import { color, rgba } from 'common/Color';
import { IColorSet, IColor } from 'browser/Types'; import { IColorSet } from 'browser/Types';
import { ICharacterJoinerService } from 'browser/services/Services'; import { ICharacterJoinerService } from 'browser/services/Services';
import { JoinedCellData } from 'browser/services/CharacterJoinerService'; import { JoinedCellData } from 'browser/services/CharacterJoinerService';
import { isPowerlineGlyph } from 'browser/renderer/RendererUtils'; import { isPowerlineGlyph } from 'browser/renderer/RendererUtils';
@@ -181,11 +181,11 @@ export class DomRendererRowFactory {
if (x >= xmin && x < xmax) { if (x >= xmin && x < xmax) {
if (d.backgroundColorRGB) { if (d.backgroundColorRGB) {
bgColorMode = Attributes.CM_RGB; bgColorMode = Attributes.CM_RGB;
bg = (d.backgroundColorRGB[0] << 16) | (d.backgroundColorRGB[1]) << 8 | d.backgroundColorRGB[2]; bg = d.backgroundColorRGB.rgba >> 8;
} }
if (d.foregroundColorRGB) { if (d.foregroundColorRGB) {
fgColorMode = Attributes.CM_RGB; fgColorMode = Attributes.CM_RGB;
fg = (d.foregroundColorRGB[0] << 16) | (d.foregroundColorRGB[1]) << 8 | d.foregroundColorRGB[2]; fg = d.foregroundColorRGB.rgba >> 8;
} }
} }
} }
@@ -4,7 +4,7 @@
*/ */
import { assert } from 'chai'; import { assert } from 'chai';
import { channels, color, css, rgb, rgba, toPaddedHex, contrastRatio } from 'browser/Color'; import { channels, color, css, rgb, rgba, toPaddedHex, contrastRatio } from 'common/Color';
describe('Color', () => { describe('Color', () => {
+1 -2
View File
@@ -3,8 +3,7 @@
* @license MIT * @license MIT
*/ */
import { IColor } from 'browser/Types'; import { IColor, IColorRGB } from 'common/Types';
import { IColorRGB } from 'common/Types';
/** /**
* Helper functions where the source type is "channels" (individual color channels as numbers). * Helper functions where the source type is "channels" (individual color channels as numbers).
+1 -1
View File
@@ -162,7 +162,7 @@ export class MockUnicodeService implements IUnicodeService {
export class MockDecorationService implements IDecorationService { export class MockDecorationService implements IDecorationService {
public serviceBrand: any; public serviceBrand: any;
public get decorations(): IterableIterator<IInternalDecoration> { return [].values(); }; public get decorations(): IterableIterator<IInternalDecoration> { return [].values(); }
public onDecorationRegistered = new EventEmitter<IInternalDecoration>().event; public onDecorationRegistered = new EventEmitter<IInternalDecoration>().event;
public onDecorationRemoved = new EventEmitter<IInternalDecoration>().event; public onDecorationRemoved = new EventEmitter<IInternalDecoration>().event;
public registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined { return undefined; } public registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined { return undefined; }
+5
View File
@@ -102,6 +102,11 @@ export interface ICharset {
} }
export type CharData = [number, string, number, number]; export type CharData = [number, string, number, number];
export interface IColor {
css: string;
rgba: number; // 32-bit int with rgba in each byte
}
export type IColorRGB = [number, number, number]; export type IColorRGB = [number, number, number];
export interface IExtendedAttrs { export interface IExtendedAttrs {
+8 -15
View File
@@ -3,10 +3,11 @@
* @license MIT * @license MIT
*/ */
import { css } from 'common/Color';
import { EventEmitter } from 'common/EventEmitter'; import { EventEmitter } from 'common/EventEmitter';
import { Disposable } from 'common/Lifecycle'; import { Disposable } from 'common/Lifecycle';
import { IDecorationService, IInternalDecoration } from 'common/services/Services'; import { IDecorationService, IInternalDecoration } from 'common/services/Services';
import { IColorRGB } from 'common/Types'; import { IColor } from 'common/Types';
import { IDecorationOptions, IDecoration, IMarker, IEvent } from 'xterm'; import { IDecorationOptions, IDecoration, IMarker, IEvent } from 'xterm';
export class DecorationService extends Disposable implements IDecorationService { export class DecorationService extends Disposable implements IDecorationService {
@@ -75,11 +76,11 @@ class Decoration extends Disposable implements IInternalDecoration {
public readonly onDispose = this._onDispose.event; public readonly onDispose = this._onDispose.event;
// TODO: React to changes on options // TODO: React to changes on options
private _cachedBg: IColorRGB | undefined | null = null; private _cachedBg: IColor | undefined | null = null;
public get backgroundColorRGB(): IColorRGB | undefined { public get backgroundColorRGB(): IColor | undefined {
if (this._cachedBg === null) { if (this._cachedBg === null) {
if (this.options.backgroundColor) { if (this.options.backgroundColor) {
this._cachedBg = toColorRGB(this.options.backgroundColor); this._cachedBg = css.toColor(this.options.backgroundColor);
} else { } else {
this._cachedBg = undefined; this._cachedBg = undefined;
} }
@@ -88,11 +89,11 @@ class Decoration extends Disposable implements IInternalDecoration {
} }
// TODO: React to changes on options // TODO: React to changes on options
private _cachedFg: IColorRGB | undefined | null = null; private _cachedFg: IColor | undefined | null = null;
public get foregroundColorRGB(): IColorRGB | undefined { public get foregroundColorRGB(): IColor | undefined {
if (this._cachedFg === null) { if (this._cachedFg === null) {
if (this.options.foregroundColor) { if (this.options.foregroundColor) {
this._cachedFg = toColorRGB(this.options.foregroundColor); this._cachedFg = css.toColor(this.options.foregroundColor);
} else { } else {
this._cachedFg = undefined; this._cachedFg = undefined;
} }
@@ -119,11 +120,3 @@ class Decoration extends Disposable implements IInternalDecoration {
super.dispose(); super.dispose();
} }
} }
function toColorRGB(css: string): IColorRGB {
// #rrggbb
if (css.length === 7) {
return [parseInt(css.slice(1, 3), 16), parseInt(css.slice(3, 5), 16), parseInt(css.slice(5, 7), 16)];
}
throw new Error('css.toColor: Unsupported css format');
}
+3 -3
View File
@@ -5,7 +5,7 @@
import { IEvent, IEventEmitter } from 'common/EventEmitter'; import { IEvent, IEventEmitter } from 'common/EventEmitter';
import { IBuffer, IBufferSet } from 'common/buffer/Types'; import { IBuffer, IBufferSet } from 'common/buffer/Types';
import { IDecPrivateModes, ICoreMouseEvent, CoreMouseEncoding, ICoreMouseProtocol, CoreMouseEventType, ICharset, IWindowOptions, IModes, IAttributeData, ScrollSource, IDisposable, IColorRGB } from 'common/Types'; import { IDecPrivateModes, ICoreMouseEvent, CoreMouseEncoding, ICoreMouseProtocol, CoreMouseEventType, ICharset, IWindowOptions, IModes, IAttributeData, ScrollSource, IDisposable, IColorRGB, IColor } from 'common/Types';
import { createDecorator } from 'common/services/ServiceRegistry'; import { createDecorator } from 'common/services/ServiceRegistry';
import { IDecorationOptions, IDecoration } from 'xterm'; import { IDecorationOptions, IDecoration } from 'xterm';
@@ -313,7 +313,7 @@ export interface IDecorationService extends IDisposable {
} }
export interface IInternalDecoration extends IDecoration { export interface IInternalDecoration extends IDecoration {
readonly options: IDecorationOptions; readonly options: IDecorationOptions;
readonly backgroundColorRGB: IColorRGB | undefined; readonly backgroundColorRGB: IColor | undefined;
readonly foregroundColorRGB: IColorRGB | undefined; readonly foregroundColorRGB: IColor | undefined;
readonly onRenderEmitter: IEventEmitter<HTMLElement>; readonly onRenderEmitter: IEventEmitter<HTMLElement>;
} }