Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const backgroundColors = ["#1976d2", "#DC004E", "#9c27b0", "#4caf50"];
- const getRandomLightColors = () => {
- let letters = "BCDEF".split("");
- let color = "#";
- for (let i = 0; i < 6; i++) {
- color += letters[Math.floor(Math.random() * letters.length)];
- }
- return color;
- };
- const getDefaultColors = (function() {
- let counter = 0;
- return function() {
- return {
- avatarColor: backgroundColors[counter++],
- length: counter
- };
- };
- })();
- const getRandomBackgroundColor = (name) => {
- let { avatarColor, length } = getDefaultColors();
- if (length >= backgroundColors.length) {
- avatarColor = getRandomLightColors();
- }
- if (initialColorMap[initials]) {
- initialColorMap[initials].push(avatarColor);
- } else {
- initialColorMap[initials] = [avatarColor];
- }
- return avatarColor;
- };
- getRandomBackgroundColor("AZ"), console.log(initialColorMap);
- getRandomBackgroundColor("AZ"), console.log(initialColorMap);
- getRandomBackgroundColor("AZ"), console.log(initialColorMap);
- getRandomBackgroundColor("AZ"), console.log(initialColorMap);
- getRandomBackgroundColor("AZ"), console.log(initialColorMap);
- getRandomBackgroundColor("MH"), console.log(initialColorMap);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement