You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
1.1 KiB

  1. const DIGIT_CHARACTERS = [
  2. '0',
  3. '1',
  4. '2',
  5. '3',
  6. '4',
  7. '5',
  8. '6',
  9. '7',
  10. '8',
  11. '9',
  12. 'A',
  13. 'B',
  14. 'C',
  15. 'D',
  16. 'E',
  17. 'F',
  18. 'G',
  19. 'H',
  20. 'I',
  21. 'J',
  22. 'K',
  23. 'L',
  24. 'M',
  25. 'N',
  26. 'O',
  27. 'P',
  28. 'Q',
  29. 'R',
  30. 'S',
  31. 'T',
  32. 'U',
  33. 'V',
  34. 'W',
  35. 'X',
  36. 'Y',
  37. 'Z',
  38. 'a',
  39. 'b',
  40. 'c',
  41. 'd',
  42. 'e',
  43. 'f',
  44. 'g',
  45. 'h',
  46. 'i',
  47. 'j',
  48. 'k',
  49. 'l',
  50. 'm',
  51. 'n',
  52. 'o',
  53. 'p',
  54. 'q',
  55. 'r',
  56. 's',
  57. 't',
  58. 'u',
  59. 'v',
  60. 'w',
  61. 'x',
  62. 'y',
  63. 'z',
  64. '#',
  65. '$',
  66. '%',
  67. '*',
  68. '+',
  69. ',',
  70. '-',
  71. '.',
  72. ':',
  73. ';',
  74. '=',
  75. '?',
  76. '@',
  77. '[',
  78. ']',
  79. '^',
  80. '_',
  81. '{',
  82. '|',
  83. '}',
  84. '~',
  85. ];
  86. export const decode83 = (str) => {
  87. let value = 0;
  88. let c, digit;
  89. for (let i = 0; i < str.length; i++) {
  90. c = str[i];
  91. digit = DIGIT_CHARACTERS.indexOf(c);
  92. value = value * 83 + digit;
  93. }
  94. return value;
  95. };
  96. export const intToRGB = int => ({
  97. r: Math.max(0, (int >> 16)),
  98. g: Math.max(0, (int >> 8) & 255),
  99. b: Math.max(0, (int & 255)),
  100. });
  101. export const getAverageFromBlurhash = blurhash => {
  102. if (!blurhash) {
  103. return null;
  104. }
  105. return intToRGB(decode83(blurhash.slice(2, 6)));
  106. };