Skip to content

Commit a24c4d2

Browse files
committed
Allow theming with arbitrary CSS color strings
This mostly worked before, but clearColor made the assumption that the colors were always in #RRBBGG format. See the discussion here: xtermjs#1327 (comment) This changes the internal representation of a color so that we hold onto into an object containing the original css string along with an RGBA value encoded as a 32-bit uint. clearColor can then use that RGBA representation. Additionally, this deduplicates some code between ColorManager and Terminal. Terminal was generating the 256 ansi colors the same way ColorManager was, so this just makes Terminal use the same constant.
1 parent 4ab79c9 commit a24c4d2

12 files changed

+392
-399
lines changed

src/Terminal.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,38 +2233,6 @@ function wasMondifierKeyOnlyEvent(ev: KeyboardEvent): boolean {
22332233
* ANSI color code.
22342234
*/
22352235

2236-
// Colors 0-15 + 16-255
2237-
// Much thanks to TooTallNate for writing this.
2238-
const vcolors: number[][] = (function(): number[][] {
2239-
const result = DEFAULT_ANSI_COLORS.map(c => {
2240-
c = c.substring(1);
2241-
return [
2242-
parseInt(c.substring(0, 2), 16),
2243-
parseInt(c.substring(2, 4), 16),
2244-
parseInt(c.substring(4, 6), 16)
2245-
];
2246-
});
2247-
const r = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff];
2248-
2249-
// 16-231
2250-
for (let i = 0; i < 216; i++) {
2251-
result.push([
2252-
r[(i / 36) % 6 | 0],
2253-
r[(i / 6) % 6 | 0],
2254-
r[i % 6]
2255-
]);
2256-
}
2257-
2258-
// 232-255 (grey)
2259-
let c: number;
2260-
for (let i = 0; i < 24; i++) {
2261-
c = 8 + i * 10;
2262-
result.push([c, c, c]);
2263-
}
2264-
2265-
return result;
2266-
})();
2267-
22682236
const matchColorCache: {[colorRGBHash: number]: number} = {};
22692237

22702238
// http://stackoverflow.com/questions/1633828
@@ -2285,17 +2253,18 @@ function matchColor_(r1: number, g1: number, b1: number): number {
22852253
let ldiff = Infinity;
22862254
let li = -1;
22872255
let i = 0;
2288-
let c: number[];
2256+
let c: number;
22892257
let r2: number;
22902258
let g2: number;
22912259
let b2: number;
22922260
let diff: number;
22932261

2294-
for (; i < vcolors.length; i++) {
2295-
c = vcolors[i];
2296-
r2 = c[0];
2297-
g2 = c[1];
2298-
b2 = c[2];
2262+
for (; i < DEFAULT_ANSI_COLORS.length; i++) {
2263+
c = DEFAULT_ANSI_COLORS[i].rgba;
2264+
r2 = c >>> 24;
2265+
g2 = c >>> 16 & 0xFF;
2266+
b2 = c >>> 8 & 0xFF;
2267+
// assume that alpha is 0xFF
22992268

23002269
diff = matchColorDistance(r1, g1, b1, r2, g2, b2);
23012270

src/Viewport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class Viewport implements IViewport {
5050
}
5151

5252
public onThemeChanged(colors: IColorSet): void {
53-
this._viewportElement.style.backgroundColor = colors.background;
53+
this._viewportElement.style.backgroundColor = colors.background.css;
5454
}
5555

5656
/**

src/renderer/BaseRenderLayer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
179179
if (this._alpha) {
180180
this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
181181
} else {
182-
this._ctx.fillStyle = this._colors.background;
182+
this._ctx.fillStyle = this._colors.background.css;
183183
this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height);
184184
}
185185
}
@@ -199,7 +199,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
199199
width * this._scaledCellWidth,
200200
height * this._scaledCellHeight);
201201
} else {
202-
this._ctx.fillStyle = this._colors.background;
202+
this._ctx.fillStyle = this._colors.background.css;
203203
this._ctx.fillRect(
204204
x * this._scaledCellWidth,
205205
y * this._scaledCellHeight,
@@ -311,12 +311,12 @@ export abstract class BaseRenderLayer implements IRenderLayer {
311311
this._ctx.textBaseline = 'top';
312312

313313
if (fg === INVERTED_DEFAULT_COLOR) {
314-
this._ctx.fillStyle = this._colors.background;
314+
this._ctx.fillStyle = this._colors.background.css;
315315
} else if (fg < 256) {
316316
// 256 color support
317-
this._ctx.fillStyle = this._colors.ansi[fg];
317+
this._ctx.fillStyle = this._colors.ansi[fg].css;
318318
} else {
319-
this._ctx.fillStyle = this._colors.foreground;
319+
this._ctx.fillStyle = this._colors.foreground.css;
320320
}
321321

322322
this._clipRow(terminal, y);

0 commit comments

Comments
 (0)