render.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const getSprite = (() => {
  2. const stripForm = ["flabebe", "floette", "florges", "vivillon", "basculin", "furfrou", "magearna"];
  3. return pokemon => {
  4. pokemon = pokemon
  5. .replace("-alola", "-alolan")
  6. .replace("-galar", "-galarian")
  7. .replace("darmanitan-galarian", "darmanitan-galarian-standard");
  8. if (stripForm.find(s => pokemon.includes(s))) {
  9. pokemon = pokemon.replace(/-.*$/, "");
  10. }
  11. return `https://img.pokemondb.net/sprites/sword-shield/icon/${pokemon}.png`;
  12. }
  13. })();
  14. const renderStatPair = (lbl, val, valueClass = "value") => `
  15. <span class="pkmn-tile_label">${lbl}</span>
  16. <span class="pkmn-tile_${valueClass}">${val}</span>
  17. `;
  18. const renderStatRow = lines => `
  19. <div class="pkmn-tile_row">
  20. ${lines.join("\n")}
  21. </div>
  22. `;
  23. const renderPokemonTileCluster = (area, { mu, nu, proportion }, scores) => {
  24. const textColor = getContrastingTextColor(mu.hex);
  25. return `
  26. <div
  27. class="pkmn-tile_stats"
  28. style="grid-area: ${area}; color: ${textColor}; background-color: ${mu.hex};"
  29. >
  30. <div class="pkmn-tile_row" style="justify-content: center;">
  31. ${(100 * proportion).toFixed(2)}% ${mu.hex}
  32. </div>
  33. <div class="toggle-on">
  34. ${
  35. [ ["α =", scores.alpha.toFixed(3)],
  36. ["σ =", scores.sigma.toFixed(3)],
  37. ["Θ =", scores.bigTheta.toFixed(3)],
  38. ["θ =", scores.theta.toFixed(3) + "°"],
  39. ["ϕ =", scores.phi.toFixed(3) + "°"],
  40. ["δ =", scores.delta.toFixed(3)],
  41. ["M =", scores.manhattan.toFixed(3)],
  42. ["Ч =", scores.ch.toFixed(3)],
  43. ["ℓ =", scores.lightnessDiff.toFixed(3)],
  44. ]
  45. .map(([lbl, val]) => renderStatPair(lbl, val))
  46. .map(ls => renderStatRow([ls]))
  47. .join("\n")
  48. }
  49. <hr style="width: 80%; color: ${textColor}"/>
  50. ${
  51. [ ["μ =", `(${mu.vector[0].toFixed(2)},`],
  52. ["", mu.vector[1].toFixed(2) + ","],
  53. ["", mu.vector[2].toFixed(2) + ")"],
  54. ["ν =", `(${nu[0].toFixed(2)},`],
  55. ["", nu[1].toFixed(2) + ","],
  56. ["", nu[2].toFixed(2) + ")"],
  57. ["I =", scores.inertia.toFixed(2)],
  58. ["L =", scores.lightness.toFixed(2)],
  59. ["C =", scores.chroma.toFixed(2)],
  60. ["β = ", scores.importance.toFixed(2)],
  61. ["V =", scores.muNuAngle.toFixed(2) + "°"],
  62. ["N =", scores.size],
  63. ]
  64. .map(([lbl, val]) => renderStatPair(lbl, val))
  65. .map(ls => renderStatRow([ls]))
  66. .join("\n")
  67. }
  68. </div>
  69. </div>
  70. `;
  71. };
  72. const clusterToggles = {};
  73. const renderPokemonTile = (kind, name, { total: { mu, nu, size }, clusters }, scores, bestClusterIndex) => {
  74. const clusterToggleId = `${name}-${kind}-pkmn-expand-toggle`;
  75. const textColor = getContrastingTextColor(mu.hex);
  76. return `
  77. <div class="pkmn-tile toggle-box">
  78. <input
  79. autocomplete="off"
  80. type="checkbox"
  81. ${clusterToggles?.[clusterToggleId] ? "checked" : ""}
  82. id="${clusterToggleId}"
  83. onchange="clusterToggles['${clusterToggleId}'] = event.target.checked"
  84. class="toggle-button"
  85. role="button"
  86. >
  87. <label for="${clusterToggleId}" style="grid-area: togg; text-align: center; align-self: start;">
  88. <div class="toggle-off">►</div>
  89. <div class="toggle-on">▼</div>
  90. </label>
  91. <img style="grid-area: icon" src="${getSprite(name)}" />
  92. <span style="grid-area: name">
  93. ${name.split("-").map(part => part.charAt(0).toUpperCase() + part.substr(1)).join(" ")}
  94. </span>
  95. <div
  96. class="pkmn-tile_stats"
  97. style="grid-area: totl; color: ${textColor}; background-color: ${mu.hex};"
  98. >
  99. <div class="pkmn-tile_row" style="justify-content: center;">
  100. ${mu.hex}
  101. </div>
  102. <div class="toggle-on">
  103. ${ renderStatRow(
  104. [ ["α =", scores.total.alpha.toFixed(3)],
  105. ["σ =", scores.total.sigma.toFixed(3)],
  106. ["Θ =", scores.total.bigTheta.toFixed(3)],
  107. ].map(([lbl, val]) => renderStatPair(lbl, val))
  108. )}
  109. ${ renderStatRow(
  110. [ ["θ =", scores.total.theta.toFixed(3) + "°"],
  111. ["ϕ =", scores.total.phi.toFixed(3) + "°"],
  112. ["δ =", scores.total.delta.toFixed(3)],
  113. ].map(([lbl, val]) => renderStatPair(lbl, val))
  114. )}
  115. ${ renderStatRow(
  116. [ ["M =", scores.total.manhattan.toFixed(3)],
  117. ["Ч =", scores.total.ch.toFixed(3)],
  118. ["ℓ =", scores.total.lightnessDiff.toFixed(3)],
  119. ].map(([lbl, val]) => renderStatPair(lbl, val))
  120. )}
  121. <hr style="width: 80%; color: ${textColor}"/>
  122. ${ renderStatRow(
  123. [ ["μ =", `(${mu.vector.map(c => c.toFixed(2)).join(", ")})`, "vector"],
  124. ["ν =", `(${nu.map(c => c.toFixed(2)).join(", ")})`, "vector"],
  125. ].map(([lbl, val, cls]) => renderStatPair(lbl, val, cls))
  126. )}
  127. ${ renderStatRow(
  128. [ ["I =", scores.total.inertia.toFixed(2)],
  129. ["V =", scores.total.muNuAngle.toFixed(2) + "°"],
  130. ["N =", size],
  131. ["L =", scores.total.lightness.toFixed(2)],
  132. ["C =", scores.total.chroma.toFixed(2)],
  133. ].map(([lbl, val, cls]) => renderStatPair(lbl, val, cls))
  134. )}
  135. ${ bestClusterIndex < 0 ? "" : `<hr style="width: 80%; color: ${textColor}"/>` }
  136. ${ renderStatRow([
  137. `<span class="pkmn-tile_indicator">${bestClusterIndex === 0 ? "▼" : ""}</span>`,
  138. `<span class="pkmn-tile_indicator">${bestClusterIndex === 1 ? "▼" : ""}</span>`,
  139. `<span class="pkmn-tile_indicator">${bestClusterIndex === 2 ? "▼" : ""}</span>`,
  140. ])}
  141. </div>
  142. </div>
  143. ${clusters.map((c, i) => renderPokemonTileCluster(`cls${i+1}`, c, scores.clusters[i])).join("\n")}
  144. </div>
  145. `
  146. };