Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const backgroundColors = ["#1976d2", "#DC004E", "#9c27b0", "#4caf50"];
  2.  
  3. const getRandomLightColors = () => {
  4.   let letters = "BCDEF".split("");
  5.   let color = "#";
  6.   for (let i = 0; i < 6; i++) {
  7.     color += letters[Math.floor(Math.random() * letters.length)];
  8.   }
  9.   return color;
  10. };
  11.  
  12. const getDefaultColors = (function() {
  13.   let counter = 0;
  14.   return function() {
  15.     return {
  16.       avatarColor: backgroundColors[counter++],
  17.       length: counter
  18.     };
  19.   };
  20. })();
  21.  
  22. const getRandomBackgroundColor = (name) => {
  23.   let { avatarColor, length } = getDefaultColors();
  24.   if (length >= backgroundColors.length) {
  25.     avatarColor = getRandomLightColors();
  26.   }
  27.   if (initialColorMap[initials]) {
  28.     initialColorMap[initials].push(avatarColor);
  29.   } else {
  30.     initialColorMap[initials] = [avatarColor];
  31.   }
  32.   return avatarColor;
  33. };
  34. getRandomBackgroundColor("AZ"), console.log(initialColorMap);
  35. getRandomBackgroundColor("AZ"), console.log(initialColorMap);
  36. getRandomBackgroundColor("AZ"), console.log(initialColorMap);
  37. getRandomBackgroundColor("AZ"), console.log(initialColorMap);
  38. getRandomBackgroundColor("AZ"), console.log(initialColorMap);
  39. getRandomBackgroundColor("MH"), console.log(initialColorMap);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement