@robosystems/core 0.21.7 → 0.22.1

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.
@@ -0,0 +1,389 @@
1
+ /**
2
+ * A knowledge graph drawn live, in the look of the brand film: nodes scattered in space come
3
+ * home to clusters around their hubs, drift a little, and turn slowly over a field of dust,
4
+ * while a pulse of light travels each link between hubs. Decoration: the clusters stand for
5
+ * nothing in particular. Plain WebGL with no library, so the landing page pays for a few
6
+ * hundred lines rather than a 3D engine. The lens and the dust are the film stage's own.
7
+ */
8
+ import { DEFAULT_PALETTE, readPalette } from './palette';
9
+ const DUST_COUNT = 900;
10
+ export const RADIUS = 3.4; // the graph fits inside a sphere of this size
11
+ const CAM_Z = 14;
12
+ const FOV = 38;
13
+ const BESIDE_CENTRE = 460; // CSS px from the top, the middle of the hero's words
14
+ const SETTLED = 8; // seconds in, the picture reduced motion holds
15
+ const clamp = (x) => Math.max(0, Math.min(1, x));
16
+ const seg = (t, a, b) => clamp((t - a) / (b - a));
17
+ const easeOut = (x) => 1 - Math.pow(1 - clamp(x), 3);
18
+ const easeInOut = (x) => {
19
+ x = clamp(x);
20
+ return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;
21
+ };
22
+ // Each cluster's size and which palette colour it wears.
23
+ const CLUSTERS = [
24
+ [46, 1],
25
+ [42, 2],
26
+ [38, 5],
27
+ [34, 0],
28
+ [32, 3],
29
+ [28, 4],
30
+ [24, 1],
31
+ [20, 2],
32
+ ];
33
+ /**
34
+ * Seeded, so every visitor and every screenshot gets the same picture. The palette colours
35
+ * the nodes and takes no part in where they sit.
36
+ */
37
+ export function buildGraph(seed = 11, palette = DEFAULT_PALETTE) {
38
+ let s = seed;
39
+ const rnd = () => ((s = (s * 16807) % 2147483647), s / 2147483647);
40
+ const dust = new Float32Array(DUST_COUNT * 3);
41
+ const dustSize = new Float32Array(DUST_COUNT);
42
+ for (let i = 0; i < DUST_COUNT; i++) {
43
+ dust.set([(rnd() - 0.5) * 60, (rnd() - 0.5) * 34, -rnd() * 110 + 8], i * 3);
44
+ dustSize[i] = 0.3 + rnd() * 0.8;
45
+ }
46
+ const total = CLUSTERS.reduce((n, [count]) => n + count, 0);
47
+ const scatter = new Float32Array(total * 3);
48
+ const home = new Float32Array(total * 3);
49
+ const size = new Float32Array(total);
50
+ const color = new Float32Array(total * 3);
51
+ const drift = new Float32Array(total);
52
+ const pairs = [];
53
+ const hubs = [];
54
+ const at = (i) => [home[i * 3], home[i * 3 + 1], home[i * 3 + 2]];
55
+ const d2 = (i, j) => at(i).reduce((sum, v, k) => sum + (v - at(j)[k]) ** 2, 0);
56
+ const nearest = (i, among, n) => among
57
+ .filter((j) => j !== i)
58
+ .sort((p, q) => d2(i, p) - d2(i, q))
59
+ .slice(0, n);
60
+ let i = 0;
61
+ CLUSTERS.forEach(([count, tint], c) => {
62
+ const rgb = palette[tint];
63
+ // cluster centres spread evenly over an ellipsoid, members in a soft cloud about each
64
+ const y = 1 - ((c + 0.5) / CLUSTERS.length) * 2;
65
+ const r = Math.sqrt(1 - y * y);
66
+ const a = c * 2.39996 + 0.6;
67
+ const centre = [Math.cos(a) * r * 3.0, y * 2.6, Math.sin(a) * r * 2.6];
68
+ const spread = 0.46 + count * 0.011;
69
+ const members = [];
70
+ for (let m = 0; m < count; m++, i++) {
71
+ const hub = m === 0;
72
+ scatter.set([(rnd() - 0.5) * 44, (rnd() - 0.5) * 24, -rnd() * 46 + 4], i * 3);
73
+ const off = () => (hub ? 0 : (rnd() + rnd() + rnd() - 1.5) * spread);
74
+ home.set([centre[0] + off(), centre[1] + off(), centre[2] + off()], i * 3);
75
+ size[i] = hub ? 4.4 : 1.4 + rnd() * 1.3;
76
+ color.set(hub ? [1, 1, 1] : rgb, i * 3);
77
+ drift[i] = hub ? 0.03 : 0.05 + rnd() * 0.07;
78
+ members.push(i);
79
+ }
80
+ hubs.push(members[0]);
81
+ members.slice(1).forEach((n) => {
82
+ // most members hang off the hub; the rest, and a few besides, off a neighbour
83
+ if (rnd() < 0.62)
84
+ pairs.push(members[0], n);
85
+ else
86
+ pairs.push(n, nearest(n, members.slice(1), 1)[0]);
87
+ if (rnd() < 0.3)
88
+ pairs.push(n, nearest(n, members.slice(1), 2)[1]);
89
+ });
90
+ });
91
+ // pull the whole graph inside its sphere, so no outlying node strays off the stage
92
+ const reach = Math.max(...Array.from({ length: total }, (_, n) => Math.hypot(...at(n))));
93
+ const fit = Math.min(1, (RADIUS - 0.15) / reach);
94
+ for (let k = 0; k < home.length; k++)
95
+ home[k] *= fit;
96
+ const routes = [];
97
+ hubs.forEach((h) => nearest(h, hubs, 2).forEach((o) => {
98
+ const seen = routes.some((_, k) => k % 2 === 0 && routes[k] === o && routes[k + 1] === h);
99
+ if (!seen)
100
+ routes.push(h, o);
101
+ }));
102
+ pairs.push(...routes);
103
+ return { dust, dustSize, scatter, home, size, color, drift, pairs, routes };
104
+ }
105
+ /**
106
+ * Where the graph sits: right of the words when they are set beside it, above them
107
+ * otherwise. Beside them its centre is a fixed distance from the top, level with the words,
108
+ * so a tall window does not drop it toward the section below.
109
+ */
110
+ export function placeGraph(aspect, beside, height) {
111
+ const halfH = CAM_Z * Math.tan((FOV * Math.PI) / 360);
112
+ const halfW = halfH * aspect;
113
+ if (beside) {
114
+ const scale = Math.min(1, (halfW * 0.36) / RADIUS, (halfH * 0.8) / RADIUS);
115
+ const centre = Math.min(BESIDE_CENTRE, height / 2) / height;
116
+ return { x: halfW * 0.48, y: halfH * (1 - 2 * centre), scale };
117
+ }
118
+ const scale = Math.min(1, (halfW * 0.8) / RADIUS, (halfH * 0.29) / RADIUS);
119
+ return { x: 0, y: halfH * 0.47, scale };
120
+ }
121
+ const POINT_VERT = `
122
+ precision mediump float;
123
+ attribute vec3 position; attribute vec3 color; attribute float size; attribute float front;
124
+ uniform mat4 uProj; uniform float uRot, uFocus, uAperture, uPx, uHalo;
125
+ varying float vA; varying vec3 vC;
126
+ void main() {
127
+ vC = color;
128
+ float c = cos(uRot), s = sin(uRot);
129
+ vec3 p = vec3(c * position.x + s * position.z, position.y, c * position.z - s * position.x);
130
+ float d = ${CAM_Z.toFixed(1)} - p.z;
131
+ float coc = abs(d - uFocus) * uAperture;
132
+ gl_PointSize = (size + coc * 5.) * 60. / d * uPx * uHalo;
133
+ gl_Position = uProj * vec4(p.xy, -d, 1.);
134
+ vec2 n = abs(gl_Position.xy / gl_Position.w);
135
+ vA = smoothstep(1.5, 5., d) * smoothstep(1.02, .84, max(n.x, n.y)) / (1. + coc * coc * 1.6) * front;
136
+ }`;
137
+ // The film blooms its highlights in a second pass; here each point carries its own soft halo.
138
+ const POINT_FRAG = `
139
+ precision mediump float;
140
+ uniform float uAlpha, uHalo, uGlow; varying float vA; varying vec3 vC;
141
+ void main() {
142
+ float r = length(gl_PointCoord - .5) * uHalo;
143
+ float a = smoothstep(.5, .32, r) + uGlow * exp(-r * r * 5.);
144
+ gl_FragColor = vec4(vC, a * vA * uAlpha);
145
+ }`;
146
+ const LINE_VERT = `
147
+ precision mediump float;
148
+ attribute vec3 position; attribute vec3 color; attribute float front; uniform mat4 uProj;
149
+ varying float vF; varying vec3 vC;
150
+ void main() { vF = front; vC = color; gl_Position = uProj * vec4(position.xy, position.z - ${CAM_Z.toFixed(1)}, 1.); }`;
151
+ const LINE_FRAG = `
152
+ precision mediump float; uniform float uAlpha; varying float vF; varying vec3 vC;
153
+ void main() { gl_FragColor = vec4(vC, uAlpha * vF); }`;
154
+ function program(gl, vert, frag) {
155
+ var _a;
156
+ const p = gl.createProgram();
157
+ for (const [type, src] of [
158
+ [gl.VERTEX_SHADER, vert],
159
+ [gl.FRAGMENT_SHADER, frag],
160
+ ]) {
161
+ const sh = gl.createShader(type);
162
+ gl.shaderSource(sh, src);
163
+ gl.compileShader(sh);
164
+ gl.attachShader(p, sh);
165
+ }
166
+ gl.linkProgram(p);
167
+ if (!gl.getProgramParameter(p, gl.LINK_STATUS)) {
168
+ throw new Error((_a = gl.getProgramInfoLog(p)) !== null && _a !== void 0 ? _a : 'shader link failed');
169
+ }
170
+ const u = (name) => gl.getUniformLocation(p, name);
171
+ const a = (name) => gl.getAttribLocation(p, name);
172
+ return { p, u, a };
173
+ }
174
+ /**
175
+ * Draws the stage on `canvas` until the returned function is called. It runs only while the
176
+ * canvas is on screen, and holds one settled frame for a visitor who asks for reduced
177
+ * motion. Where WebGL is unavailable it draws nothing: the page stands without it.
178
+ */
179
+ export function mountGraphStage(canvas,
180
+ /** The CSS width from which the page sets its words beside the graph. */
181
+ besideFrom) {
182
+ const gl = canvas.getContext('webgl', { alpha: true, antialias: true });
183
+ if (!gl)
184
+ return () => { };
185
+ const palette = readPalette(canvas);
186
+ const g = buildGraph(11, palette);
187
+ const count = g.size.length;
188
+ const points = program(gl, POINT_VERT, POINT_FRAG);
189
+ const lines = program(gl, LINE_VERT, LINE_FRAG);
190
+ const buffer = (data, usage = gl.STATIC_DRAW) => {
191
+ const b = gl.createBuffer();
192
+ gl.bindBuffer(gl.ARRAY_BUFFER, b);
193
+ gl.bufferData(gl.ARRAY_BUFFER, data, usage);
194
+ return b;
195
+ };
196
+ const bind = (prog, name, b, n) => {
197
+ const loc = prog.a(name);
198
+ gl.bindBuffer(gl.ARRAY_BUFFER, b);
199
+ gl.enableVertexAttribArray(loc);
200
+ gl.vertexAttribPointer(loc, n, gl.FLOAT, false, 0, 0);
201
+ };
202
+ const dustPos = buffer(g.dust);
203
+ const dustSize = buffer(g.dustSize);
204
+ const nodeSize = buffer(g.size);
205
+ const nodeColor = buffer(g.color);
206
+ const pos = new Float32Array(count * 3);
207
+ const front = new Float32Array(count);
208
+ const edgePos = new Float32Array(g.pairs.length * 3);
209
+ const edgeFront = new Float32Array(g.pairs.length);
210
+ const edgeColor = new Float32Array(g.pairs.length * 3);
211
+ g.pairs.forEach((n, k) => edgeColor.set(g.color.subarray(n * 3, n * 3 + 3), k * 3));
212
+ const edgeColorBuf = buffer(edgeColor);
213
+ // a pulse of light along each route between hubs
214
+ const pulses = g.routes.length / 2;
215
+ const pulsePos = new Float32Array(pulses * 3);
216
+ const pulseFront = new Float32Array(pulses);
217
+ const pulsePosBuf = buffer(pulsePos, gl.DYNAMIC_DRAW);
218
+ const pulseFrontBuf = buffer(pulseFront, gl.DYNAMIC_DRAW);
219
+ const pulseSize = buffer(new Float32Array(pulses).fill(2.4));
220
+ const nodePos = buffer(pos, gl.DYNAMIC_DRAW);
221
+ const nodeFront = buffer(front, gl.DYNAMIC_DRAW);
222
+ const edgePosBuf = buffer(edgePos, gl.DYNAMIC_DRAW);
223
+ const edgeFrontBuf = buffer(edgeFront, gl.DYNAMIC_DRAW);
224
+ gl.enable(gl.BLEND);
225
+ // Light is added, and alpha is kept in step with it: a GPU compositor drops colour that
226
+ // has no alpha behind it, though a software one shows it.
227
+ gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
228
+ gl.clearColor(0, 0, 0, 0);
229
+ const proj = new Float32Array(16);
230
+ let place = placeGraph(1, false, 1);
231
+ const resize = () => {
232
+ const dpr = Math.min(window.devicePixelRatio || 1, 2);
233
+ const w = Math.max(1, Math.round(canvas.clientWidth * dpr));
234
+ const h = Math.max(1, Math.round(canvas.clientHeight * dpr));
235
+ if (canvas.width !== w || canvas.height !== h) {
236
+ canvas.width = w;
237
+ canvas.height = h;
238
+ }
239
+ gl.viewport(0, 0, w, h);
240
+ const f = 1 / Math.tan((FOV * Math.PI) / 360);
241
+ const near = 0.1;
242
+ const far = 400;
243
+ proj.fill(0);
244
+ proj[0] = f / (w / h);
245
+ proj[5] = f;
246
+ proj[10] = (far + near) / (near - far);
247
+ proj[11] = -1;
248
+ proj[14] = (2 * far * near) / (near - far);
249
+ place = placeGraph(w / h, canvas.clientWidth >= besideFrom, canvas.clientHeight);
250
+ };
251
+ const draw = (t) => {
252
+ const home = easeInOut(seg(t, 0.3, 3.2));
253
+ // The turn: a slow spin about the upright axis, then a slight tilt toward the viewer.
254
+ const [tilt, spin] = [0.22, t * 0.07];
255
+ const [cx, sx, cy, sy] = [
256
+ Math.cos(tilt),
257
+ Math.sin(tilt),
258
+ Math.cos(spin),
259
+ Math.sin(spin),
260
+ ];
261
+ for (let i = 0; i < count; i++) {
262
+ const w = g.drift[i];
263
+ const hx = g.home[i * 3] + w * Math.sin(t * 0.6 + i);
264
+ const hy = g.home[i * 3 + 1] + w * Math.cos(t * 0.5 + i * 1.3);
265
+ const hz = g.home[i * 3 + 2] + w * Math.sin(t * 0.4 + i * 0.7);
266
+ const x2 = cy * hx + sy * hz;
267
+ const z2 = cy * hz - sy * hx;
268
+ const y3 = cx * hy - sx * z2;
269
+ const z3 = sx * hy + cx * z2;
270
+ const target = [
271
+ place.x + x2 * place.scale,
272
+ place.y + y3 * place.scale,
273
+ z3 * place.scale,
274
+ ];
275
+ for (let k = 0; k < 3; k++) {
276
+ const from = g.scatter[i * 3 + k];
277
+ pos[i * 3 + k] = from + (target[k] - from) * home;
278
+ }
279
+ // nodes on the far side are dimmed, which is what gives the graph its depth
280
+ const facing = 0.4 + 0.6 * easeInOut(seg(z3 / RADIUS, -1, 0.5));
281
+ front[i] = 1 + (facing - 1) * home;
282
+ }
283
+ g.pairs.forEach((n, k) => {
284
+ edgePos.set(pos.subarray(n * 3, n * 3 + 3), k * 3);
285
+ edgeFront[k] = front[n];
286
+ });
287
+ gl.clear(gl.COLOR_BUFFER_BIT);
288
+ const px = canvas.height / 1080;
289
+ gl.useProgram(lines.p);
290
+ gl.uniformMatrix4fv(lines.u('uProj'), false, proj);
291
+ gl.uniform1f(lines.u('uAlpha'), 0.42 * easeOut(seg(t, 2.4, 4)));
292
+ gl.bindBuffer(gl.ARRAY_BUFFER, edgePosBuf);
293
+ gl.bufferSubData(gl.ARRAY_BUFFER, 0, edgePos);
294
+ gl.bindBuffer(gl.ARRAY_BUFFER, edgeFrontBuf);
295
+ gl.bufferSubData(gl.ARRAY_BUFFER, 0, edgeFront);
296
+ bind(lines, 'position', edgePosBuf, 3);
297
+ bind(lines, 'front', edgeFrontBuf, 1);
298
+ bind(lines, 'color', edgeColorBuf, 3);
299
+ gl.drawArrays(gl.LINES, 0, g.pairs.length);
300
+ gl.useProgram(points.p);
301
+ gl.uniformMatrix4fv(points.u('uProj'), false, proj);
302
+ gl.uniform1f(points.u('uFocus'), CAM_Z);
303
+ // dust: the first brand colour, small, drifting almost imperceptibly
304
+ gl.uniform1f(points.u('uRot'), t * 0.004);
305
+ gl.uniform1f(points.u('uAperture'), 0.05);
306
+ gl.uniform1f(points.u('uPx'), px);
307
+ gl.uniform1f(points.u('uHalo'), 1);
308
+ gl.uniform1f(points.u('uGlow'), 0);
309
+ gl.uniform1f(points.u('uAlpha'), 0.5);
310
+ bind(points, 'position', dustPos, 3);
311
+ bind(points, 'size', dustSize, 1);
312
+ gl.disableVertexAttribArray(points.a('front'));
313
+ gl.vertexAttrib1f(points.a('front'), 1);
314
+ gl.disableVertexAttribArray(points.a('color'));
315
+ gl.vertexAttrib3f(points.a('color'), ...palette[0]);
316
+ gl.drawArrays(gl.POINTS, 0, DUST_COUNT);
317
+ // the nodes: haloed, white hubs and coloured members
318
+ gl.uniform1f(points.u('uRot'), 0);
319
+ gl.uniform1f(points.u('uAperture'), 0.06);
320
+ gl.uniform1f(points.u('uPx'), px * (1 + (place.scale - 1) * home));
321
+ gl.uniform1f(points.u('uHalo'), 3);
322
+ gl.uniform1f(points.u('uGlow'), 0.22);
323
+ gl.uniform1f(points.u('uAlpha'), 1);
324
+ gl.bindBuffer(gl.ARRAY_BUFFER, nodePos);
325
+ gl.bufferSubData(gl.ARRAY_BUFFER, 0, pos);
326
+ gl.bindBuffer(gl.ARRAY_BUFFER, nodeFront);
327
+ gl.bufferSubData(gl.ARRAY_BUFFER, 0, front);
328
+ bind(points, 'position', nodePos, 3);
329
+ bind(points, 'size', nodeSize, 1);
330
+ bind(points, 'front', nodeFront, 1);
331
+ bind(points, 'color', nodeColor, 3);
332
+ gl.drawArrays(gl.POINTS, 0, count);
333
+ {
334
+ const lit = easeOut(seg(t, 3.6, 5));
335
+ for (let k = 0; k < pulses; k++) {
336
+ const [a, b] = [g.routes[k * 2], g.routes[k * 2 + 1]];
337
+ const along = (t * 0.16 + k * 0.37) % 1;
338
+ for (let c = 0; c < 3; c++) {
339
+ pulsePos[k * 3 + c] =
340
+ pos[a * 3 + c] + (pos[b * 3 + c] - pos[a * 3 + c]) * along;
341
+ }
342
+ pulseFront[k] = Math.sin(Math.PI * along) * lit;
343
+ }
344
+ gl.bindBuffer(gl.ARRAY_BUFFER, pulsePosBuf);
345
+ gl.bufferSubData(gl.ARRAY_BUFFER, 0, pulsePos);
346
+ gl.bindBuffer(gl.ARRAY_BUFFER, pulseFrontBuf);
347
+ gl.bufferSubData(gl.ARRAY_BUFFER, 0, pulseFront);
348
+ bind(points, 'position', pulsePosBuf, 3);
349
+ bind(points, 'size', pulseSize, 1);
350
+ bind(points, 'front', pulseFrontBuf, 1);
351
+ gl.disableVertexAttribArray(points.a('color'));
352
+ gl.vertexAttrib3f(points.a('color'), 1, 1, 1);
353
+ gl.drawArrays(gl.POINTS, 0, pulses);
354
+ }
355
+ };
356
+ const still = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
357
+ let raf = 0;
358
+ let start = 0;
359
+ let onScreen = false;
360
+ const loop = (now) => {
361
+ start || (start = now);
362
+ draw((now - start) / 1000);
363
+ raf = requestAnimationFrame(loop);
364
+ };
365
+ const run = () => {
366
+ cancelAnimationFrame(raf);
367
+ if (still)
368
+ draw(SETTLED);
369
+ else if (onScreen)
370
+ raf = requestAnimationFrame(loop);
371
+ };
372
+ const sized = new ResizeObserver(() => {
373
+ resize();
374
+ if (still)
375
+ draw(SETTLED);
376
+ });
377
+ const seen = new IntersectionObserver(([entry]) => {
378
+ onScreen = entry.isIntersecting;
379
+ run();
380
+ });
381
+ resize();
382
+ sized.observe(canvas);
383
+ seen.observe(canvas);
384
+ return () => {
385
+ cancelAnimationFrame(raf);
386
+ sized.disconnect();
387
+ seen.disconnect();
388
+ };
389
+ }
@@ -0,0 +1,7 @@
1
+ export { Dust } from './Dust';
2
+ export { GraphGalaxies } from './GraphGalaxies';
3
+ export { GraphOrb } from './GraphOrb';
4
+ export { GraphStage } from './GraphStage';
5
+ export { GridFloor } from './GridFloor';
6
+ export { NodeGlow } from './NodeGlow';
7
+ export { DEFAULT_PALETTE, PALETTE_VARS, type Rgb } from './palette';
@@ -0,0 +1,7 @@
1
+ export { Dust } from './Dust';
2
+ export { GraphGalaxies } from './GraphGalaxies';
3
+ export { GraphOrb } from './GraphOrb';
4
+ export { GraphStage } from './GraphStage';
5
+ export { GridFloor } from './GridFloor';
6
+ export { NodeGlow } from './NodeGlow';
7
+ export { DEFAULT_PALETTE, PALETTE_VARS } from './palette';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The colours of the graph stage. An app names its three brand colours, each with a lighter
3
+ * shade, as CSS variables holding space-separated RGB (`--graph-1: 34 211 238`); the defaults
4
+ * are in styles/graph-stage.css. The SVG and CSS pieces use the variables directly, and the
5
+ * live canvas reads them once when it starts.
6
+ */
7
+ export type Rgb = [number, number, number];
8
+ /** In palette order: each colour, then its lighter shade. */
9
+ export declare const PALETTE_VARS: readonly ["--graph-1", "--graph-1-light", "--graph-2", "--graph-2-light", "--graph-3", "--graph-3-light"];
10
+ /** The RoboSystems colours (cyan, blue, indigo), for where no variables are set. */
11
+ export declare const DEFAULT_PALETTE: Rgb[];
12
+ /** A palette colour as CSS, with an optional opacity. */
13
+ export declare const ink: (index: number, alpha?: number) => string;
14
+ /** The palette an element inherits, as 0..1 channels; the default where a variable is unset. */
15
+ export declare function readPalette(el: Element): Rgb[];
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The colours of the graph stage. An app names its three brand colours, each with a lighter
3
+ * shade, as CSS variables holding space-separated RGB (`--graph-1: 34 211 238`); the defaults
4
+ * are in styles/graph-stage.css. The SVG and CSS pieces use the variables directly, and the
5
+ * live canvas reads them once when it starts.
6
+ */
7
+ /** In palette order: each colour, then its lighter shade. */
8
+ export const PALETTE_VARS = [
9
+ '--graph-1',
10
+ '--graph-1-light',
11
+ '--graph-2',
12
+ '--graph-2-light',
13
+ '--graph-3',
14
+ '--graph-3-light',
15
+ ];
16
+ /** The RoboSystems colours (cyan, blue, indigo), for where no variables are set. */
17
+ export const DEFAULT_PALETTE = [
18
+ [0.13, 0.83, 0.93],
19
+ [0.4, 0.91, 0.98],
20
+ [0.23, 0.48, 0.96],
21
+ [0.38, 0.65, 0.98],
22
+ [0.39, 0.4, 0.95],
23
+ [0.51, 0.55, 0.97],
24
+ ];
25
+ /** A palette colour as CSS, with an optional opacity. */
26
+ export const ink = (index, alpha) => `rgb(var(${PALETTE_VARS[index]})${alpha === undefined ? '' : ` / ${alpha}`})`;
27
+ /** The palette an element inherits, as 0..1 channels; the default where a variable is unset. */
28
+ export function readPalette(el) {
29
+ const style = getComputedStyle(el);
30
+ return PALETTE_VARS.map((name, i) => {
31
+ const parts = style
32
+ .getPropertyValue(name)
33
+ .trim()
34
+ .split(/[\s,]+/)
35
+ .map(Number);
36
+ return parts.length === 3 && parts.every(Number.isFinite)
37
+ ? parts.map((v) => v / 255)
38
+ : DEFAULT_PALETTE[i];
39
+ });
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robosystems/core",
3
- "version": "0.21.7",
3
+ "version": "0.22.1",
4
4
  "description": "Shared React core for the RoboSystems apps",
5
5
  "license": "MIT",
6
6
  "private": false,