export function applyNextIndicatorColors(config: IndicatorConfig, usedColors: readonly string[]): IndicatorConfig {
const nextConfig = cloneDeep(config);
const reservedColors = new Set(usedColors.map(normalizeColor));
const colorizableSeriesCount = nextConfig.series.filter(shouldUsePaletteColor).length;
const startIndex = getNextFreePaletteIndex(reservedColors);
let colorizableSeriesIndex = 0;
nextConfig.series = nextConfig.series.map((serie) => {
if (!shouldUsePaletteColor(serie)) {
return serie;
}
const color =
colorizableSeriesCount > 1
? getGroupedPaletteColor(reservedColors, startIndex, colorizableSeriesIndex, colorizableSeriesCount)
: getNextPaletteColor(reservedColors);
colorizableSeriesIndex += 1;
reservedColors.add(normalizeColor(color));
return {
...serie,
seriesOptions: {
...serie.seriesOptions,
color,
},
};
});
return nextConfig;
}