-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
155 lines (142 loc) · 4.64 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* jshint node: true, browser: true */
'use strict';
const DEFAULT_INTERVAL = 1000 / 60;
const UTILS = require('./utils');
/**
* Invoke callback `cb` once with optional parameters `args` as soon as condition `check` is `true`, checking every `interval` *ms*
* (by default, *60 Hz ≈ 16.67 ms*).
*
* @example
* superscript.hold(check, cb, interval, ...args)
*/
const hold = (check, cb, interval, ...args) => {
var timer;
if ('undefined' !== typeof setTimeout) timer = setTimeout;
else if ('undefined' !== typeof window && 'undefined' !== typeof window.setTimeout) timer = window.setTimeout;
else throw new Error('superscript.hold: cannot use “setTimeout()” nor “window.setTimeout()”');
const DELAY = interval ? interval : DEFAULT_INTERVAL;
(function loop() {
if (check()) cb(...args);
else timer(loop, DELAY);
})();
};
/**
* https://davidwalsh.name/javascript-debounce-function
*/
const debounce = (func, wait, immediate) => {
var timeout;
return function () {
var context = this,
args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
/**
* Create a palette of `n` evenly-distributed colours (*n ≥ 2*).
*
* ☞ [Demo](https://codepen.io/tripu/full/JEMBoN)
*
* Invoke <code>get(i)</code> on the resulting object to retrieve the <em>i<sup>th</sup></em> colour in the palette (<em>0 ≥ i < n</em>):
* <code>get(i, rbg = false)</code>
*
* Default options:
*
* ```json
* {
* "shuffle": false,
* "shades": "auto",
* "greys": "auto",
* "blackAndWhite": false
* }
* ```
*
* Then, invoke `get(i)` on the resulting object to retrieve the *i<sup>th</sup>* colour in the palette (*0 ≥ i < n*):
*
* ```javascript
* new superscript.palette(10).get(5); // 65535
* new superscript.palette(10).get(0, true); // 'ff0000'
* ```
*
* ```javascript
* // Assume there's a pie chart with 4 slices. Let's assign colours to them:
* const palette = new superscript.palette(4);
* chart[0].css('color', '#' + palette.get(0, true));
* chart[1].css('color', '#' + palette.get(1, true));
* chart[2].css('color', '#' + palette.get(2, true));
* chart[3].css('color', '#' + palette.get(3, true));
* ```
* @example
* new superscript.Palette(n[, opts])
*/
const Palette = function (n, opts) {
const OPTIONS = JSON.parse(JSON.stringify(UTILS.DEFAULT_PALETTE_OPTIONS));
if (opts) for (const i in opts) OPTIONS[i] = opts[i];
const ERROR = UTILS.checkPaletteParams(n, OPTIONS);
if (ERROR) throw ERROR;
const MAX = 0x100;
const range = n - (OPTIONS.blackAndWhite ? 2 : 0);
let /* Shades, */ b, g, r, x;
/*
* If (false === OPTIONS.shades) {
* shades = 0;
* } else if (true === OPTIONS.shades)
* shades = 0;
* else if (true === OPTIONS.shades)
* shades = 0;
* else
*shades = OPTIONS.shades;
*/
this.n = n;
this.c = [];
for (let i = 0; i < range; i++) {
x = i * 6 / range;
r = g = b = 0;
if (1 > x) {
r = MAX - 1;
g = MAX * x;
} else if (2 > x) {
r = MAX - MAX * (x - 1);
g = MAX - 1;
} else if (3 > x) {
g = MAX - 1;
b = MAX * (x - 2);
} else if (4 > x) {
g = MAX - MAX * (x - 3);
b = MAX - 1;
} else if (5 > x) {
r = MAX * (x - 4);
b = MAX - 1;
} else {
r = MAX - 1;
b = MAX - MAX * (x - 5);
}
r = Math.min(MAX - 1, Math.round(r));
g = Math.min(MAX - 1, Math.round(g));
b = Math.min(MAX - 1, Math.round(b));
this.c.push((r << 16) + (g << 8) + b);
}
if (OPTIONS.blackAndWhite) {
this.c.unshift(0);
this.c.push(0xffffff);
}
if ('random' === OPTIONS.shuffle) this.c = UTILS.randomiseArray(this.c);
else if (OPTIONS.shuffle) this.c = UTILS.shuffleArray(this.c);
};
Palette.prototype.get = function (i, rgb = false) {
if (!this.hasOwnProperty('n') || !this.hasOwnProperty('c')) throw new Error('superscript.Palette.get: wrong palette object');
if (0 > i || i >= this.n) throw new Error('superscript.Palette.get: “i” should be an integer ≥ 0 and < n');
if (rgb) return (0x1000000 + this.c[i]).toString(16).substr(1, 6);
return this.c[i];
};
if ('undefined' !== typeof exports) {
exports.hold = hold;
exports.debounce = debounce;
exports.Palette = Palette;
}