-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperscript.js
232 lines (207 loc) · 8.24 KB
/
superscript.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.superscript = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
/* 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;
}
},{"./utils":2}],2:[function(require,module,exports){
/* jshint node: true */
'use strict';
const DEFAULT_PALETTE_OPTIONS = {
'blackAndWhite': false,
'greys': 'auto',
'shades': 'auto',
'shuffle': false
};
const shuffleArray = (a) => {
const CUTOFF = a.length % 22 + 1,
MASK = 0xffffff & a.length << 16 + a.length << 8 + a.length;
const HASH = (n) => MASK & n << CUTOFF + (n >> 24 - CUTOFF);
return a.slice().sort((x, y) => {
if (HASH(x) > HASH(y))
return -1;
if (HASH(x) < HASH(y))
return +1;
return 0;
});
};
const randomiseArray = (a) => {
const RESULT = a.slice();
let tmp, x;
for (let i = 0; i < RESULT.length; i++) {
x = Math.floor(Math.random() * RESULT.length);
tmp = RESULT[x];
RESULT[x] = RESULT[i];
RESULT[i] = tmp;
}
return RESULT;
};
const checkPaletteParams = (n, opts) => {
if (!Number.isInteger(n) || 2 > n)
return new Error('superscript.Palette: “n” must be an integer ≥ 2');
for (const i in opts)
if ('blackAndWhite' === i) {
if (false !== opts[i] && true !== opts[i])
return new Error('superscript.Palette: “blackAndWhite” must be either “false” or “true”');
} else if ('greys' === i) {
if (false !== opts[i] && true !== opts[i] && 'auto' !== opts[i])
return new Error('superscript.Palette: “greys” must be either “false”, “true” or “auto”');
} else if ('shades' === i) {
if (false !== opts[i] && true !== opts[i] && 'auto' !== opts[i] && (!Number.isInteger(opts[i]) || 1 > opts[i]))
return new Error('superscript.Palette: “shades” must be either “false”, “true”, “auto” or an integer ≥ 1');
} else if ('shuffle' === i) {
if (false !== opts[i] && true !== opts[i] && 'random' !== opts[i])
return new Error('superscript.Palette: “shuffle” must be either “false”, “true” or “random”');
} else
return new Error(`superscript.Palette: “${i}” is not a valid option`);
if (opts.blackAndWhite && true === opts.greys && 3 > n)
return new Error('superscript.Palette: “blackAndWhite” and “greys” cannot be both “true” when “n” < 3');
if (opts.blackAndWhite && true === opts.shades && 4 > n)
return new Error('superscript.Palette: “blackAndWhite” and “shades” cannot be both “true” when “n” < 4');
if (opts.shades + (opts.blackAndWhite ? 2 : 0) < n)
return new Error(`superscript.Palette: the palette is too small to include ${opts.shades} shades${opts.blackAndWhite ? ' plus black and white' : ''}`);
return false;
};
exports.DEFAULT_PALETTE_OPTIONS = DEFAULT_PALETTE_OPTIONS;
exports.shuffleArray = shuffleArray;
exports.randomiseArray = randomiseArray;
exports.checkPaletteParams = checkPaletteParams;
},{}]},{},[1])(1)
});