-
Notifications
You must be signed in to change notification settings - Fork 24
/
helpers.js
101 lines (85 loc) · 2.24 KB
/
helpers.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
// http://stackoverflow.com/a/1099670/895589
function getQueryParams(qs) {
qs = qs.split('+').join(' ');
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
// http://stackoverflow.com/a/17243070/895589
/* accepts parameters
* h Object = {h:x, s:y, v:z}
* OR
* h, s, v
*/
function getRGB(h, s, v) {
while (h > 1) h -= 1;
while (h < 0) h += 1;
var r, g, b, i, f, p, q, t;
if (h && s === undefined && v === undefined) {
s = h.s, v = h.v, h = h.h;
}
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
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: Math.floor(r * 255),
g: Math.floor(g * 255),
b: Math.floor(b * 255)
};
}
// General random & noise functions
function getRandomSeed() {
return Math.round(Math.random() * 9007199254740991);
}
function getInt(max, pivot) {
return (scene.seed % pivot) % max;
}
function getBool(pivot) {
return getInt(2, pivot) == 1;
}
function getFloat(pivot, from, to) {
if (!from)
from = 0;
if (!to)
to = 1;
var x = ((scene.seed % pivot) % 100000) / 100000;
return from + (to - from) * x;
}
function getPivot(text) {
var myrng = new Math.seedrandom(text);
return Math.round(myrng() * 100000000);
}
function simplex(x, y, octaves, lowercap, uppercap) {
if (!octaves) octaves = 6;
if (!lowercap) lowercap = 0;
if (!uppercap) uppercap = 1;
var v = 0;
for (var i = 1; i <= octaves; i++) {
var s = noise.simplex2(x * Math.pow(2, i-1),y * Math.pow(2, i-1)) / 2 + 0.5;
v += s * Math.pow(2, -i);
}
if (v < lowercap)
return 0;
if (v > uppercap)
return 1;
return (v - lowercap) / (uppercap - lowercap);
}
function getColorString(color, alpha) {
if (alpha === undefined)
alpha = 1;
return 'rgba(' + color.r + ', ' + color.g + ', ' + color.b + ', ' + alpha + ')';
}