const util = {
  getGyroPermission: () => {
    if(!DeviceOrientationEvent.requestPermission) {
      return Promise.resolve("granted")
    } else{
      return DeviceOrientationEvent.requestPermission()
    }
  },
  round: (x, decimalPlaces) =>
    Math.round(x * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces),
  // parameters in radians
  // return values in radians
  toPolarCoordinates: (alpha, beta, gamma) => {
    const rz = m3.zRotation(alpha);
    const rx = m3.xRotation(beta);
    const ry = m3.yRotation(gamma);

    const rot = m3.mul(rz, m3.mul(rx, ry));

    const screenNormal = m3.mulV(rot, [0, 0, 1]);

    const phi = Math.atan2(screenNormal[1], screenNormal[0]);
    const r = v3.norm2(screenNormal);
    // theta ∈ [0, π]
    // where 0 is lying flat and π is upside down
    const theta = Math.acos(screenNormal[2] / r);

    return [screenNormal, phi, theta];
  },
  rgb: (r, g, b) => `rgb(${255 * r}, ${255 * g}, ${255 * b})`,
  rgba: (r, g, b, a) => `rgb(${255 * r}, ${255 * g}, ${255 * b}, ${a})`,
  deg2rad: (deg) => deg / 180 * Math.PI,
  diffAngles: (a, b) => Math.atan2(Math.sin(b - a), Math.cos(b - a)),
};
