@supersuit/components 0.0.0-stage → 0.3.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/CHANGELOG.md +55 -0
- package/LICENSE +21 -0
- package/README.md +338 -2
- package/bin/supersuit-theme.mjs +116 -0
- package/dist/app-shell/app-shell.d.ts +68 -0
- package/dist/app-shell/app-shell.js +62 -0
- package/dist/app-shell/index.d.ts +3 -0
- package/dist/app-shell/index.js +5 -0
- package/dist/app-shell/sheet.d.ts +14 -0
- package/dist/app-shell/sheet.js +57 -0
- package/dist/app-shell/styles.css +112 -0
- package/dist/check/depth/bar.d.ts +27 -0
- package/dist/check/depth/bar.js +40 -0
- package/dist/check/depth/check.d.ts +40 -0
- package/dist/check/depth/check.js +50 -0
- package/dist/check/depth/crop.d.ts +24 -0
- package/dist/check/depth/crop.js +126 -0
- package/dist/check/depth/index.d.ts +3 -0
- package/dist/check/depth/index.js +5 -0
- package/dist/check/depth/measure.d.ts +42 -0
- package/dist/check/depth/measure.js +141 -0
- package/dist/check/depth/parts-page.d.ts +6 -0
- package/dist/check/depth/parts-page.js +69 -0
- package/dist/check/depth/playwright.d.ts +4 -0
- package/dist/check/depth/playwright.js +10 -0
- package/dist/check/depth/png.d.ts +3 -0
- package/dist/check/depth/png.js +12 -0
- package/dist/check/depth/shoot.d.ts +37 -0
- package/dist/check/depth/shoot.js +30 -0
- package/dist/choice-chips/choice-chips.d.ts +40 -0
- package/dist/choice-chips/choice-chips.js +106 -0
- package/dist/choice-chips/index.d.ts +1 -0
- package/dist/choice-chips/index.js +3 -0
- package/dist/choice-chips/styles.css +38 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +11 -0
- package/dist/page-header/index.d.ts +2 -0
- package/dist/page-header/index.js +4 -0
- package/dist/page-header/page-header.d.ts +58 -0
- package/dist/page-header/page-header.js +124 -0
- package/dist/page-header/shell-context.d.ts +12 -0
- package/dist/page-header/shell-context.js +21 -0
- package/dist/page-header/styles.css +77 -0
- package/dist/row/index.d.ts +1 -0
- package/dist/row/index.js +3 -0
- package/dist/row/row.d.ts +76 -0
- package/dist/row/row.js +69 -0
- package/dist/row/styles.css +69 -0
- package/dist/sound/engine.d.ts +59 -0
- package/dist/sound/engine.js +325 -0
- package/dist/sound/index.d.ts +18 -0
- package/dist/sound/index.js +10 -0
- package/dist/sound/synth.d.ts +24 -0
- package/dist/sound/synth.js +139 -0
- package/dist/theme/color.d.ts +29 -0
- package/dist/theme/color.js +116 -0
- package/dist/theme/compile.d.ts +23 -0
- package/dist/theme/compile.js +138 -0
- package/dist/theme/index.d.ts +3 -0
- package/dist/theme/index.js +4 -0
- package/dist/theme/record.d.ts +90 -0
- package/dist/theme/record.js +222 -0
- package/dist/voice-field/box-trace.d.ts +18 -0
- package/dist/voice-field/box-trace.js +87 -0
- package/dist/voice-field/index.d.ts +8 -0
- package/dist/voice-field/index.js +10 -0
- package/dist/voice-field/recorder.d.ts +71 -0
- package/dist/voice-field/recorder.js +211 -0
- package/dist/voice-field/rules.d.ts +27 -0
- package/dist/voice-field/rules.js +62 -0
- package/dist/voice-field/saved-take.d.ts +7 -0
- package/dist/voice-field/saved-take.js +66 -0
- package/dist/voice-field/store.d.ts +27 -0
- package/dist/voice-field/store.js +40 -0
- package/dist/voice-field/styles.css +161 -0
- package/dist/voice-field/toast.d.ts +13 -0
- package/dist/voice-field/toast.js +26 -0
- package/dist/voice-field/voice-field.d.ts +80 -0
- package/dist/voice-field/voice-field.js +259 -0
- package/dist/voice-field/words.d.ts +37 -0
- package/dist/voice-field/words.js +33 -0
- package/package.json +126 -4
- package/specs/brand-theme/v1/SPEC.md +130 -0
- package/specs/brand-theme/v1/default-bar.json +1196 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// The sound engine: one module that plays the nine interface sounds and their haptics, for every app.
|
|
2
|
+
//
|
|
3
|
+
// The haptics and the audio unlock are Supertools' feel.ts (helm2/src/lib/feel.ts, the operator, 2026-11-19),
|
|
4
|
+
// ported without a behaviour change and generalised from three moments to the nine names:
|
|
5
|
+
// - Android (Chrome): navigator.vibrate with short patterns, only once the person has touched the page.
|
|
6
|
+
// - iPhone (Safari 18+): no vibrate. Toggling a hidden <input type="checkbox" switch> through its label makes the
|
|
7
|
+
// Taptic Engine tick, and only inside the person's own tap, so a haptic outside a gesture is skipped, never faked.
|
|
8
|
+
// - A computer (no coarse pointer): no haptics. Haptics are off under prefers-reduced-motion.
|
|
9
|
+
// The sounds are made in code (synth.ts) from the brand's recipe, once per name per recipe, into an AudioBuffer.
|
|
10
|
+
// The audio context is made just after the first tap (made inside it, it held a Chromium tap back ~116 ms,
|
|
11
|
+
// 2026-11-21); a tap that plays a sound at once still makes it inside itself, which the browser requires.
|
|
12
|
+
// The page's audio session (navigator.audioSession) is never set here (supersuit-dev#3900: a type set once holds
|
|
13
|
+
// for the whole page and silenced videos and the microphone on iPhone).
|
|
14
|
+
//
|
|
15
|
+
// Three settings, kept on this device as `supersuit:sound:<key>` = 'on' | 'off' (design call `sound-default`,
|
|
16
|
+
// 2026-12-01): `sounds` (on) silences every sound; `pressSounds` (off) adds the press sounds (tap, toggles,
|
|
17
|
+
// sheet-open) to the moment sounds; `haptics` (on) is its own row.
|
|
18
|
+
// A sound that fails to make or play is silent and logs once: a sound is never worth an error on screen.
|
|
19
|
+
import { makeSound, MAISON_SOUNDS, PRESS_SOUNDS } from './synth.js';
|
|
20
|
+
export const SETTING_DEFAULTS = { sounds: true, pressSounds: false, haptics: true };
|
|
21
|
+
export const settingKey = (key) => `supersuit:sound:${key}`;
|
|
22
|
+
/** The primary faces Supertools draws as buttons ("BUTTONS LOOK LIKE BUTTONS"), and a card's recommended answer:
|
|
23
|
+
* feel.ts's PRIMARY, kept until the raw-control baseline reaches zero. A switch, a radio, a tab or a pressed chip
|
|
24
|
+
* is a choice, not a button. `data-feel="tap"` opts any other control in. */
|
|
25
|
+
export const PRIMARY = [
|
|
26
|
+
':is([data-slot="button"][data-variant="default"], button[class~="bg-primary"], a[class~="bg-primary"]):not([role="switch"], [role="radio"], [role="tab"], [aria-pressed], [aria-checked])',
|
|
27
|
+
'button.nx-ans.rec',
|
|
28
|
+
'[data-feel="tap"]',
|
|
29
|
+
].join(', ');
|
|
30
|
+
/** Android patterns, in milliseconds (vibrate, pause, vibrate ...). */
|
|
31
|
+
export const PATTERNS = {
|
|
32
|
+
tap: [10], 'toggle-on': [10], 'toggle-off': [10], 'record-start': [10], 'record-stop': [10], send: [10], 'sheet-open': [10],
|
|
33
|
+
success: [12, 70, 18], error: [30, 60, 30, 60, 30],
|
|
34
|
+
};
|
|
35
|
+
/** iPhone ticks: one for a tap, two for success, three for an error, one for the rest. */
|
|
36
|
+
export const TICKS = {
|
|
37
|
+
tap: 1, 'toggle-on': 1, 'toggle-off': 1, 'record-start': 1, 'record-stop': 1, send: 1, 'sheet-open': 1,
|
|
38
|
+
success: 2, error: 3,
|
|
39
|
+
};
|
|
40
|
+
/** The gap between iPhone ticks. A later tick goes only while the browser still counts the tap as active. */
|
|
41
|
+
export const TICK_GAP_MS = 90;
|
|
42
|
+
const globalEnv = () => ({
|
|
43
|
+
win: typeof window === 'undefined' ? null : window,
|
|
44
|
+
doc: typeof document === 'undefined' ? null : document,
|
|
45
|
+
nav: typeof navigator === 'undefined' ? null : navigator,
|
|
46
|
+
storage: (() => { try {
|
|
47
|
+
return typeof localStorage === 'undefined' ? null : localStorage;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return null;
|
|
51
|
+
} })(),
|
|
52
|
+
now: () => (typeof performance === 'undefined' ? Date.now() : performance.now()),
|
|
53
|
+
});
|
|
54
|
+
const isPress = (name) => PRESS_SOUNDS.includes(name);
|
|
55
|
+
export function makeSoundEngine(envOf = globalEnv) {
|
|
56
|
+
let ctx = null;
|
|
57
|
+
let recipe = MAISON_SOUNDS;
|
|
58
|
+
let tapSelector = PRIMARY;
|
|
59
|
+
const buffers = new Map();
|
|
60
|
+
let warned = false;
|
|
61
|
+
/** True while one of the person's own events is being dispatched (set in capture, cleared after). */
|
|
62
|
+
let tapping = false;
|
|
63
|
+
let tappedOnce = false;
|
|
64
|
+
let lastStrongAt = -Infinity;
|
|
65
|
+
let label = null;
|
|
66
|
+
let unlockQueued = false;
|
|
67
|
+
function setting(key) {
|
|
68
|
+
try {
|
|
69
|
+
const v = envOf().storage?.getItem(settingKey(key));
|
|
70
|
+
return v === 'on' ? true : v === 'off' ? false : SETTING_DEFAULTS[key];
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return SETTING_DEFAULTS[key];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function setSetting(key, on) {
|
|
77
|
+
try {
|
|
78
|
+
envOf().storage?.setItem(settingKey(key), on ? 'on' : 'off');
|
|
79
|
+
}
|
|
80
|
+
catch { /* private window: this visit only */ }
|
|
81
|
+
}
|
|
82
|
+
const media = (q) => { try {
|
|
83
|
+
return !!envOf().win?.matchMedia?.(q).matches;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return false;
|
|
87
|
+
} };
|
|
88
|
+
const inGesture = () => tapping || envOf().nav?.userActivation?.isActive === true;
|
|
89
|
+
const warnOnce = (err) => {
|
|
90
|
+
if (warned)
|
|
91
|
+
return;
|
|
92
|
+
warned = true;
|
|
93
|
+
try {
|
|
94
|
+
console.warn('[supersuit sound] a sound could not play; sounds stay silent', err);
|
|
95
|
+
}
|
|
96
|
+
catch { /* no console */ }
|
|
97
|
+
};
|
|
98
|
+
/** What this device can do. A computer has no haptics; a phone has vibrate (Android) or the switch tick (iPhone 18+). */
|
|
99
|
+
function haptics() {
|
|
100
|
+
const { nav, doc } = envOf();
|
|
101
|
+
if (!nav || !doc || !media('(pointer: coarse)') || media('(prefers-reduced-motion: reduce)'))
|
|
102
|
+
return null;
|
|
103
|
+
if (typeof nav.vibrate === 'function')
|
|
104
|
+
return 'vibrate';
|
|
105
|
+
if ('switch' in doc.createElement('input'))
|
|
106
|
+
return 'switch';
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
/** One tick of the Taptic Engine: the hidden switch, toggled through its label, inside the tap. */
|
|
110
|
+
function tick() {
|
|
111
|
+
const { doc } = envOf();
|
|
112
|
+
if (!doc)
|
|
113
|
+
return;
|
|
114
|
+
if (!label || !label.isConnected) {
|
|
115
|
+
label = doc.createElement('label');
|
|
116
|
+
label.setAttribute('aria-hidden', 'true');
|
|
117
|
+
label.dataset.feel = 'haptic';
|
|
118
|
+
label.style.cssText = 'position:fixed;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);opacity:0;pointer-events:none;left:-9999px';
|
|
119
|
+
const input = doc.createElement('input');
|
|
120
|
+
input.type = 'checkbox';
|
|
121
|
+
input.setAttribute('switch', '');
|
|
122
|
+
input.tabIndex = -1;
|
|
123
|
+
label.appendChild(input);
|
|
124
|
+
doc.body.appendChild(label);
|
|
125
|
+
}
|
|
126
|
+
label.click();
|
|
127
|
+
}
|
|
128
|
+
/** The haptic for one name, where this device has one and the haptics row is on. */
|
|
129
|
+
function haptic(name) {
|
|
130
|
+
if (!setting('haptics'))
|
|
131
|
+
return;
|
|
132
|
+
if (!isPress(name))
|
|
133
|
+
lastStrongAt = envOf().now();
|
|
134
|
+
const kind = haptics();
|
|
135
|
+
if (!kind)
|
|
136
|
+
return;
|
|
137
|
+
const { nav, win } = envOf();
|
|
138
|
+
if (kind === 'vibrate') {
|
|
139
|
+
// Chrome refuses vibrate before the first tap, and logs it: wait until the person has touched the page.
|
|
140
|
+
if (!(nav?.userActivation?.hasBeenActive ?? tappedOnce))
|
|
141
|
+
return;
|
|
142
|
+
try {
|
|
143
|
+
nav.vibrate(PATTERNS[name]);
|
|
144
|
+
}
|
|
145
|
+
catch { /* no vibration here */ }
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// iPhone: only inside the person's own tap. A later tick runs only while the browser still counts the tap as active.
|
|
149
|
+
if (!inGesture())
|
|
150
|
+
return;
|
|
151
|
+
tick();
|
|
152
|
+
for (let i = 1; i < TICKS[name]; i++)
|
|
153
|
+
win?.setTimeout(() => { if (envOf().nav?.userActivation?.isActive)
|
|
154
|
+
tick(); }, TICK_GAP_MS * i);
|
|
155
|
+
}
|
|
156
|
+
/** Make the audio context (a browser only lets sound start from a tap), with one silent sample to wake it. */
|
|
157
|
+
function unlock() {
|
|
158
|
+
if (!setting('sounds'))
|
|
159
|
+
return;
|
|
160
|
+
const { win } = envOf();
|
|
161
|
+
if (!win)
|
|
162
|
+
return;
|
|
163
|
+
if (!ctx) {
|
|
164
|
+
const AC = win.AudioContext ?? win.webkitAudioContext;
|
|
165
|
+
if (!AC)
|
|
166
|
+
return;
|
|
167
|
+
try {
|
|
168
|
+
ctx = new AC();
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
const src = ctx.createBufferSource();
|
|
175
|
+
src.buffer = ctx.createBuffer(1, 1, 22050);
|
|
176
|
+
src.connect(ctx.destination);
|
|
177
|
+
src.start(0);
|
|
178
|
+
}
|
|
179
|
+
catch { /* a context that cannot play yet resumes below */ }
|
|
180
|
+
}
|
|
181
|
+
if (ctx.state === 'suspended')
|
|
182
|
+
void ctx.resume().catch(() => { });
|
|
183
|
+
}
|
|
184
|
+
/** The unlock a tap asks for, made just after the tap's frame instead of inside it (see the header). */
|
|
185
|
+
function unlockSoon() {
|
|
186
|
+
if (ctx) {
|
|
187
|
+
unlock();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (unlockQueued || !setting('sounds'))
|
|
191
|
+
return;
|
|
192
|
+
const { win } = envOf();
|
|
193
|
+
if (!win)
|
|
194
|
+
return;
|
|
195
|
+
unlockQueued = true;
|
|
196
|
+
const go = () => { unlockQueued = false; unlock(); };
|
|
197
|
+
if (win.requestAnimationFrame)
|
|
198
|
+
win.requestAnimationFrame(() => { win.setTimeout(go, 0); });
|
|
199
|
+
else
|
|
200
|
+
win.setTimeout(go, 0);
|
|
201
|
+
}
|
|
202
|
+
function bufferFor(c, name) {
|
|
203
|
+
const rate = c.sampleRate ?? 48000;
|
|
204
|
+
const key = `${JSON.stringify(recipe)}|${rate}|${name}`;
|
|
205
|
+
let buf = buffers.get(key);
|
|
206
|
+
if (!buf) {
|
|
207
|
+
const samples = makeSound(recipe, name, rate);
|
|
208
|
+
buf = c.createBuffer(1, samples.length, rate);
|
|
209
|
+
buf.copyToChannel(samples, 0);
|
|
210
|
+
buffers.set(key, buf);
|
|
211
|
+
}
|
|
212
|
+
return buf;
|
|
213
|
+
}
|
|
214
|
+
/** Play one sound: silent with sounds off, and a press sound silent with press sounds off. */
|
|
215
|
+
function play(name) {
|
|
216
|
+
if (!setting('sounds'))
|
|
217
|
+
return;
|
|
218
|
+
if (isPress(name) && !setting('pressSounds'))
|
|
219
|
+
return;
|
|
220
|
+
if (!isPress(name))
|
|
221
|
+
lastStrongAt = envOf().now();
|
|
222
|
+
if (!ctx && inGesture())
|
|
223
|
+
unlock(); // a sound in the page's first tap: the browser only lets it start inside the tap
|
|
224
|
+
if (!ctx)
|
|
225
|
+
return;
|
|
226
|
+
try {
|
|
227
|
+
if (ctx.state === 'suspended')
|
|
228
|
+
void ctx.resume().catch(() => { });
|
|
229
|
+
const src = ctx.createBufferSource();
|
|
230
|
+
src.buffer = bufferFor(ctx, name);
|
|
231
|
+
src.connect(ctx.destination);
|
|
232
|
+
src.start();
|
|
233
|
+
}
|
|
234
|
+
catch (err) {
|
|
235
|
+
warnOnce(err);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/** Which sound a press makes, read from the element it landed in: `data-sound`, else the part's `data-slot`,
|
|
239
|
+
* else the tap selector (feel.ts's primary faces). A toggle names the state it is leaving. */
|
|
240
|
+
function soundFor(target, opts = {}) {
|
|
241
|
+
const named = target.closest('[data-sound]');
|
|
242
|
+
if (named)
|
|
243
|
+
return named.dataset.sound || null;
|
|
244
|
+
const slot = target.closest('[data-slot]');
|
|
245
|
+
switch (slot?.dataset.slot) {
|
|
246
|
+
case 'switch':
|
|
247
|
+
case 'checkbox':
|
|
248
|
+
return slot.getAttribute('data-state') === 'checked' ? 'toggle-off' : 'toggle-on';
|
|
249
|
+
case 'radio-group-item':
|
|
250
|
+
return 'toggle-on';
|
|
251
|
+
case 'button':
|
|
252
|
+
case 'tabs-trigger':
|
|
253
|
+
case 'select-item':
|
|
254
|
+
case 'dropdown-menu-item':
|
|
255
|
+
return 'tap';
|
|
256
|
+
}
|
|
257
|
+
return target.closest(opts.tap ?? tapSelector) ? 'tap' : null;
|
|
258
|
+
}
|
|
259
|
+
/** The press sounds, the tap haptic and the audio unlock, from one set of listeners on the document. Returns its removal. */
|
|
260
|
+
function install(doc = envOf().doc, opts = {}) {
|
|
261
|
+
if (opts.recipe)
|
|
262
|
+
recipe = opts.recipe;
|
|
263
|
+
if (opts.selectors?.tap)
|
|
264
|
+
tapSelector = opts.selectors.tap;
|
|
265
|
+
if (!doc)
|
|
266
|
+
return () => { };
|
|
267
|
+
const tap = tapSelector;
|
|
268
|
+
let pending = null;
|
|
269
|
+
// The hidden switch's own click (tick) is not the person's tap: it neither marks nor feels anything.
|
|
270
|
+
const ours = (e) => !!e.target?.closest?.('[data-feel="haptic"]');
|
|
271
|
+
// A handler that stops the event leaves no bubble to clear the mark, so it also clears once the event is done.
|
|
272
|
+
const mark = (e) => { if (ours(e))
|
|
273
|
+
return; tapping = true; tappedOnce = true; unlockSoon(); envOf().win?.setTimeout(() => { tapping = false; }, 0); };
|
|
274
|
+
const end = () => { tapping = false; };
|
|
275
|
+
// Capture: name the sound before any handler runs, so a toggle is read in the state it is leaving.
|
|
276
|
+
const onClickCapture = (e) => {
|
|
277
|
+
mark(e);
|
|
278
|
+
if (ours(e))
|
|
279
|
+
return;
|
|
280
|
+
const t = e.target;
|
|
281
|
+
pending = t?.closest ? soundFor(t, { tap }) : null;
|
|
282
|
+
};
|
|
283
|
+
const onClick = (e) => {
|
|
284
|
+
if (ours(e))
|
|
285
|
+
return;
|
|
286
|
+
tapping = true;
|
|
287
|
+
tappedOnce = true;
|
|
288
|
+
const name = pending;
|
|
289
|
+
pending = null;
|
|
290
|
+
try {
|
|
291
|
+
// A moment felt in this same tap (a handler ran first) is the stronger feel; no light press on top.
|
|
292
|
+
if (envOf().now() - lastStrongAt <= 300)
|
|
293
|
+
return;
|
|
294
|
+
const el = e.target?.closest?.(tap);
|
|
295
|
+
if (el)
|
|
296
|
+
haptic('tap');
|
|
297
|
+
if (name)
|
|
298
|
+
play(name);
|
|
299
|
+
}
|
|
300
|
+
finally {
|
|
301
|
+
tapping = false;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
const types = ['pointerdown', 'touchend', 'keydown'];
|
|
305
|
+
for (const t of types) {
|
|
306
|
+
doc.addEventListener(t, mark, true);
|
|
307
|
+
doc.addEventListener(t, end, false);
|
|
308
|
+
}
|
|
309
|
+
doc.addEventListener('click', onClickCapture, true);
|
|
310
|
+
doc.addEventListener('click', onClick, false);
|
|
311
|
+
return () => {
|
|
312
|
+
for (const t of types) {
|
|
313
|
+
doc.removeEventListener(t, mark, true);
|
|
314
|
+
doc.removeEventListener(t, end, false);
|
|
315
|
+
}
|
|
316
|
+
doc.removeEventListener('click', onClickCapture, true);
|
|
317
|
+
doc.removeEventListener('click', onClick, false);
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
play, setting, setSetting, soundFor, install, haptic, haptics, unlock,
|
|
322
|
+
/** for tests */
|
|
323
|
+
reset() { ctx = null; recipe = MAISON_SOUNDS; tapSelector = PRIMARY; buffers.clear(); warned = false; unlockQueued = false; tapping = false; tappedOnce = false; lastStrongAt = -Infinity; label?.remove(); label = null; },
|
|
324
|
+
};
|
|
325
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { makeSound, SOUND_NAMES, MOMENT_SOUNDS, PRESS_SOUNDS, MAISON_SOUNDS, GARDEN_SOUNDS } from './synth.js';
|
|
2
|
+
export type { SoundName, SoundRecipe, Samples } from './synth.js';
|
|
3
|
+
export { makeSoundEngine, PRIMARY, PATTERNS, TICKS, TICK_GAP_MS, SETTING_DEFAULTS, settingKey } from './engine.js';
|
|
4
|
+
export type { SoundEnv, SoundSetting, SoundEngine, InstallOptions } from './engine.js';
|
|
5
|
+
/** The page's one engine. */
|
|
6
|
+
export declare const sound: {
|
|
7
|
+
play: (name: import("./synth.js").SoundName) => void;
|
|
8
|
+
setting: (key: import("./engine.js").SoundSetting) => boolean;
|
|
9
|
+
setSetting: (key: import("./engine.js").SoundSetting, on: boolean) => void;
|
|
10
|
+
soundFor: (target: Element, opts?: {
|
|
11
|
+
tap?: string;
|
|
12
|
+
}) => import("./synth.js").SoundName | null;
|
|
13
|
+
install: (doc?: Document | null, opts?: import("./engine.js").InstallOptions) => () => void;
|
|
14
|
+
haptic: (name: import("./synth.js").SoundName) => void;
|
|
15
|
+
haptics: () => "vibrate" | "switch" | null;
|
|
16
|
+
unlock: () => void;
|
|
17
|
+
reset(): void;
|
|
18
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// @supersuit/components/sound: the interface sounds and haptics, one engine for every app. No React, so raw
|
|
2
|
+
// JavaScript imports it too.
|
|
3
|
+
// import { sound } from '@supersuit/components/sound'
|
|
4
|
+
// sound.install(document, { recipe: theme.sounds }) // the listener, the unlock, the tap haptic
|
|
5
|
+
// sound.play('success'); sound.haptic('success')
|
|
6
|
+
export { makeSound, SOUND_NAMES, MOMENT_SOUNDS, PRESS_SOUNDS, MAISON_SOUNDS, GARDEN_SOUNDS } from './synth.js';
|
|
7
|
+
export { makeSoundEngine, PRIMARY, PATTERNS, TICKS, TICK_GAP_MS, SETTING_DEFAULTS, settingKey } from './engine.js';
|
|
8
|
+
import { makeSoundEngine } from './engine.js';
|
|
9
|
+
/** The page's one engine. */
|
|
10
|
+
export const sound = makeSoundEngine();
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const SOUND_NAMES: readonly ["tap", "toggle-on", "toggle-off", "record-start", "record-stop", "send", "success", "error", "sheet-open"];
|
|
2
|
+
export type SoundName = (typeof SOUND_NAMES)[number];
|
|
3
|
+
/** The moments a person is told about: on by default (call `sound-default`, 2026-12-01). */
|
|
4
|
+
export declare const MOMENT_SOUNDS: readonly ["success", "error", "send", "record-start", "record-stop"];
|
|
5
|
+
/** The presses: off until the person turns on "All interface sounds". */
|
|
6
|
+
export declare const PRESS_SOUNDS: readonly ["tap", "toggle-on", "toggle-off", "sheet-open"];
|
|
7
|
+
/** The two families of sound: soft wooden knocks, or rounder bubbles. */
|
|
8
|
+
export type SoundFamily = 'wood' | 'bubble';
|
|
9
|
+
export interface SoundRecipe {
|
|
10
|
+
family: SoundFamily;
|
|
11
|
+
/** The tap's base note, in Hz. */
|
|
12
|
+
pitch: number;
|
|
13
|
+
/** 1 is the approved sound; under 1 is duller, over 1 brighter. */
|
|
14
|
+
brightness: number;
|
|
15
|
+
success: 'marimba' | 'bell';
|
|
16
|
+
/** The tap's peak, in dBFS. */
|
|
17
|
+
level: number;
|
|
18
|
+
}
|
|
19
|
+
export declare const MAISON_SOUNDS: SoundRecipe;
|
|
20
|
+
export declare const GARDEN_SOUNDS: SoundRecipe;
|
|
21
|
+
/** Mono samples on a plain ArrayBuffer, which AudioBuffer.copyToChannel takes. */
|
|
22
|
+
export type Samples = Float32Array<ArrayBuffer>;
|
|
23
|
+
/** One sound's samples, mono, at the given rate (the audio context's own, so nothing is resampled). */
|
|
24
|
+
export declare function makeSound(recipe: SoundRecipe, name: SoundName, sampleRate?: number): Samples;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// The interface sounds, made in code from a recipe. Pure: same recipe, same samples, in any browser or in Node.
|
|
2
|
+
//
|
|
3
|
+
// Ported from the approved vibe check (component-vibe/tools/make-sounds.mjs, round 3, 2026-12-01): the same
|
|
4
|
+
// primitives (seeded noise, decaying partials, the wooden bar, the marimba, the bubble, the bell) and both
|
|
5
|
+
// families, moved from a build script that wrote .m4a files through macOS afconvert into a function the engine
|
|
6
|
+
// calls on first use. No audio files, no macOS tool.
|
|
7
|
+
//
|
|
8
|
+
// A recipe (`brand-theme/v1` `sounds`) moves the approved sound, it never replaces it:
|
|
9
|
+
// - pitch: every frequency is scaled by pitch / the family's tap pitch (wood 1050 Hz, bubble 1500 Hz);
|
|
10
|
+
// - brightness: scales the noise low-pass cutoffs and the upper partials' amplitudes (1 = approved);
|
|
11
|
+
// - success: the success voice, marimba (Maison) or bell (Garden), whatever the family;
|
|
12
|
+
// - level: the tap peaks at 10^(level/20); every other sound keeps its approved peak ratio to the tap.
|
|
13
|
+
export const SOUND_NAMES = ['tap', 'toggle-on', 'toggle-off', 'record-start', 'record-stop', 'send', 'success', 'error', 'sheet-open'];
|
|
14
|
+
/** The moments a person is told about: on by default (call `sound-default`, 2026-12-01). */
|
|
15
|
+
export const MOMENT_SOUNDS = ['success', 'error', 'send', 'record-start', 'record-stop'];
|
|
16
|
+
/** The presses: off until the person turns on "All interface sounds". */
|
|
17
|
+
export const PRESS_SOUNDS = ['tap', 'toggle-on', 'toggle-off', 'sheet-open'];
|
|
18
|
+
export const MAISON_SOUNDS = { family: 'wood', pitch: 1050, brightness: 1, success: 'marimba', level: -12 };
|
|
19
|
+
export const GARDEN_SOUNDS = { family: 'bubble', pitch: 1500, brightness: 1, success: 'bell', level: -12 };
|
|
20
|
+
const FAMILY_TAP_PITCH = { wood: 1050, bubble: 1500 };
|
|
21
|
+
/** make-sounds' finish() peaks were relative to this tap peak. */
|
|
22
|
+
const APPROVED_TAP_PEAK = 0.25;
|
|
23
|
+
/** mulberry32, so the noise is the same every run. */
|
|
24
|
+
function rng(seed) {
|
|
25
|
+
return () => { seed |= 0; seed = (seed + 0x6D2B79F5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
|
|
26
|
+
}
|
|
27
|
+
function kit(SR, recipe) {
|
|
28
|
+
const k = recipe.pitch / FAMILY_TAP_PITCH[recipe.family];
|
|
29
|
+
const b = recipe.brightness;
|
|
30
|
+
const gain = 10 ** (recipe.level / 20) / APPROVED_TAP_PEAK;
|
|
31
|
+
const ms = (x) => Math.round((x / 1000) * SR);
|
|
32
|
+
const buffer = (lengthMs) => new Float32Array(ms(lengthMs));
|
|
33
|
+
/** One decaying partial. glide raises the pitch by that fraction over glideMs (a bubble); negative falls. Unscaled. */
|
|
34
|
+
function raw(out, { at = 0, f, amp = 1, decay = 30, attack = 1.5, glide = 0, glideMs = 60 }) {
|
|
35
|
+
const start = ms(at), n = out.length - start;
|
|
36
|
+
let phase = 0;
|
|
37
|
+
for (let i = 0; i < n; i++) {
|
|
38
|
+
const t = (i / SR) * 1000;
|
|
39
|
+
const env = (t < attack ? t / attack : 1) * Math.exp(-Math.max(0, t - attack) / decay);
|
|
40
|
+
if (env < 1e-5 && t > attack)
|
|
41
|
+
break;
|
|
42
|
+
const fNow = f * (1 + glide * Math.min(t / glideMs, 1));
|
|
43
|
+
phase += (2 * Math.PI * fNow) / SR;
|
|
44
|
+
out[start + i] += amp * env * Math.sin(phase);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** A fundamental at the recipe's pitch. */
|
|
48
|
+
const partial = (out, o) => raw(out, { ...o, f: o.f * k });
|
|
49
|
+
/** An upper partial: its amplitude follows brightness. */
|
|
50
|
+
const upper = (out, o) => raw(out, { ...o, f: o.f * k, amp: (o.amp ?? 1) * b });
|
|
51
|
+
/** A struck small wooden bar: three inharmonic modes that die fast. */
|
|
52
|
+
function wood(out, { at = 0, f, amp = 1, decay = 22 }) {
|
|
53
|
+
partial(out, { at, f, amp, decay });
|
|
54
|
+
upper(out, { at, f: f * 2.57, amp: amp * 0.32, decay: decay * 0.55 });
|
|
55
|
+
upper(out, { at, f: f * 4.21, amp: amp * 0.1, decay: decay * 0.3 });
|
|
56
|
+
}
|
|
57
|
+
/** A marimba bar: the warm note under the success sound. */
|
|
58
|
+
function marimba(out, { at = 0, f, amp = 1, decay = 260 }) {
|
|
59
|
+
partial(out, { at, f, amp, decay, attack: 2 });
|
|
60
|
+
upper(out, { at, f: f * 3.93, amp: amp * 0.22, decay: decay * 0.25, attack: 1 });
|
|
61
|
+
upper(out, { at, f: f * 9.2, amp: amp * 0.05, decay: decay * 0.08, attack: 0.5 });
|
|
62
|
+
}
|
|
63
|
+
/** A round bubble: a sine that glides up, with a little second harmonic. */
|
|
64
|
+
function bubble(out, { at = 0, f, amp = 1, decay = 34, glide = 0.28, glideMs = 40 }) {
|
|
65
|
+
partial(out, { at, f, amp, decay, attack: 2.5, glide, glideMs });
|
|
66
|
+
upper(out, { at, f: f * 2, amp: amp * 0.12, decay: decay * 0.6, attack: 2.5, glide, glideMs });
|
|
67
|
+
}
|
|
68
|
+
/** A small bell: near-harmonic partials, longer ring. */
|
|
69
|
+
function bell(out, { at = 0, f, amp = 1, decay = 230 }) {
|
|
70
|
+
partial(out, { at, f, amp, decay, attack: 3 });
|
|
71
|
+
upper(out, { at, f: f * 2.01, amp: amp * 0.18, decay: decay * 0.5, attack: 2 });
|
|
72
|
+
upper(out, { at, f: f * 3.02, amp: amp * 0.05, decay: decay * 0.25, attack: 2 });
|
|
73
|
+
}
|
|
74
|
+
/** Filtered noise: felt, air, a swoosh. lp/lpEnd sweep a one-pole low-pass (scaled by brightness); hp removes rumble. */
|
|
75
|
+
function noise(out, { at = 0, dur = 20, amp = 0.3, lp = 2000, lpEnd = lp, hp = 80, shape = 'hit', seed = 7 }) {
|
|
76
|
+
lp *= b;
|
|
77
|
+
lpEnd *= b;
|
|
78
|
+
const r = rng(seed), start = ms(at), n = Math.min(ms(dur), out.length - start);
|
|
79
|
+
let low = 0, prevIn = 0, prevHp = 0;
|
|
80
|
+
for (let i = 0; i < n; i++) {
|
|
81
|
+
const u = i / n;
|
|
82
|
+
const cutoff = lp + (lpEnd - lp) * u;
|
|
83
|
+
const a = 1 - Math.exp((-2 * Math.PI * cutoff) / SR);
|
|
84
|
+
const white = r() * 2 - 1;
|
|
85
|
+
low += a * (white - low);
|
|
86
|
+
const rc = 1 / (2 * Math.PI * hp), dt = 1 / SR, kk = rc / (rc + dt);
|
|
87
|
+
const high = kk * (prevHp + low - prevIn);
|
|
88
|
+
prevIn = low;
|
|
89
|
+
prevHp = high;
|
|
90
|
+
const env = shape === 'swell' ? Math.sin(Math.PI * u) ** 1.6 : Math.min(1, (i / SR) * 1000 / 0.8) * Math.exp(-u * 5);
|
|
91
|
+
out[start + i] += amp * env * high;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Normalise to the approved peak p, moved to the recipe level, with a 6 ms fade so nothing clicks. */
|
|
95
|
+
function finish(out, p = APPROVED_TAP_PEAK) {
|
|
96
|
+
const peak = p * gain;
|
|
97
|
+
let max = 0;
|
|
98
|
+
for (const v of out)
|
|
99
|
+
max = Math.max(max, Math.abs(v));
|
|
100
|
+
const g = max > 0 ? peak / max : 0;
|
|
101
|
+
const fade = ms(6);
|
|
102
|
+
for (let i = 0; i < out.length; i++) {
|
|
103
|
+
const tail = out.length - i;
|
|
104
|
+
out[i] *= g * (tail < fade ? tail / fade : 1);
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
const success = {
|
|
109
|
+
marimba: () => { const o = buffer(620); marimba(o, { f: 523.25, amp: 0.85 }); marimba(o, { at: 110, f: 783.99 }); return finish(o, 0.24); },
|
|
110
|
+
bell: () => { const o = buffer(680); bell(o, { f: 659.25, amp: 0.8 }); bell(o, { at: 95, f: 987.77, amp: 0.8 }); bell(o, { at: 190, f: 1318.5 }); return finish(o, 0.22); },
|
|
111
|
+
};
|
|
112
|
+
const families = {
|
|
113
|
+
wood: {
|
|
114
|
+
tap: () => { const o = buffer(90); wood(o, { f: 1050 }); noise(o, { dur: 10, amp: 0.25, lp: 2600 }); return finish(o); },
|
|
115
|
+
'toggle-on': () => { const o = buffer(130); wood(o, { f: 820, amp: 0.8 }); wood(o, { at: 45, f: 1180 }); return finish(o); },
|
|
116
|
+
'toggle-off': () => { const o = buffer(130); wood(o, { f: 1120 }); wood(o, { at: 45, f: 760, amp: 0.75 }); return finish(o, 0.22); },
|
|
117
|
+
'record-start': () => { const o = buffer(180); wood(o, { f: 660, decay: 30 }); wood(o, { at: 60, f: 990, decay: 34 }); noise(o, { dur: 14, amp: 0.2, lp: 900 }); return finish(o); },
|
|
118
|
+
'record-stop': () => { const o = buffer(180); wood(o, { f: 990, decay: 30 }); wood(o, { at: 60, f: 600, decay: 40 }); return finish(o); },
|
|
119
|
+
send: () => { const o = buffer(195); noise(o, { dur: 120, amp: 0.5, lp: 500, lpEnd: 4200, hp: 200, shape: 'swell', seed: 11 }); wood(o, { at: 112, f: 1400, amp: 0.7, decay: 18 }); return finish(o); },
|
|
120
|
+
error: () => { const o = buffer(195); wood(o, { f: 300, decay: 40 }); noise(o, { dur: 16, amp: 0.25, lp: 700 }); wood(o, { at: 95, f: 250, decay: 40 }); noise(o, { at: 95, dur: 16, amp: 0.25, lp: 700, seed: 9 }); return finish(o, 0.24); },
|
|
121
|
+
'sheet-open': () => { const o = buffer(190); noise(o, { dur: 150, amp: 0.6, lp: 260, lpEnd: 1700, hp: 60, shape: 'swell', seed: 5 }); partial(o, { f: 140, amp: 0.35, decay: 40, attack: 4 }); return finish(o, 0.22); },
|
|
122
|
+
},
|
|
123
|
+
bubble: {
|
|
124
|
+
tap: () => { const o = buffer(110); bubble(o, { f: 1500 }); return finish(o); },
|
|
125
|
+
'toggle-on': () => { const o = buffer(140); bubble(o, { f: 1150, amp: 0.8 }); bubble(o, { at: 45, f: 1750 }); return finish(o); },
|
|
126
|
+
'toggle-off': () => { const o = buffer(140); bubble(o, { f: 1700, glide: -0.12 }); bubble(o, { at: 45, f: 1120, glide: -0.12, amp: 0.8 }); return finish(o, 0.22); },
|
|
127
|
+
'record-start': () => { const o = buffer(185); bubble(o, { f: 880, decay: 40 }); bubble(o, { at: 60, f: 1320, decay: 44 }); return finish(o); },
|
|
128
|
+
'record-stop': () => { const o = buffer(185); bubble(o, { f: 1320, decay: 40, glide: -0.1 }); bubble(o, { at: 60, f: 880, decay: 48, glide: -0.1 }); return finish(o); },
|
|
129
|
+
send: () => { const o = buffer(195); partial(o, { f: 700, amp: 0.8, decay: 70, attack: 6, glide: 2.1, glideMs: 120 }); bubble(o, { at: 115, f: 2100, amp: 0.45, decay: 24 }); return finish(o); },
|
|
130
|
+
error: () => { const o = buffer(195); bubble(o, { f: 440, glide: -0.15, decay: 40 }); bubble(o, { at: 90, f: 370, glide: -0.15, decay: 44 }); return finish(o, 0.24); },
|
|
131
|
+
'sheet-open': () => { const o = buffer(190); partial(o, { f: 320, amp: 0.8, decay: 90, attack: 20, glide: 1.1, glideMs: 150 }); noise(o, { dur: 150, amp: 0.12, lp: 6000, hp: 2200, shape: 'swell', seed: 3 }); return finish(o, 0.2); },
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
return (name) => (name === 'success' ? success[recipe.success]() : families[recipe.family][name]());
|
|
135
|
+
}
|
|
136
|
+
/** One sound's samples, mono, at the given rate (the audio context's own, so nothing is resampled). */
|
|
137
|
+
export function makeSound(recipe, name, sampleRate = 48000) {
|
|
138
|
+
return kit(sampleRate, recipe)(name);
|
|
139
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type RGBA = {
|
|
2
|
+
r: number;
|
|
3
|
+
g: number;
|
|
4
|
+
b: number;
|
|
5
|
+
a: number;
|
|
6
|
+
};
|
|
7
|
+
export type RGB = {
|
|
8
|
+
r: number;
|
|
9
|
+
g: number;
|
|
10
|
+
b: number;
|
|
11
|
+
};
|
|
12
|
+
export declare function isHex(s: string): boolean;
|
|
13
|
+
export declare function parseHex(hex: string): RGBA;
|
|
14
|
+
export declare function toHex(c: RGB): string;
|
|
15
|
+
/** CSS text for a resolved colour: hex when opaque, `rgb(r g b / a)` otherwise. */
|
|
16
|
+
export declare function toCss(c: RGBA): string;
|
|
17
|
+
/** OKLab (Ottosson 2020) from sRGB 0-255. L is 0-1 here. */
|
|
18
|
+
export declare function toOklab(c: RGB): [number, number, number];
|
|
19
|
+
export declare function fromOklab([L, A, B]: [number, number, number]): RGB;
|
|
20
|
+
/** OKLab lightness on a 0 to 100 scale. */
|
|
21
|
+
export declare function oklabL(c: RGB): number;
|
|
22
|
+
/** `color-mix(in oklab, a, b p%)`: premultiplied alpha, as CSS Color 5 says. */
|
|
23
|
+
export declare function mixOklab(a: RGBA, b: RGBA, p: number): RGBA;
|
|
24
|
+
/** Resolve one colour expression against a theme's tokens. Throws with words a person can act on. */
|
|
25
|
+
export declare function resolveColor(expr: string, tokens: Record<string, string>): RGBA;
|
|
26
|
+
/** WCAG 2 contrast ratio. Accepts hex strings or resolved colours. */
|
|
27
|
+
export declare function contrast(a: RGB | string, b: RGB | string): number;
|
|
28
|
+
/** `r g b` channel text, for `--shade-rgb` and `--primary-glow-rgb`. */
|
|
29
|
+
export declare function rgbChannels(c: RGB): string;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Colour arithmetic for brand-theme/v1: the colour grammar, sRGB <-> OKLab, WCAG contrast.
|
|
2
|
+
// A colour value is a token name, `a / N%` (a token at N% opacity) or `mix(a, b N%)`
|
|
3
|
+
// (an OKLab mix of two tokens, b at N%, the same as CSS `color-mix(in oklab, a, b N%)`).
|
|
4
|
+
const HEX = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
5
|
+
const TOKEN = /^[a-z][a-z0-9-]*$/;
|
|
6
|
+
const ALPHA = /^([a-z][a-z0-9-]*)\s*\/\s*(\d+(?:\.\d+)?)%$/;
|
|
7
|
+
const MIX = /^mix\(\s*([a-z][a-z0-9-]*)\s*,\s*([a-z][a-z0-9-]*)\s+(\d+(?:\.\d+)?)%\s*\)$/;
|
|
8
|
+
export function isHex(s) {
|
|
9
|
+
return HEX.test(s.trim());
|
|
10
|
+
}
|
|
11
|
+
export function parseHex(hex) {
|
|
12
|
+
const m = HEX.exec(hex.trim());
|
|
13
|
+
if (!m)
|
|
14
|
+
throw new Error(`not a hex colour: ${hex}`);
|
|
15
|
+
let h = m[1];
|
|
16
|
+
if (h.length === 3)
|
|
17
|
+
h = h.split('').map((c) => c + c).join('');
|
|
18
|
+
const n = (i) => parseInt(h.slice(i, i + 2), 16);
|
|
19
|
+
return { r: n(0), g: n(2), b: n(4), a: h.length === 8 ? Math.round((n(6) / 255) * 1000) / 1000 : 1 };
|
|
20
|
+
}
|
|
21
|
+
export function toHex(c) {
|
|
22
|
+
const p = (v) => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, '0').toUpperCase();
|
|
23
|
+
return `#${p(c.r)}${p(c.g)}${p(c.b)}`;
|
|
24
|
+
}
|
|
25
|
+
/** CSS text for a resolved colour: hex when opaque, `rgb(r g b / a)` otherwise. */
|
|
26
|
+
export function toCss(c) {
|
|
27
|
+
return c.a >= 1 ? toHex(c) : `rgb(${c.r} ${c.g} ${c.b} / ${round(c.a, 3)})`;
|
|
28
|
+
}
|
|
29
|
+
const round = (v, d) => Math.round(v * 10 ** d) / 10 ** d;
|
|
30
|
+
function lin(v) {
|
|
31
|
+
const c = v / 255;
|
|
32
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
33
|
+
}
|
|
34
|
+
function delin(v) {
|
|
35
|
+
const c = v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055;
|
|
36
|
+
return Math.max(0, Math.min(255, Math.round(c * 255)));
|
|
37
|
+
}
|
|
38
|
+
/** OKLab (Ottosson 2020) from sRGB 0-255. L is 0-1 here. */
|
|
39
|
+
export function toOklab(c) {
|
|
40
|
+
const r = lin(c.r), g = lin(c.g), b = lin(c.b);
|
|
41
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
42
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
43
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
44
|
+
return [
|
|
45
|
+
0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
|
|
46
|
+
1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
|
|
47
|
+
0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
export function fromOklab([L, A, B]) {
|
|
51
|
+
const l = (L + 0.3963377774 * A + 0.2158037573 * B) ** 3;
|
|
52
|
+
const m = (L - 0.1055613458 * A - 0.0638541728 * B) ** 3;
|
|
53
|
+
const s = (L - 0.0894841775 * A - 1.291485548 * B) ** 3;
|
|
54
|
+
return {
|
|
55
|
+
r: delin(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s),
|
|
56
|
+
g: delin(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s),
|
|
57
|
+
b: delin(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** OKLab lightness on a 0 to 100 scale. */
|
|
61
|
+
export function oklabL(c) {
|
|
62
|
+
return toOklab(c)[0] * 100;
|
|
63
|
+
}
|
|
64
|
+
/** `color-mix(in oklab, a, b p%)`: premultiplied alpha, as CSS Color 5 says. */
|
|
65
|
+
export function mixOklab(a, b, p) {
|
|
66
|
+
const t = p / 100;
|
|
67
|
+
const alpha = a.a * (1 - t) + b.a * t;
|
|
68
|
+
const la = toOklab(a), lb = toOklab(b);
|
|
69
|
+
const out = [0, 1, 2].map((i) => (alpha === 0 ? 0 : (la[i] * a.a * (1 - t) + lb[i] * b.a * t) / alpha));
|
|
70
|
+
return { ...fromOklab(out), a: round(alpha, 3) };
|
|
71
|
+
}
|
|
72
|
+
/** Resolve one colour expression against a theme's tokens. Throws with words a person can act on. */
|
|
73
|
+
export function resolveColor(expr, tokens) {
|
|
74
|
+
const e = String(expr).trim();
|
|
75
|
+
const tok = (name) => {
|
|
76
|
+
const v = tokens[name];
|
|
77
|
+
if (v === undefined)
|
|
78
|
+
throw new Error(`unknown token "${name}"`);
|
|
79
|
+
if (!isHex(v))
|
|
80
|
+
throw new Error(`token "${name}" is not a hex colour`);
|
|
81
|
+
return parseHex(v);
|
|
82
|
+
};
|
|
83
|
+
if (isHex(e))
|
|
84
|
+
throw new Error(`bare hex ${e}: name it in tokens and use the token`);
|
|
85
|
+
let m = MIX.exec(e);
|
|
86
|
+
if (m) {
|
|
87
|
+
const p = Number(m[3]);
|
|
88
|
+
if (p > 100)
|
|
89
|
+
throw new Error(`mix over 100%: ${e}`);
|
|
90
|
+
return mixOklab(tok(m[1]), tok(m[2]), p);
|
|
91
|
+
}
|
|
92
|
+
m = ALPHA.exec(e);
|
|
93
|
+
if (m) {
|
|
94
|
+
const p = Number(m[2]);
|
|
95
|
+
if (p > 100)
|
|
96
|
+
throw new Error(`opacity over 100%: ${e}`);
|
|
97
|
+
return { ...tok(m[1]), a: round(p / 100, 3) };
|
|
98
|
+
}
|
|
99
|
+
if (TOKEN.test(e))
|
|
100
|
+
return tok(e);
|
|
101
|
+
throw new Error(`not a colour expression: "${e}" (a token, "a / N%" or "mix(a, b N%)")`);
|
|
102
|
+
}
|
|
103
|
+
function luminance(c) {
|
|
104
|
+
return 0.2126 * lin(c.r) + 0.7152 * lin(c.g) + 0.0722 * lin(c.b);
|
|
105
|
+
}
|
|
106
|
+
/** WCAG 2 contrast ratio. Accepts hex strings or resolved colours. */
|
|
107
|
+
export function contrast(a, b) {
|
|
108
|
+
const ca = typeof a === 'string' ? parseHex(a) : a;
|
|
109
|
+
const cb = typeof b === 'string' ? parseHex(b) : b;
|
|
110
|
+
const la = luminance(ca), lb = luminance(cb);
|
|
111
|
+
return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
|
|
112
|
+
}
|
|
113
|
+
/** `r g b` channel text, for `--shade-rgb` and `--primary-glow-rgb`. */
|
|
114
|
+
export function rgbChannels(c) {
|
|
115
|
+
return `${c.r} ${c.g} ${c.b}`;
|
|
116
|
+
}
|