// https://stackoverflow.com/a/54014428
// input: h in [0,360] and s,v in [0,1] - output: r,g,b in [0,1]
export function hsl2rgb(h: number, s: number, l: number) {
  let a = s * Math.min(l, 1 - l);
  let f = (n: number, k = (n + h / 30) % 12) => l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
  return [f(0), f(8), f(4)];
}


export function clamp(x: number, bounds: [number, number]): number {
  const lower = Math.min(...bounds);
  const upper = Math.max(...bounds);

  return Math.max(lower, Math.min(upper, x));
}

export function rescale(x: number, range: { from?: [number, number], to?: [number, number] }): number {
  const [a, b] = range.from ? range.from : [0, 1];
  const [c, d] = range.to ? range.to : [0, 1];

  return c + (d - c) * (x - a) / (b - a);
}
