-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
45 lines (36 loc) · 1.06 KB
/
utils.js
File metadata and controls
45 lines (36 loc) · 1.06 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
function clamp(value, min, max) {
return Math.max(min, Math.min(value, max));
}
function clamp2D(x, y, dx, dy, dw, dh) {
x = clamp(x, dx, dx + dw);
y = clamp(y, dy, dy + dh);
return {x, y};
}
function toPixels(value) {
return `${value}px`;
}
function hex(hexValue) {
return '#' + hexValue;
}
function rgba({r, g, b, a=1}) {
return a === 1 ? `rgb(${r}, ${g}, ${b})` : `rgba(${r}, ${g}, ${b}, ${a})`;
}
function hsla({h, s, l, a=1}) {
return a === 1 ? `hsl(${h}, ${s}%, ${l}%)` : `hsla(${h}, ${s}%, ${l}%, ${a})`;
}
function hsva({h, s, v, a=1}) {
return a === 1 ? `hsv(${h}, ${s}%, ${v}%)` : `hsva(${h}, ${s}%, ${v}%, ${a})`;
}
function copyText(string) {
navigator.clipboard.writeText(string)
.catch(err => {
console.error("Error copying to clipboard: ", err);
alert("Failed to copy to clipboard!.");
});
}
function floor(value) {
return Math.floor(value);
}
function truncate(value, decimal=0) {
return Math.trunc(value * (10 ** decimal)) / 10 ** decimal;
}