Trace View: Use number instead of array for last color index (#74115)

* Use integer instead of array for last color index

* Update naming
This commit is contained in:
Joey
2023-08-31 16:27:09 +01:00
committed by GitHub
parent ad00200a39
commit a8fcbe75ea

View File

@ -33,12 +33,14 @@ class ColorGenerator {
colorsHex: string[];
colorsRgb: Array<[number, number, number]>;
cache: Map<string, number>;
prevColorIndex: number | undefined;
constructor(colorsHex: string[], theme: GrafanaTheme2) {
const filteredColors = getFilteredColors(colorsHex, theme);
this.colorsHex = filteredColors;
this.colorsRgb = filteredColors.map(strToRgb);
this.cache = new Map();
this.prevColorIndex = undefined;
}
_getColorIndex(key: string): number {
@ -47,15 +49,14 @@ class ColorGenerator {
const hash = this.hashCode(key.toLowerCase());
i = Math.abs(hash % this.colorsHex.length);
const prevCacheItem = Array.from(this.cache).pop();
if (prevCacheItem && prevCacheItem[1]) {
if (this.prevColorIndex !== undefined) {
// disallow a color that is the same as the previous color
if (prevCacheItem[1] === i) {
if (this.prevColorIndex === i) {
i = this.getNextIndex(i);
}
// disallow a color that looks very similar to the previous color
const prevColor = this.colorsHex[prevCacheItem[1]];
const prevColor = this.colorsHex[this.prevColorIndex];
if (tinycolor.readability(prevColor, this.colorsHex[i]) < 1.5) {
let newIndex = i;
for (let j = 0; j < this.colorsHex.length; j++) {
@ -70,6 +71,7 @@ class ColorGenerator {
}
this.cache.set(key, i);
this.prevColorIndex = i;
}
return i;
}