-
Notifications
You must be signed in to change notification settings - Fork 0
/
helperFunctions.js
56 lines (48 loc) · 1.45 KB
/
helperFunctions.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
createDistribution = (array, weights, size) => {
const distribution = [];
const sum = weights.reduce((a, b) => a + b);
const quant = size / sum;
for (let i = 0; i < array.length; ++i) {
const limit = quant * weights[i];
for (let j = 0; j < limit; ++j) {
distribution.push(i);
}
}
return distribution;
};
randomIndex = (distribution) => {
const index = Math.floor(distribution.length * Math.random()); // random index
return distribution[index];
};
convertScoreToInteger = (limit, score) => {
if (score === 0) {
return -limit;
}
else {
const b = parseInt((limit - score)/limit);
return parseInt(score + (limit * b));
}
}
HSVtoRGB = function(h, s, v) {
let r, g, b;
let i = Math.floor(h * 6);
let f = h * 6 - i;
let p = v * (1 - s);
let q = v * (1 - f * s);
let t = v * (1 - (1 - f) * s);
switch(i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return [r * 255, g * 255, b * 255];
}
function hexToRGB(hex) {
return ['0x' + hex[1] + hex[2] | 0, '0x' + hex[3] + hex[4] | 0, '0x' + hex[5] + hex[6] | 0];
}
randomFloat = function(min, max) {
return parseFloat((Math.random() * (max - min) + min));
}