Files: 8b2bb74986427753f3b1628bd266bcc7bd7693ea / common.js
1447 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, flat) { |
27 | var idColors = {} |
28 | var colorsUsed = {} |
29 | var ensureUniqueness = !flat |
30 | for (var i = 0; i < ids.length; i++) { |
31 | var id = ids[i] |
32 | var x = i / ids.length |
33 | var colorI = Math.floor(x*20) |
34 | var inbetweenAmount = x*20 - colorI |
35 | var color = common.interpolate(colors20[colorI], colors20[colorI+1], inbetweenAmount) |
36 | color[0] = Math.floor(color[0]) |
37 | color[1] = Math.floor(color[1]) |
38 | color[2] = Math.floor(color[2]) |
39 | color[3] = 255 |
40 | if (ensureUniqueness) { |
41 | while (colorsUsed[color]) { |
42 | color[3]-- |
43 | } |
44 | if (color[3] < 0) throw new Error('Too many ids for color') |
45 | colorsUsed[color] = true |
46 | } |
47 | idColors[id] = color |
48 | } |
49 | return idColors |
50 | } |
51 |
Built with git-ssb-web