convert.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const jab2hex = jab => d3.jab(...jab).formatHex();
  2. const rgb2hex = rgb => d3.rgb(...rgb).formatHex();
  3. const jab2hue = ([, a, b]) => rad2deg * Math.atan2(b, a);
  4. const rgb2hue = rgb => d3.hsl(d3.rgb(...rgb)).h || 0;
  5. const hex2rgb = hex => {
  6. const { r, g, b } = d3.color(hex);
  7. return [r, g, b];
  8. };
  9. const buildVectorData = (vector, toHue, 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 hex = toHex(vector);
  15. return { vector, sqMag, mag, unit, hue, hex };
  16. };
  17. const buildClusterData = (size, inertia, mu1, mu2, mu3, nu1, nu2, nu3, toHue, toHex) => ({
  18. size, inertia,
  19. mu: buildVectorData([mu1, mu2, mu3], toHue, toHex),
  20. nu: [nu1, nu2, nu3],
  21. });
  22. const buildPokemonData = ([name, size, ...values]) => ({
  23. name,
  24. jab: {
  25. total: buildClusterData(size, ...values.slice(0, 7), jab2hue, jab2hex),
  26. clusters: [
  27. buildClusterData(...values.slice(7, 15), jab2hue, jab2hex),
  28. buildClusterData(...values.slice(15, 23), jab2hue, jab2hex),
  29. buildClusterData(...values.slice(23, 31), jab2hue, jab2hex),
  30. ],
  31. },
  32. rgb: {
  33. total: buildClusterData(size, ...values.slice(31, 38), rgb2hue, rgb2hex),
  34. clusters: [
  35. buildClusterData(...values.slice(38, 46), rgb2hue, rgb2hex),
  36. buildClusterData(...values.slice(46, 54), rgb2hue, rgb2hex),
  37. buildClusterData(...values.slice(54, 62), rgb2hue, rgb2hex),
  38. ],
  39. },
  40. });
  41. const pokemonData = databaseV2.map(row => buildPokemonData(row));