-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.js
49 lines (42 loc) · 1.33 KB
/
utils.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
/**
* https://stackoverflow.com/a/52827031/778272
* @returns {Boolean} true if system is big endian */
const isBigEndian = (() => {
const array = new Uint8Array(4);
const view = new Uint32Array(array.buffer);
return !((view[0] = 1) & array[0]);
})();
console.info("Endianness: " + (isBigEndian ? "big" : "little"));
const rgbToVal = isBigEndian ?
(r, g, b) => ((r << 24) | (g << 16) | (b << 8) | 0xff) >>> 0:
(r, g, b) => ((0xff << 24) | (b << 16) | (g << 8) | r) >>> 0;
function readCssVar(varName) {
varName = varName.startsWith("--") ? varName : "--" + varName;
return window.getComputedStyle(document.documentElement).getPropertyValue(varName);
}
function readCssVarAsHexNumber(varName) {
const strValue = readCssVar(varName);
return strValue ? parseInt(strValue.replace(/[^a-fA-F0-9]/g, ""), 16) : null;
}
function cssColorToColor(cssColor) {
return rgbToVal(cssColor >>> 16 & 0xff, cssColor >>> 8 & 0xff, cssColor & 0xff);
}
class Debouncer {
constructor () { this.timer = null; }
set(task, delay) {
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
this.timer = null;
task();
}, delay);
}
}
export {
isBigEndian,
rgbToVal,
readCssVarAsHexNumber,
cssColorToColor,
Debouncer,
};