Skip to content

Commit 9a51cb0

Browse files
committed
added mapper
major property assignment cleanup separated helper functions JS add WE mapper and Surose mappers fixed onChange functions running when value update with the same value
1 parent 9bce4db commit 9a51cb0

File tree

4 files changed

+262
-463
lines changed

4 files changed

+262
-463
lines changed

index.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
<meta charset="utf-8">
77
<link rel="stylesheet" type="text/css" href="./css/index.css" media="screen" />
88
<script src="./js/lil-gui-0.19.js"></script>
9-
<script type="text/javascript" src="./js/index.js"></script>
9+
<script src="./js/helper.js"></script>
10+
<script src="./js/mapper.js"></script>
11+
<script src="./js/index.js"></script>
1012
<link rel="icon" type="image/x-icon" href="./favicon.ico">
1113
</head>
1214

js/helper.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
function rgbToHue(color) {
2+
let tmp = color.map(function (c) {
3+
return Math.ceil(c * 255)
4+
});
5+
return rgbToHsl(...tmp)[0] * 360;
6+
}
7+
8+
function hexToRgb(hex) {
9+
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
10+
return result ? {
11+
r: parseInt(result[2], 16),
12+
g: parseInt(result[3], 16),
13+
b: parseInt(result[4], 16)
14+
} : null;
15+
}
16+
17+
function rgbToHsl(r, g, b) {
18+
r /= 255, g /= 255, b /= 255;
19+
20+
var max = Math.max(r, g, b), min = Math.min(r, g, b);
21+
var h, s, l = (max + min) / 2;
22+
23+
if (max == min) {
24+
h = s = 0; // achromatic
25+
} else {
26+
var d = max - min;
27+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
28+
29+
switch (max) {
30+
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
31+
case g: h = (b - r) / d + 2; break;
32+
case b: h = (r - g) / d + 4; break;
33+
}
34+
35+
h /= 6;
36+
}
37+
38+
return [h, s, l];
39+
}
40+
41+
function map(value, from_a, from_b, to_a, to_b) {
42+
return (((value - from_a) * (to_b - to_a)) / (from_b - from_a)) + to_a;
43+
}
44+
45+
function clamp(min, max, value) {
46+
if (value < min)
47+
return min;
48+
if (value > max)
49+
return max;
50+
return value;
51+
}
52+
53+
function optionsToDict(options) {
54+
return options.reduce((acc, option) => {
55+
acc[option.label] = option.value;
56+
return acc;
57+
}, {});
58+
}
59+
60+
function paramsToUrl(urlParams, paramDefaults, filter) {
61+
var defaults = new URLSearchParams(paramDefaults)
62+
var params = new URLSearchParams(urlParams)
63+
64+
filter.forEach(key => {
65+
params.delete(key);
66+
defaults.delete(key);
67+
});
68+
69+
defaults.forEach((value, key) => {
70+
if (params.get(key) === value)
71+
params.delete(key);
72+
});
73+
74+
return window.location.origin + window.location.pathname + "?" + params.toString();
75+
}
76+
77+
function copyToClipboard(text) {
78+
const el = document.createElement('textarea');
79+
el.value = text;
80+
el.setAttribute('readonly', '');
81+
document.body.appendChild(el);
82+
el.select();
83+
document.execCommand('copy');
84+
document.body.removeChild(el);
85+
}
86+
87+
function getUrlParams() {
88+
urlParams = new URLSearchParams(window.location.search);
89+
if (urlParams.size == 0)
90+
return null;
91+
92+
params = {};
93+
for (const [key, value] of urlParams)
94+
params[key] = value;
95+
96+
return params;
97+
}
98+
99+
function preProcessProperties(properties) {
100+
const transformedProperties = {};
101+
for (const [key, value] of Object.entries(properties))
102+
transformedProperties[key] = value.value;
103+
return transformedProperties;
104+
}
105+
106+
function preProcessPreset(preset) {
107+
const transformedProperties = {};
108+
for (const [key, value] of Object.entries(preset))
109+
transformedProperties[key.toLowerCase()] = value;
110+
return transformedProperties;
111+
}
112+
113+
function arraysAreEqual(arr1, arr2) {
114+
if (!Array.isArray(arr1) || !Array.isArray(arr2))
115+
return false;
116+
if (arr1.length !== arr2.length)
117+
return false;
118+
for (let i = 0; i < arr1.length; i++)
119+
if (arr1[i] !== arr2[i])
120+
return false;
121+
return true;
122+
}

0 commit comments

Comments
 (0)