b806aea1516caa470b8c2aff2c15e410ba163972a194e8a1e7bd6ae5db0859b9a7185bbc28cece6dfd58294b8b022106b188e1db3d2e92504c028fb6bae35f 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { Emitter } from '../../../base/common/event.js';
  6. import { Disposable, markAsSingleton } from '../../../base/common/lifecycle.js';
  7. import { RGBA8 } from '../core/rgba.js';
  8. import { TokenizationRegistry } from '../languages.js';
  9. export class MinimapTokensColorTracker extends Disposable {
  10. constructor() {
  11. super();
  12. this._onDidChange = new Emitter();
  13. this.onDidChange = this._onDidChange.event;
  14. this._updateColorMap();
  15. this._register(TokenizationRegistry.onDidChange(e => {
  16. if (e.changedColorMap) {
  17. this._updateColorMap();
  18. }
  19. }));
  20. }
  21. static getInstance() {
  22. if (!this._INSTANCE) {
  23. this._INSTANCE = markAsSingleton(new MinimapTokensColorTracker());
  24. }
  25. return this._INSTANCE;
  26. }
  27. _updateColorMap() {
  28. const colorMap = TokenizationRegistry.getColorMap();
  29. if (!colorMap) {
  30. this._colors = [RGBA8.Empty];
  31. this._backgroundIsLight = true;
  32. return;
  33. }
  34. this._colors = [RGBA8.Empty];
  35. for (let colorId = 1; colorId < colorMap.length; colorId++) {
  36. const source = colorMap[colorId].rgba;
  37. // Use a VM friendly data-type
  38. this._colors[colorId] = new RGBA8(source.r, source.g, source.b, Math.round(source.a * 255));
  39. }
  40. const backgroundLuminosity = colorMap[2 /* ColorId.DefaultBackground */].getRelativeLuminance();
  41. this._backgroundIsLight = backgroundLuminosity >= 0.5;
  42. this._onDidChange.fire(undefined);
  43. }
  44. getColor(colorId) {
  45. if (colorId < 1 || colorId >= this._colors.length) {
  46. // background color (basically invisible)
  47. colorId = 2 /* ColorId.DefaultBackground */;
  48. }
  49. return this._colors[colorId];
  50. }
  51. backgroundIsLight() {
  52. return this._backgroundIsLight;
  53. }
  54. }
  55. MinimapTokensColorTracker._INSTANCE = null;