• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Rainbows are hard.
2// https://mycarta.wordpress.com/2012/10/06/the-rainbow-is-deadlong-live-the-rainbow-part-3/
3
4var PHI = (1 + Math.sqrt(5)) / 2;
5
6// http://basecase.org/env/on-rainbows
7function sinebow(h) {
8  h += 0.5;
9  h = -h;
10  var r = Math.sin(Math.PI * h);
11  var g = Math.sin(Math.PI * (h + 1/3));
12  var b = Math.sin(Math.PI * (h + 2/3));
13  r *= r; g *= g; b *= b;
14
15  // Roughly correct for human perception.
16  // https://en.wikipedia.org/wiki/Luma_%28video%29
17  // Multiply by 2 to normalize all values to 0.5.
18  // (Halfway between black and white.)
19  var y = 2 * (0.2989 * r + 0.5870 * g + 0.1140 * b);
20  r /= y; g /= y; b /= y;
21
22  return [256 * r, 256 * g, 256 * b];
23}
24
25function nthColor(n) {
26  return sinebow(n * PHI);
27}
28
29function calculateColor(r, g, b, a, brightness) {
30  if (brightness <= 1) {
31    r *= brightness;
32    g *= brightness;
33    b *= brightness;
34  } else {
35    r = mapRange(brightness, 1, 2, r, 255);
36    g = mapRange(brightness, 1, 2, g, 255);
37    b = mapRange(brightness, 1, 2, b, 255);
38  }
39  r = Math.round(r);
40  g = Math.round(g);
41  b = Math.round(b);
42  return 'rgba(' + r + ',' + g + ',' + b + ', ' + a + ')';
43}
44