convert.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 jab2lit = ([j]) => j;
  6. const rgb2lit = rgb => d3.hsl(d3.rgb(...rgb)).l || 0;
  7. const buildVectorData = (vector, toHue, toLightness, toHex) => {
  8. const sqMag = vectorDot(vector, vector);
  9. const mag = Math.sqrt(sqMag);
  10. const unit = vector.map(c => c / mag);
  11. const hue = toHue(vector);
  12. const lightness = toLightness(vector);
  13. const hex = toHex(vector);
  14. return { vector, sqMag, mag, unit, hue, lightness, hex };
  15. };
  16. const buildClusterData = (size, inertia, mu1, mu2, mu3, nu1, nu2, nu3, toHue, toLightness, toHex) => {
  17. const mu = buildVectorData([mu1, mu2, mu3], toHue, toLightness, toHex);
  18. const nu = [nu1, nu2, nu3];
  19. return {
  20. size, inertia, mu, nu,
  21. muNuAngle: rad2deg * Math.acos(vectorDot(mu.unit, nu) / vectorMag(nu)),
  22. };
  23. };
  24. const buildPokemonData = ([name, size, ...values]) => ({
  25. name,
  26. jab: {
  27. total: buildClusterData(size, ...values.slice(0, 7), jab2hue, jab2lit, jab2hex),
  28. clusters: [
  29. buildClusterData(...values.slice(7, 15), jab2hue, jab2lit, jab2hex),
  30. buildClusterData(...values.slice(15, 23), jab2hue, jab2lit, jab2hex),
  31. buildClusterData(...values.slice(23, 31), jab2hue, jab2lit, jab2hex),
  32. ],
  33. },
  34. rgb: {
  35. total: buildClusterData(size, ...values.slice(31, 38), rgb2hue, rgb2lit, rgb2hex),
  36. clusters: [
  37. buildClusterData(...values.slice(38, 46), rgb2hue, rgb2lit, rgb2hex),
  38. buildClusterData(...values.slice(46, 54), rgb2hue, rgb2lit, rgb2hex),
  39. buildClusterData(...values.slice(54, 62), rgb2hue, rgb2lit, rgb2hex),
  40. ],
  41. },
  42. });
  43. const pokemonData = databaseV2.map(row => buildPokemonData(row));