Skip to content

Instantly share code, notes, and snippets.

@tadite
Created October 4, 2020 18:27
Show Gist options
  • Save tadite/a553ef3fe24ff3a9fc2f7546b2c00446 to your computer and use it in GitHub Desktop.
Save tadite/a553ef3fe24ff3a9fc2f7546b2c00446 to your computer and use it in GitHub Desktop.
export const getUniqueColorsForShowIds = (showIds: number[]) => {
console.log("colors.length "+colors.length);
console.log("showIds "+showIds);
// get list of indicies in color array
const listOfIndicies: number[] = showIds.map(id => {
// using modulus operator, so if id = 103 and color.length = 100, then colorIndex will be 3
const colorIndex = id % colors.length;
return colorIndex;
});
console.log("listOfIndicies "+listOfIndicies);
const usedIndicies: { [key: number]: boolean } = {};
// trying to get unique numbers if have dublicates in listOfIndicies
const uniqueIndices = listOfIndicies.map(index => {
// counting the number of steps done while finding unique index
var findUniqueIndexStepsCount = 0;
var uniqueIndex = index;
// stopping when tried all available color indices
while (findUniqueIndexStepsCount < colors.length) {
console.log(index+": trying to find unique replacement, testing - "+uniqueIndex);
if (!usedIndicies.hasOwnProperty(uniqueIndex)) {
break;
} else {
findUniqueIndexStepsCount++;
// using modulus operator, so if colors.length = 3 and index = 2
// will be trying to find unique index in cycle stepping like 2->3->0->1->2
uniqueIndex = (uniqueIndex + 1) % colors.length;
}
}
console.log(index+ ": found unique replacement "+uniqueIndex);
usedIndicies[uniqueIndex] = true;
return uniqueIndex;
});
console.log("uniqueIndices "+Object.values(uniqueIndices));
const uniqueColors = uniqueIndices.map(num => colors[num]);
return uniqueColors;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment