-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
75 lines (75 loc) · 3.31 KB
/
util.js
File metadata and controls
75 lines (75 loc) · 3.31 KB
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
function sbcRip(d) {
var i = parseInt, r = Math.round;
var l = d.length, RGB = new Object();
if (l > 9) {
d = d.split(",");
if (d.length < 3 || d.length > 4)
return null; //ErrorCheck
RGB[0] = i(d[0].slice(4)), RGB[1] = i(d[1]), RGB[2] = i(d[2]), RGB[3] = d[3] ? parseFloat(d[3]) : -1;
}
else {
if (l == 8 || l == 6 || l < 4)
return null; //ErrorCheck
if (l < 6)
d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); //3 digit
d = i(d.slice(1), 16), RGB[0] = d >> 16 & 255, RGB[1] = d >> 8 & 255, RGB[2] = d & 255, RGB[3] = l == 9 || l == 5 ? r(((d >> 24 & 255) / 255) * 10000) / 10000 : -1;
}
return RGB;
}
// http://stackoverflow.com/a/13542669/6364886
function shadeBlendConvert(p, from, to = null) {
if (typeof (p) != "number" || p < -1 || p > 1 || typeof (from) != "string" || (from[0] != 'r' && from[0] != '#') || (typeof (to) != "string" && typeof (to) != "undefined"))
return null; //ErrorCheck
var i = parseInt, r = Math.round, h = from.length > 9, h = typeof (to) == "string" ? to.length > 9 ? true : to == "c" ? !h : false : h, b = p < 0, p = b ? p * -1 : p, to = to && to != "c" ? to : b ? "#000000" : "#FFFFFF", f = sbcRip(from), t = sbcRip(to);
if (!f || !t)
return null; //ErrorCheck
if (h)
return "rgb(" + r((t[0] - f[0]) * p + f[0]) + "," + r((t[1] - f[1]) * p + f[1]) + "," + r((t[2] - f[2]) * p + f[2]) + (f[3] < 0 && t[3] < 0 ? ")" : "," + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 : t[3] < 0 ? f[3] : t[3]) + ")");
else
return "#" + (0x100000000 + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 255) : t[3] > -1 ? r(t[3] * 255) : f[3] > -1 ? r(f[3] * 255) : 255) * 0x1000000 + r((t[0] - f[0]) * p + f[0]) * 0x10000 + r((t[1] - f[1]) * p + f[1]) * 0x100 + r((t[2] - f[2]) * p + f[2])).toString(16).slice(f[3] > -1 || t[3] > -1 ? 1 : 3);
}
function randomizeArray(field) {
// Fisher-Yates shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
for (var i = 0; i < field.length; i++) {
var j = Math.round(Math.random() * i);
var temp = field[i];
field[i] = field[j];
field[j] = temp;
}
return field;
}
function getValueFromHMTLInput(id) {
var input = document.getElementById(id);
if (input == null)
return null;
if (input.type == "checkbox")
return input.checked;
else if (input.type == "number")
return input.valueAsNumber;
else
return input.value;
}
function setHtmlInputValue(id, val) {
var input = document.getElementById(id);
if (input != null) {
if (input.type == "checkbox")
input.checked = val == '1' ? true : false;
else
input.value = val;
}
}
// fill the configuration inputs on the html page
function fillHtmlInputs(config) {
Object.keys(config).forEach(function (key, index) {
setHtmlInputValue(key, config[key]);
});
}
function readConfigurationValues(config) {
Object.keys(config).forEach(function (key, index) {
var val = getValueFromHMTLInput(key);
if (val != null) {
config[key] = val;
}
});
}
//# sourceMappingURL=util.js.map