convert.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 buildClusterData = (size, inertia, mu1, mu2, mu3, nu1, nu2, nu3, toHue, toLightness, toChroma, toHex) => {
  20. const mu = buildVectorData([mu1, mu2, mu3], toHue, toLightness, toChroma, toHex);
  21. const nu = [nu1, nu2, nu3];
  22. return {
  23. size, inertia, mu, nu,
  24. muNuAngle: rad2deg * Math.acos(vectorDot(mu.unit, nu) / vectorMag(nu)),
  25. };
  26. };
  27. const buildPokemonData = ([name, size, ...values]) => ({
  28. name,
  29. jab: {
  30. total: buildClusterData(size, ...values.slice(0, 7), jab2hue, jab2lit, jab2chroma, jab2hex),
  31. clusters: [
  32. buildClusterData(...values.slice(7, 15), jab2hue, jab2lit, jab2chroma, jab2hex),
  33. buildClusterData(...values.slice(15, 23), jab2hue, jab2lit, jab2chroma, jab2hex),
  34. buildClusterData(...values.slice(23, 31), jab2hue, jab2lit, jab2chroma, jab2hex),
  35. ],
  36. },
  37. rgb: {
  38. total: buildClusterData(size, ...values.slice(31, 38), rgb2hue, rgb2lit, rgb2chroma, rgb2hex),
  39. clusters: [
  40. buildClusterData(...values.slice(38, 46), rgb2hue, rgb2lit, rgb2chroma, rgb2hex),
  41. buildClusterData(...values.slice(46, 54), rgb2hue, rgb2lit, rgb2chroma, rgb2hex),
  42. buildClusterData(...values.slice(54, 62), rgb2hue, rgb2lit, rgb2chroma, rgb2hex),
  43. ],
  44. },
  45. });
  46. const pokemonData = databaseV2.map(row => buildPokemonData(row));