jeudi 3 décembre 2020

Make sense color for word

We have A-z chars and abcdef0-9 color.

I need get always same color on same word, (apple always red, car always green, cars - always orange) - even if word have 1 or 3 letter, I wanna see same color every time. In my function I have random function if no letters to Math color.

Please help me upgrade function and replace Math Random for something like get letter index in alphabet divide on colors char position or something else

export function MakeSenseColor(str) {
  if(!str && typeof str !== 'string') return '';

  const chars1 = 'abc6789';
  const chars2 = 'abcdef0123456789';

  const Digits = 'qйwцeуrкtеyнuгiшoщpзaхsъdфfыgвhаjпkрlоzлxдcжvэbяnчmсмитьбю1234567890';
  const splittedStr = str.split('').map((s,i,arr) => {
    const charPos = s && (Digits.split('')).indexOf(`${s}`.toLowerCase());

    if(!charPos || charPos === -1) return '0';

    if (charPos >= 0 ) {
      const char = (charPos * chars2.length) / Digits.length
      const charColor = (char * 6) / arr.length;

      return chars2[parseInt(charColor)];
    }

    return '0';
  });

  let color = '#';

  for (let i = 0; color.length < 7; i++) {
    const add = splittedStr[i%6] || chars1.charAt(Math.floor(Math.random() * chars1.length))

    color = color + add;
  }

  return color;
}



Aucun commentaire:

Enregistrer un commentaire