convert.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const jab2hex = jab => d3.jab(...jab).formatHex();
  2. const rgb2hex = rgb => d3.rgb(...rgb).formatHex();
  3. const jab2hue = jab => d3.jch(d3.jab(...jab)).h || 0;
  4. const rgb2hue = rgb => d3.hsl(d3.rgb(...rgb)).h || 0;
  5. const jab2lit = ([j]) => j / 100;
  6. const rgb2lit = rgb => d3.hsl(d3.rgb(...rgb)).l || 0;
  7. const jab2chroma = jab => d3.jch(d3.jab(...jab)).C / 100;
  8. const rgb2chroma = rgb => d3.jch(d3.rgb(...rgb)).C / 100;
  9. const buildVectorData = (vector, toHue, toLightness, toChroma, toHex) => {
  10. const sqMag = vectorDot(vector, vector);
  11. const mag = Math.sqrt(sqMag);
  12. const unit = vector.map(c => c / mag);
  13. const hue = toHue(vector);
  14. const lightness = toLightness(vector);
  15. const chroma = toChroma(vector);
  16. const hex = toHex(vector);
  17. return { vector, sqMag, mag, unit, hue, lightness, chroma, hex };
  18. };
  19. const calcImportance = (chroma, lightness, proportion) => (
  20. // scale the final result by 100 just for clarity
  21. 100
  22. // ramp the proportion value such that
  23. // proportion > 85% => basically guaranteed to be most important, gets bonus
  24. // proportion < 15% => no way to be most important, goes negative
  25. // otherwise => depend approximately on sqrt(proportion)
  26. * ( Math.tanh(100 * (proportion - 0.85))
  27. + Math.tanh(100 * (proportion - 0.15))
  28. + Math.sqrt(proportion) )
  29. // consider the chroma based on the sqrt of its part of chroma + lightness
  30. * Math.sqrt(chroma / (chroma + lightness))
  31. // ramp the lightness value such that
  32. // lightness >> 50% => scale by 3
  33. // lightness << 50% => scale by 1
  34. // otherwise => slightly steep ramp
  35. * (2 + Math.tanh(10 * (lightness - 0.5)))
  36. );
  37. const buildClusterData = (size, inertia, mu1, mu2, mu3, nu1, nu2, nu3, totalSize, toHue, toLightness, toChroma, toHex) => {
  38. const mu = buildVectorData([mu1, mu2, mu3], toHue, toLightness, toChroma, toHex);
  39. const nu = [nu1, nu2, nu3];
  40. const muNuAngle = rad2deg * Math.acos(vectorDot(mu.unit, nu) / vectorMag(nu));
  41. const proportion = size / totalSize;
  42. const importance = calcImportance(mu.chroma, mu.lightness, proportion);
  43. return {
  44. size, inertia, mu, nu,
  45. muNuAngle, proportion, importance,
  46. };
  47. };
  48. const buildPokemonData = ([name, size, ...values]) => ({
  49. name,
  50. jab: {
  51. total: buildClusterData(size, ...values.slice(0, 7), size, jab2hue, jab2lit, jab2chroma, jab2hex),
  52. clusters: [
  53. buildClusterData(...values.slice(7, 15), size, jab2hue, jab2lit, jab2chroma, jab2hex),
  54. buildClusterData(...values.slice(15, 23), size, jab2hue, jab2lit, jab2chroma, jab2hex),
  55. buildClusterData(...values.slice(23, 31), size, jab2hue, jab2lit, jab2chroma, jab2hex),
  56. ],
  57. },
  58. rgb: {
  59. total: buildClusterData(size, ...values.slice(31, 38), size, rgb2hue, rgb2lit, rgb2chroma, rgb2hex),
  60. clusters: [
  61. buildClusterData(...values.slice(38, 46), size, rgb2hue, rgb2lit, rgb2chroma, rgb2hex),
  62. buildClusterData(...values.slice(46, 54), size, rgb2hue, rgb2lit, rgb2chroma, rgb2hex),
  63. buildClusterData(...values.slice(54, 62), size, rgb2hue, rgb2lit, rgb2chroma, rgb2hex),
  64. ],
  65. },
  66. });
  67. const pokemonData = databaseV2.map(row => buildPokemonData(row));