math.js 362 B

123456789101112
  1. // Vector Math
  2. const vectorDot = (u, v) => u.map((x, i) => x * v[i]).reduce((x, y) => x + y);
  3. const vectorMag = (v) => Math.sqrt(vectorDot(v, v));
  4. // Angle Math
  5. const rad2deg = 180 / Math.PI;
  6. // Contrast
  7. const getContrastingTextColor = (hex) => {
  8. const { r, g, b } = d3.color(hex);
  9. return vectorDot([r, g, b], [0.3, 0.6, 0.1]) >= 128 ? "#222" : "#ddd";
  10. };