userColorsTest.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Run on the command line to test colors.
  2. // node userColorsTest.js > colorTest.html
  3. console.log(`<body style="background: #2d3748">`);
  4. for (var i = 0; i < 20; i++) {
  5. const element = generateElement(randomString(6));
  6. console.log(element);
  7. }
  8. console.log(`</body>`);
  9. function generateElement(string) {
  10. const bgColor = messageBubbleColorForString(string);
  11. const fgColor = textColorForString(string);
  12. return `<div style="background-color: ${bgColor}; color: ${fgColor}; padding: 30px;">${string}</div>`;
  13. }
  14. function randomString(length) {
  15. return Math.random().toString(36).substring(length);
  16. }
  17. function messageBubbleColorForString(str) {
  18. let hash = 0;
  19. for (let i = 0; i < str.length; i++) {
  20. // eslint-disable-next-line
  21. hash = str.charCodeAt(i) + ((hash << 5) - hash);
  22. }
  23. // Tweak these to adjust the result of the color
  24. const saturation = 25;
  25. const lightness = 45;
  26. const alpha = 0.3;
  27. const hue = parseInt(Math.abs(hash), 16) % 360;
  28. return `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`;
  29. }
  30. function textColorForString(str) {
  31. let hash = 0;
  32. for (let i = 0; i < str.length; i++) {
  33. // eslint-disable-next-line
  34. hash = str.charCodeAt(i) + ((hash << 5) - hash);
  35. }
  36. // Tweak these to adjust the result of the color
  37. const saturation = 80;
  38. const lightness = 80;
  39. const alpha = 0.8;
  40. const hue = parseInt(Math.abs(hash), 16) % 360;
  41. return `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`;
  42. }