Files: cec705118d65476d8ba5b903fb836cae42f2f1dd / common.js
1327 bytesRaw
1 | var common = typeof window !== 'undefined' ? window : exports |
2 | |
3 | common.startTime = new Date('2015-08-05').getTime() |
4 | |
5 | var colors20 = [ |
6 | [255, 106, 152], [255, 98, 188], [250, 98, 219], [231, 107, 243], |
7 | [199, 124, 255], [149, 144, 255], [53, 162, 255], [0, 176, 246], |
8 | [0, 186, 224], [0, 191, 196], [0, 193, 163], [0, 191, 125], |
9 | [0, 187, 78], [57, 182, 0], [124, 174, 0], [163, 165, 0], |
10 | [192, 155, 0], [216, 144, 0], [234, 131, 49], [248, 118, 109], |
11 | [255, 106, 152] |
12 | ] |
13 | |
14 | common.interpolate = function (a, b, n) { |
15 | if (!a) a = [0, 0, 0, 0] |
16 | if (!b) b = [0, 0, 0, 0] |
17 | var m = 1 - n |
18 | return [ |
19 | a[0] * m + b[0] * n, |
20 | a[1] * m + b[1] * n, |
21 | a[2] * m + b[2] * n, |
22 | a[3] * m + b[3] * n, |
23 | ] |
24 | } |
25 | |
26 | common.computeIdColors = function (ids) { |
27 | var idColors = {} |
28 | var colorsUsed = {} |
29 | for (var i = 0; i < ids.length; i++) { |
30 | var id = ids[i] |
31 | var x = i / ids.length |
32 | var colorI = Math.floor(x*20) |
33 | var inbetweenAmount = x*20 - colorI |
34 | var color = common.interpolate(colors20[colorI], colors20[colorI+1], inbetweenAmount) |
35 | color[0] = Math.floor(color[0]) |
36 | color[1] = Math.floor(color[1]) |
37 | color[2] = Math.floor(color[2]) |
38 | color[3] = 255 |
39 | // Ensure uniqueness |
40 | while (colorsUsed[color]) { |
41 | color[3]-- |
42 | } |
43 | colorsUsed[color] = true |
44 | idColors[id] = color |
45 | } |
46 | return idColors |
47 | } |
48 |
Built with git-ssb-web