// logic adapted from How to Generate Random Colors Programmatically by Martin Ankerl
// https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
const goldenRatioConj = 0.618033988749895;

const getColorGenerator = () => {
  let h = 0;
  const nextColor = () => {
    h += goldenRatioConj;
    h %= 1;
    const h6 = h * 6;
    const hIndex = Math.floor(h6);
    const f = 0.45 * (h6 - hIndex);
    const q = 0.9 - f;
    const t = f + 0.45;
    return `#${[
      [0.9, t, 0.45],
      [q, 0.9, 0.45],
      [0.45, 0.9, t],
      [0.45, q, 0.9],
      [t, 0.45, 0.9],
      [0.9, 0.45, q],
    ][hIndex]
      .map(component =>
        Math.floor(component * 256)
          .toString(16)
          .padStart(2, "0")
      )
      .reduce((x, y) => x + y)}`;
  };

  return nextColor;
};

export default getColorGenerator;
