forked from ThinkInvis/BRSerker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
185 lines (167 loc) · 5.43 KB
/
util.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
//links 2 number inputs together such that:
// - both inputs are limited within an overall min/max
// - value of second input is guaranteed to always be >= value of first input + margin
//assumes starting values (dft1, dft2) are sane under those restrictions
var LinkNumInputs = function(i1, i2, min, max, dft1, dft2, margin=0) {
i1.data("linkedInput", i2);
i2.data("linkedInput", i1);
i1.data("linkedInputMargin", margin);
i2.data("linkedInputMargin", margin);
i1.change(function() {
var newv = i1.val();
i1.data("linkedInput").attr('min', newv*1+i1.data("linkedInputMargin")*1);
});
i2.change(function() {
var newv = i2.val();
i2.data("linkedInput").attr('max', newv*1-i2.data("linkedInputMargin")*1);
});
i1.attr('min', min);
i1.attr('max', dft2-margin);
i2.attr('min', dft1+margin);
i2.attr('max', max);
i1.val(dft1);
i2.val(dft2);
}
//links any number of other controls [arri2] to an option input [i1] such that the other controls are hidden unless [i1]'s selected option has any of the values in [optsAllow]
var ParentOptionInput = function(i1, arri2, optsAllow) {
i1.data("childInputCount", arri2.length);
for(var i = 0; i < arri2.length; i++) {
i1.data("childInput" + i, arri2[i]);
}
i1.change(function() {
var doTogg = optsAllow.includes(i1.val());
for(var i = 0; i < i1.data("childInputCount"); i++) {
var iN = i1.data("childInput" + i);
if(doTogg)
iN.slideDown(100);
else
iN.slideUp(100);
}
});
}
var ParentCheckboxInput = function(i1, arrChk, arrOff) {
i1.data("childInputOnCount", arrChk.length);
for(var i = 0; i < arrChk.length; i++) {
i1.data("childInputOn" + i, arrChk[i]);
}
i1.data("childInputOffCount", arrOff.length);
for(var i = 0; i < arrOff.length; i++) {
i1.data("childInputOff" + i, arrOff[i]);
}
i1.change(function(e) {
var doTogg = i1.get(0).checked;
for(var i = 0; i < i1.data("childInputOnCount"); i++) {
var iN = i1.data("childInputOn" + i);
if(doTogg)
iN.slideDown(100);
else
iN.slideUp(100);
}
for(var i = 0; i < i1.data("childInputOffCount"); i++) {
var iN = i1.data("childInputOff" + i);
if(!doTogg)
iN.slideDown(100);
else
iN.slideUp(100);
}
});
}
var ParentClickInput = function(i1, arrChk) {
i1.data("childInputCount", arrChk.length);
for(var i = 0; i < arrChk.length; i++) {
i1.data("childInput" + i, arrChk[i]);
}
i1.click(function(e) {
for(var i = 0; i < i1.data("childInputCount"); i++) {
var iN = i1.data("childInput" + i);
iN.slideToggle(100);
}
});
}
//% operator is signed remainder, not modulus -- works differently on negative numbers
var Mod = function(n, p) {
return n - p * Math.floor(n/p);
}
var isPow2 = function(n) {
return (n !== 0) && (n & (n - 1)) === 0;
}
var pctDone = function(x,a,b) {
return ((x-a)/(b-a)*100).toFixed(1) + "%";
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function BlobDownload(Filename, Bytes, Mimetype) {
var filData = new Blob(Bytes, { type: Mimetype });
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // for IE
window.navigator.msSaveOrOpenBlob(filData, Filename);
} else { // for Non-IE (chrome, firefox etc.)
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var filUrl = URL.createObjectURL(filData);
a.href = filUrl;
a.download = Filename;
a.click();
a.remove();
}
};
//find the closest color in a colorset using the dE2000 algorithm
var ColorQuantize = function(color, colorset) {
//expects color to be an array-RGBA value from 0-1, and colorset to be an array of such values
//TODO: opacity doesn't work right yet
var bestScore = Number.MAX_VALUE
var bestIndex = 0; //fallback to the first color if something goes horribly wrong
var bestColor = colorset[0];
var colorLAB = Colour.RGBA2LAB(color[0]*255, color[1]*255, color[2]*255, color[3]);
for(var i = 0; i < colorset.length; i++) {
var matchLAB = Colour.RGBA2LAB(colorset[i][0]*255, colorset[i][1]*255, colorset[i][2]*255, colorset[i][3]);
var thisScore = Colour.DeltaE00(colorLAB[0], colorLAB[1], colorLAB[2], matchLAB[0], matchLAB[1], matchLAB[2]);
if(thisScore < bestScore) {
bestScore = thisScore;
bestIndex = i;
bestColor = colorset[i];
}
}
return {
Closeness: bestScore,
SetI: bestIndex,
Color: bestColor
}
}
//based on threejs implementation of randInt
var fracToInt = function (low, high, src) {
return low + Math.floor(src * ( high - low + 1 ));
}
var mapLocalIter2 = function(func, map, r, x, y, mx, my, bx, by) {
var rxm = Math.max(x-r, mx);
var rxp = Math.min(x+r, bx-1);
var rym = Math.max(y-r, my);
var ryp = Math.min(y+r, by-1);
for(var i = rxm; i <= rxp; i++) {
for(var j = rym; j <= ryp; j++) {
func(map[i][j], i, j, (i == x) && (j == y), x-i, y-j);
}
}
}
var mapLocalIter3 = function(func, map, r, x, y, z, mx, my, mz, bx, by, bz) {
var rxm = Math.max(x-r, mx);
var rxp = Math.min(x+r, bx-1);
var rym = Math.max(y-r, my);
var ryp = Math.min(y+r, by-1);
var rzm = Math.max(z-r, mz);
var rzp = Math.min(z+r, bz-1);
for(var i = rxm; i <= rxp; i++) {
for(var j = rym; j <= ryp; j++) {
for(var k = rzm; k <= rzp; k++) {
func(map[i][j][k], i, j, k, (i == x) && (j == y) && (k == z));
}
}
}
}