@thi.ng/imago 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/units.js ADDED
@@ -0,0 +1,85 @@
1
+ import { isArray, isArrayLike, isNumber, isString } from "@thi.ng/checks";
2
+ import { illegalArgs } from "@thi.ng/errors";
3
+ import {
4
+ GRAVITY_MAP
5
+ } from "./api.js";
6
+ const round = Math.round;
7
+ const ensureSize = (meta) => !(isNumber(meta.width) && isNumber(meta.height)) && illegalArgs("can't determine image size");
8
+ const coerceColor = (col) => isString(col) ? col : isArrayLike(col) ? { r: col[0], g: col[1], b: col[2], alpha: col[3] ?? 1 } : col;
9
+ const positionOrGravity = (pos, gravity, [w, h], [parentW, parentH], unit = "px") => {
10
+ if (!pos)
11
+ return gravity ? { gravity: GRAVITY_MAP[gravity] } : void 0;
12
+ const isPC = unit === "%";
13
+ let { l, r, t, b } = pos;
14
+ if (l != null)
15
+ l = round(isPC ? l * parentW / 100 : l);
16
+ if (r != null)
17
+ l = round(parentW - (isPC ? r * parentW / 100 : r) - w);
18
+ if (t != null)
19
+ t = round(isPC ? t * parentH / 100 : t);
20
+ if (b != null)
21
+ t = round(parentH - (isPC ? b * parentH / 100 : b) - h);
22
+ return { left: l, top: t };
23
+ };
24
+ const gravityPosition = (gravity, [w, h], [parentW, parentH]) => [
25
+ gravity.includes("w") ? 0 : gravity.includes("e") ? parentW - w : parentW - w >> 1,
26
+ gravity.includes("n") ? 0 : gravity.includes("s") ? parentH - h : parentH - h >> 1
27
+ ];
28
+ const refSize = ([w, h], ref) => {
29
+ switch (ref) {
30
+ case "w":
31
+ return w;
32
+ case "h":
33
+ return h;
34
+ case "max":
35
+ return Math.max(w, h);
36
+ case "min":
37
+ default:
38
+ return Math.min(w, h);
39
+ }
40
+ };
41
+ const computeSize = (size, curr, unit = "px") => {
42
+ const aspect = curr[0] / curr[1];
43
+ let res;
44
+ if (isNumber(size)) {
45
+ res = aspect > 1 ? [size, size / aspect] : [size * aspect, size];
46
+ } else {
47
+ const [w, h] = size;
48
+ res = w >= 0 ? h >= 0 ? size : [w, w / aspect] : h > 0 ? [w * aspect, h] : illegalArgs(
49
+ `require at least width or height, but got: ${JSON.stringify(
50
+ size
51
+ )}`
52
+ );
53
+ }
54
+ if (unit === "%") {
55
+ res[0] *= curr[0] / 100;
56
+ res[1] *= curr[1] / 100;
57
+ }
58
+ res[0] = round(res[0]);
59
+ res[1] = round(res[1]);
60
+ return res;
61
+ };
62
+ const computeMargins = (size, curr, ref = "min", unit = "px") => {
63
+ let res;
64
+ const refSide = refSize(curr, ref);
65
+ const isPC = unit === "%";
66
+ if (isArray(size) && size.length === 4) {
67
+ res = isPC ? size.map((x) => round(x * refSide / 100)) : size.map(round);
68
+ } else if (isNumber(size)) {
69
+ const w = round(isPC ? refSide * size / 100 : size);
70
+ res = [w, w, w, w];
71
+ } else {
72
+ const [w, h] = computeSize(size, curr, unit);
73
+ res = [w, w, h, h];
74
+ }
75
+ return res;
76
+ };
77
+ export {
78
+ coerceColor,
79
+ computeMargins,
80
+ computeSize,
81
+ ensureSize,
82
+ gravityPosition,
83
+ positionOrGravity,
84
+ refSize
85
+ };