glasslikeui 1.9.0 → 1.10.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.
Files changed (128) hide show
  1. package/README.md +892 -24
  2. package/dist/actions/adaptiveTone.d.ts +50 -0
  3. package/dist/actions/adaptiveTone.js +569 -0
  4. package/dist/actions/alertLogic.d.ts +67 -0
  5. package/dist/actions/alertLogic.js +123 -0
  6. package/dist/actions/anchorPlacement.d.ts +92 -0
  7. package/dist/actions/anchorPlacement.js +137 -0
  8. package/dist/actions/dragSnap.d.ts +1 -1
  9. package/dist/actions/dragSnapMath.d.ts +70 -0
  10. package/dist/actions/dragSnapMath.js +115 -0
  11. package/dist/actions/glassToneLogic.d.ts +182 -0
  12. package/dist/actions/glassToneLogic.js +513 -0
  13. package/dist/actions/heightTransition.d.ts +32 -0
  14. package/dist/actions/heightTransition.js +64 -0
  15. package/dist/actions/index.d.ts +1 -0
  16. package/dist/actions/index.js +1 -0
  17. package/dist/actions/inertSiblings.js +20 -4
  18. package/dist/actions/menuLogic.d.ts +40 -0
  19. package/dist/actions/menuLogic.js +68 -0
  20. package/dist/actions/modalDialog.d.ts +19 -0
  21. package/dist/actions/modalDialog.js +48 -0
  22. package/dist/actions/popoverLogic.d.ts +25 -0
  23. package/dist/actions/popoverLogic.js +48 -0
  24. package/dist/actions/rovingIndex.d.ts +30 -0
  25. package/dist/actions/rovingIndex.js +49 -0
  26. package/dist/actions/scrollRunTracker.d.ts +36 -0
  27. package/dist/actions/scrollRunTracker.js +77 -0
  28. package/dist/actions/scrollUnder.d.ts +21 -0
  29. package/dist/actions/scrollUnder.js +29 -0
  30. package/dist/actions/segmentedLogic.d.ts +38 -0
  31. package/dist/actions/segmentedLogic.js +59 -0
  32. package/dist/actions/sheetLogic.d.ts +43 -0
  33. package/dist/actions/sheetLogic.js +71 -0
  34. package/dist/actions/sliderLogic.d.ts +29 -0
  35. package/dist/actions/sliderLogic.js +53 -0
  36. package/dist/actions/swipeActions.d.ts +46 -2
  37. package/dist/actions/swipeActions.js +267 -46
  38. package/dist/actions/tabBarMinimize.d.ts +31 -0
  39. package/dist/actions/tabBarMinimize.js +38 -0
  40. package/dist/actions/tabViewLogic.d.ts +47 -0
  41. package/dist/actions/tabViewLogic.js +71 -0
  42. package/dist/actions/toggleKnobLogic.d.ts +57 -0
  43. package/dist/actions/toggleKnobLogic.js +66 -0
  44. package/dist/actions/toolbarFocus.d.ts +44 -0
  45. package/dist/actions/toolbarFocus.js +228 -0
  46. package/dist/actions/topLayer.d.ts +12 -0
  47. package/dist/actions/topLayer.js +18 -0
  48. package/dist/components/dialog/ActionSheet.svelte +457 -0
  49. package/dist/components/dialog/ActionSheet.svelte.d.ts +25 -0
  50. package/dist/components/dialog/Alert.svelte +337 -0
  51. package/dist/components/dialog/Alert.svelte.d.ts +28 -0
  52. package/dist/components/dialog/index.d.ts +6 -0
  53. package/dist/components/dialog/index.js +6 -0
  54. package/dist/components/glass/Glass.svelte +49 -17
  55. package/dist/components/glass/Glass.svelte.d.ts +8 -0
  56. package/dist/components/glass/GlassSection.svelte +9 -1
  57. package/dist/components/glass/LensFilters.svelte +4 -2
  58. package/dist/components/glass/Popover.svelte +379 -0
  59. package/dist/components/glass/Popover.svelte.d.ts +30 -0
  60. package/dist/components/glass/Sheet.svelte +470 -103
  61. package/dist/components/glass/Sheet.svelte.d.ts +55 -7
  62. package/dist/components/glass/index.d.ts +3 -0
  63. package/dist/components/glass/index.js +1 -0
  64. package/dist/components/index.d.ts +2 -0
  65. package/dist/components/index.js +4 -0
  66. package/dist/components/interactive/Badge.svelte +97 -6
  67. package/dist/components/interactive/Badge.svelte.d.ts +13 -2
  68. package/dist/components/interactive/Button.svelte +162 -20
  69. package/dist/components/interactive/Button.svelte.d.ts +14 -1
  70. package/dist/components/interactive/IconButton.svelte +214 -4
  71. package/dist/components/interactive/IconButton.svelte.d.ts +2 -1
  72. package/dist/components/interactive/ProgressView.svelte +306 -0
  73. package/dist/components/interactive/ProgressView.svelte.d.ts +25 -0
  74. package/dist/components/interactive/Segment.d.ts +14 -0
  75. package/dist/components/interactive/Segment.js +1 -0
  76. package/dist/components/interactive/SegmentedControl.svelte +399 -0
  77. package/dist/components/interactive/SegmentedControl.svelte.d.ts +17 -0
  78. package/dist/components/interactive/Slider.svelte +299 -0
  79. package/dist/components/interactive/Slider.svelte.d.ts +23 -0
  80. package/dist/components/interactive/TabView.svelte +573 -41
  81. package/dist/components/interactive/TabView.svelte.d.ts +25 -0
  82. package/dist/components/interactive/Toggle.svelte +287 -16
  83. package/dist/components/interactive/Toggle.svelte.d.ts +12 -0
  84. package/dist/components/interactive/index.d.ts +4 -0
  85. package/dist/components/interactive/index.js +3 -0
  86. package/dist/components/list/List.svelte +41 -17
  87. package/dist/components/list/List.svelte.d.ts +1 -0
  88. package/dist/components/list/ListRow.svelte +208 -47
  89. package/dist/components/list/ListRow.svelte.d.ts +16 -1
  90. package/dist/components/menu/Menu.svelte +51 -27
  91. package/dist/components/menu/Menu.svelte.d.ts +5 -0
  92. package/dist/components/menu/MenuItem.svelte +82 -25
  93. package/dist/components/menu/MenuItem.svelte.d.ts +8 -0
  94. package/dist/components/menu/MenuSection.svelte +95 -0
  95. package/dist/components/menu/MenuSection.svelte.d.ts +18 -0
  96. package/dist/components/menu/MenuSubmenu.svelte +196 -0
  97. package/dist/components/menu/MenuSubmenu.svelte.d.ts +11 -0
  98. package/dist/components/menu/index.d.ts +4 -1
  99. package/dist/components/menu/index.js +3 -1
  100. package/dist/components/navigation/NavigationBar.svelte +97 -15
  101. package/dist/components/navigation/NavigationBar.svelte.d.ts +6 -0
  102. package/dist/components/toolbar/Toolbar.svelte +43 -0
  103. package/dist/components/toolbar/Toolbar.svelte.d.ts +13 -0
  104. package/dist/components/toolbar/ToolbarItemGroup.svelte +45 -0
  105. package/dist/components/toolbar/ToolbarItemGroup.svelte.d.ts +14 -0
  106. package/dist/components/toolbar/ToolbarSpacer.svelte +36 -0
  107. package/dist/components/toolbar/ToolbarSpacer.svelte.d.ts +12 -0
  108. package/dist/components/toolbar/index.d.ts +7 -0
  109. package/dist/components/toolbar/index.js +7 -0
  110. package/dist/components/typography/Text.svelte +9 -1
  111. package/dist/constants/alertAction.d.ts +19 -0
  112. package/dist/constants/alertAction.js +1 -0
  113. package/dist/constants/badge.d.ts +32 -0
  114. package/dist/constants/badge.js +47 -0
  115. package/dist/constants/buttons.d.ts +45 -0
  116. package/dist/constants/buttons.js +52 -0
  117. package/dist/constants/colors.d.ts +9 -4
  118. package/dist/constants/colors.js +9 -4
  119. package/dist/constants/index.d.ts +3 -0
  120. package/dist/constants/index.js +3 -0
  121. package/dist/constants/progress.d.ts +56 -0
  122. package/dist/constants/progress.js +75 -0
  123. package/dist/constants/variants.d.ts +6 -2
  124. package/dist/constants/variants.js +2 -2
  125. package/dist/styles/glasslikeui.css +135 -1
  126. package/dist/styles/lens.css +21 -19
  127. package/dist/styles/squircle.css +17 -7
  128. package/package.json +1 -1
@@ -0,0 +1,182 @@
1
+ /**
2
+ * The colour maths behind adaptiveTone. Pure, so every decision is unit tested
3
+ * and mutated. Internal: only the GlassTone type is public.
4
+ *
5
+ * Glass is see-through, so the colour text sits on is the glass fill painted
6
+ * over whatever is behind it. adaptiveTone estimates what is behind from the
7
+ * page's own backgrounds, and this module turns that estimate into a tone: the
8
+ * backdrop is light (dark text) or dark (light text).
9
+ */
10
+ /**
11
+ * How a glass surface picks its text colour. `auto` follows the backdrop,
12
+ * `light` and `dark` force the tone for a backdrop of that kind, and `theme`
13
+ * keeps the theme's text colours.
14
+ */
15
+ export type GlassTone = 'auto' | 'light' | 'dark' | 'theme';
16
+ /** The tone of the backdrop behind a glass surface. */
17
+ export type BackdropTone = 'light' | 'dark';
18
+ /** An sRGB colour with channels 0 to 255. */
19
+ export type Rgb = readonly [number, number, number];
20
+ /** An sRGB colour with channels 0 to 255 and an alpha of 0 to 1. */
21
+ export interface Rgba {
22
+ readonly rgb: Rgb;
23
+ readonly alpha: number;
24
+ }
25
+ /** WCAG 2.2 AA for text. */
26
+ export declare const TEXT_CONTRAST = 4.5;
27
+ /** WCAG 2.2 AA for glyphs and other non-text parts. */
28
+ export declare const GLYPH_CONTRAST = 3;
29
+ /**
30
+ * When neither tone reaches TEXT_CONTRAST, the other tone must beat the current
31
+ * one by this factor before the surface switches, so a backdrop near the middle
32
+ * does not make it flicker.
33
+ */
34
+ export declare const SWITCH_MARGIN = 1.25;
35
+ /** The text colour of each tone. Black and white, the widest contrast there is. */
36
+ export declare const TONE_FOREGROUND: Readonly<Record<BackdropTone, Rgb>>;
37
+ /**
38
+ * How much of the primary colour secondary text keeps, as a percentage, from
39
+ * the dimmest. 60 matches iOS secondary labels. adaptiveTone picks the first
40
+ * that still holds TEXT_CONTRAST.
41
+ */
42
+ export declare const SECONDARY_STEPS: readonly [60, 80, 100];
43
+ export type SecondaryStep = (typeof SECONDARY_STEPS)[number];
44
+ /**
45
+ * The colours a hinted backdrop may be. A hint says light or dark, not how
46
+ * light: a dark photo has lighter patches, so a dark hint is anything from
47
+ * black to a dark grey, and a light hint anything from white to a light grey.
48
+ * The tone and its steps must hold over both ends.
49
+ */
50
+ export declare const HINT_COLOURS: Readonly<Record<BackdropTone, readonly Rgb[]>>;
51
+ /**
52
+ * Reads a computed CSS colour: `rgb()`, `rgba()`, `color(srgb ...)` or
53
+ * `transparent`, the forms browsers compute sRGB colours to. Anything else,
54
+ * such as `oklch()`, returns null, so the caller can treat it as unknown.
55
+ */
56
+ export declare function parseCssColor(value: string): Rgba | null;
57
+ /** WCAG relative luminance, 0 for black to 1 for white. */
58
+ export declare function relativeLuminance([r, g, b]: Rgb): number;
59
+ /** WCAG contrast ratio between two opaque colours, 1 to 21. */
60
+ export declare function contrastRatio(a: Rgb, b: Rgb): number;
61
+ /** Paints a colour with alpha over an opaque one, as the browser does, in sRGB. */
62
+ export declare function paintOver(top: Rgba, bottom: Rgb): Rgb;
63
+ /**
64
+ * Flattens a surface's own layers, listed bottom first (the element, then its
65
+ * ::before, then its ::after), into one fill.
66
+ */
67
+ export declare function flattenFill(layers: readonly Rgba[]): Rgba;
68
+ /**
69
+ * One layer of the page behind a point, front first:
70
+ * - a list of colours: a background colour, or a gradient as its stops. Any
71
+ * one stop may be the one behind the point.
72
+ * - `light` or `dark`: an author's `data-glass-backdrop`, taken as solid.
73
+ * - null: an image, video or canvas with no hint. Its pixels are not read.
74
+ */
75
+ export type BackdropLayer = readonly Rgba[] | BackdropTone | null;
76
+ /** Most backdrop candidates one point yields, so gradients cannot blow up the work. */
77
+ export declare const MAX_CANDIDATES = 16;
78
+ /**
79
+ * The opaque colours that may be behind one point, from its layers front
80
+ * first, over the canvas colour. A gradient gives one candidate per stop.
81
+ * Returns null when an unknown layer shows through, so the point is skipped.
82
+ */
83
+ export declare function backdropCandidates(layers: readonly BackdropLayer[], canvas: Rgb): Rgb[] | null;
84
+ /**
85
+ * The lowest contrast a tone's text reaches over the given surfaces, with the
86
+ * text at `percent` of its colour over each surface, as secondary text is.
87
+ * Infinity for no surfaces.
88
+ */
89
+ export declare function toneContrast(tone: BackdropTone, surfaces: readonly Rgb[], percent?: number): number;
90
+ /**
91
+ * Picks the tone for the surfaces the glass may show, given the tone it has
92
+ * now. With none, returns null so the theme colours stay. A tone is kept while
93
+ * its text holds TEXT_CONTRAST over every surface, which is the hysteresis: it
94
+ * switches only when the text would fail, and only to a tone that does better.
95
+ * Black or white always holds 4.5:1 over one opaque colour, so over a plain
96
+ * backdrop the result always passes.
97
+ */
98
+ export declare function chooseTone(surfaces: readonly Rgb[], current: BackdropTone | null): BackdropTone | null;
99
+ /**
100
+ * The dimmest secondary step whose text still holds TEXT_CONTRAST over every
101
+ * surface. Falls back to 100, the primary colour, when none does.
102
+ */
103
+ export declare function secondaryStep(tone: BackdropTone, surfaces: readonly Rgb[]): SecondaryStep;
104
+ /** Where on a surface adaptiveTone looks, as fractions of its width and height. */
105
+ export declare const SAMPLE_COLUMNS: readonly [0.15, 0.5, 0.85];
106
+ /** Surfaces taller than this are sampled on two rows. */
107
+ export declare const TWO_ROW_HEIGHT = 56;
108
+ export interface Box {
109
+ readonly left: number;
110
+ readonly top: number;
111
+ readonly width: number;
112
+ readonly height: number;
113
+ }
114
+ /**
115
+ * The points to sample on a surface, in viewport coordinates. Three across,
116
+ * on one row, or two for a tall surface. Points outside the viewport are
117
+ * dropped, because nothing can be hit there.
118
+ */
119
+ export declare function samplePoints(box: Box, viewportWidth: number, viewportHeight: number): Array<[number, number]>;
120
+ /** Reads a `data-glass-backdrop` value. Anything but light or dark is no hint. */
121
+ export declare function readBackdropHint(value: string | null): BackdropTone | null;
122
+ /** Reads a tone prop. An unknown value falls back to `auto`. */
123
+ export declare function resolveGlassTone(value: unknown): GlassTone;
124
+ /** Reads one CSS colour, or returns null. */
125
+ export type ColourReader = (value: string) => Rgba | null;
126
+ /**
127
+ * Reads the stops of a computed background-image made only of gradients.
128
+ * Returns null for an image, or for colours `read` cannot read, so the caller
129
+ * treats the layer as unknown. `none` gives an empty list.
130
+ */
131
+ export declare function gradientColours(backgroundImage: string, read?: ColourReader): Rgba[] | null;
132
+ /** A stop of a linear gradient, at a length in px or a fraction of the line. */
133
+ export interface GradientStop {
134
+ readonly colour: Rgba;
135
+ readonly at: {
136
+ readonly px: number;
137
+ } | {
138
+ readonly fraction: number;
139
+ } | null;
140
+ }
141
+ /** One linear gradient, as its direction and its stops. */
142
+ export interface LinearGradient {
143
+ /** CSS angle in degrees, 0 toward the top and 90 toward the right, or a side or corner. */
144
+ readonly direction: number | {
145
+ readonly x: number;
146
+ readonly y: number;
147
+ };
148
+ readonly stops: readonly GradientStop[];
149
+ readonly repeating: boolean;
150
+ }
151
+ /**
152
+ * Reads a computed background-image that is one linear gradient. Returns
153
+ * null for anything else, such as several layers, a radial gradient or a
154
+ * position given with calc().
155
+ */
156
+ export declare function parseLinearGradient(backgroundImage: string, read?: ColourReader): LinearGradient | null;
157
+ /**
158
+ * The colour of a linear gradient at a point, with the gradient painted over
159
+ * `box`. Colours mix premultiplied, as browsers paint them.
160
+ */
161
+ export declare function linearGradientAt(gradient: LinearGradient, box: Box, x: number, y: number): Rgba;
162
+ /**
163
+ * The CSS saturate() filter on sRGB values, with each channel clipped, using
164
+ * the matrix in the Filter Effects spec. Chromium applies the glass
165
+ * backdrop-filter to the fill and the page under it together. Saturating clips
166
+ * channels, which changes how light a colour is: orange gets lighter and green
167
+ * darker. Firefox and WebKit show the page without it.
168
+ */
169
+ export declare function saturate(rgb: Rgb, amount: number): Rgb;
170
+ /** The saturation a computed backdrop-filter applies, 1 for none. Several multiply. */
171
+ export declare function readSaturation(backdropFilter: string): number;
172
+ /**
173
+ * How much of the accent colour accent text on glass keeps, as a percentage,
174
+ * from the most. The rest is the tone colour. adaptiveTone picks the first
175
+ * that holds TEXT_CONTRAST.
176
+ */
177
+ export declare const ACCENT_STEPS: readonly [100, 75, 50, 25, 0];
178
+ export type AccentStep = (typeof ACCENT_STEPS)[number];
179
+ /** Accent mixed with the tone colour, as `color-mix(in srgb, accent p%, fg)`. */
180
+ export declare function accentMix(tone: BackdropTone, accent: Rgb, percent: number): Rgb;
181
+ /** The most accent the text can keep and still hold TEXT_CONTRAST over every surface. */
182
+ export declare function accentStep(tone: BackdropTone, accent: Rgb, surfaces: readonly Rgb[]): AccentStep;
@@ -0,0 +1,513 @@
1
+ /**
2
+ * The colour maths behind adaptiveTone. Pure, so every decision is unit tested
3
+ * and mutated. Internal: only the GlassTone type is public.
4
+ *
5
+ * Glass is see-through, so the colour text sits on is the glass fill painted
6
+ * over whatever is behind it. adaptiveTone estimates what is behind from the
7
+ * page's own backgrounds, and this module turns that estimate into a tone: the
8
+ * backdrop is light (dark text) or dark (light text).
9
+ */
10
+ var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
11
+ if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
12
+ return cooked;
13
+ };
14
+ /** WCAG 2.2 AA for text. */
15
+ export var TEXT_CONTRAST = 4.5;
16
+ /** WCAG 2.2 AA for glyphs and other non-text parts. */
17
+ export var GLYPH_CONTRAST = 3;
18
+ /**
19
+ * When neither tone reaches TEXT_CONTRAST, the other tone must beat the current
20
+ * one by this factor before the surface switches, so a backdrop near the middle
21
+ * does not make it flicker.
22
+ */
23
+ export var SWITCH_MARGIN = 1.25;
24
+ /** The text colour of each tone. Black and white, the widest contrast there is. */
25
+ export var TONE_FOREGROUND = {
26
+ light: [0, 0, 0],
27
+ dark: [255, 255, 255]
28
+ };
29
+ /**
30
+ * How much of the primary colour secondary text keeps, as a percentage, from
31
+ * the dimmest. 60 matches iOS secondary labels. adaptiveTone picks the first
32
+ * that still holds TEXT_CONTRAST.
33
+ */
34
+ export var SECONDARY_STEPS = [60, 80, 100];
35
+ /**
36
+ * The colours a hinted backdrop may be. A hint says light or dark, not how
37
+ * light: a dark photo has lighter patches, so a dark hint is anything from
38
+ * black to a dark grey, and a light hint anything from white to a light grey.
39
+ * The tone and its steps must hold over both ends.
40
+ */
41
+ export var HINT_COLOURS = {
42
+ light: [
43
+ [255, 255, 255],
44
+ [170, 170, 170]
45
+ ],
46
+ dark: [
47
+ [0, 0, 0],
48
+ [68, 68, 68]
49
+ ]
50
+ };
51
+ var TRANSPARENT = { rgb: [0, 0, 0], alpha: 0 };
52
+ var clamp = function (value, low, high) { return Math.min(high, Math.max(low, value)); };
53
+ function channel(text, scale) {
54
+ var value = text.endsWith('%') ? (Number(text.slice(0, -1)) / 100) * scale : Number(text);
55
+ return Number.isFinite(value) ? value : null;
56
+ }
57
+ /**
58
+ * Reads a computed CSS colour: `rgb()`, `rgba()`, `color(srgb ...)` or
59
+ * `transparent`, the forms browsers compute sRGB colours to. Anything else,
60
+ * such as `oklch()`, returns null, so the caller can treat it as unknown.
61
+ */
62
+ export function parseCssColor(value) {
63
+ var _a;
64
+ var text = value.trim().toLowerCase();
65
+ if (text === 'transparent')
66
+ return TRANSPARENT;
67
+ var rgb = /^rgba?\(([^)]*)\)$/.exec(text);
68
+ var srgb = /^color\(srgb ([^)]*)\)$/.exec(text);
69
+ var body = (_a = rgb === null || rgb === void 0 ? void 0 : rgb[1]) !== null && _a !== void 0 ? _a : srgb === null || srgb === void 0 ? void 0 : srgb[1];
70
+ if (body === undefined)
71
+ return null;
72
+ var parts = body.trim().split(/[\s,/]+/);
73
+ if (parts.length !== 3 && parts.length !== 4)
74
+ return null;
75
+ var scale = rgb ? 255 : 1;
76
+ // Colour channels are 0 to 255 in rgb() and 0 to 1 in color(); alpha is 0 to 1.
77
+ var values = parts.map(function (part, i) { return channel(part, i < 3 ? scale : 1); });
78
+ if (values.some(function (v) { return v === null; }))
79
+ return null;
80
+ var _b = values, r = _b[0], g = _b[1], b = _b[2], _c = _b[3], alpha = _c === void 0 ? 1 : _c;
81
+ var toByte = function (c) { return clamp((c * 255) / scale, 0, 255); };
82
+ return { rgb: [toByte(r), toByte(g), toByte(b)], alpha: clamp(alpha, 0, 1) };
83
+ }
84
+ var linear = function (c) {
85
+ var s = c / 255;
86
+ return s <= 0.04045 ? s / 12.92 : Math.pow(((s + 0.055) / 1.055), 2.4);
87
+ };
88
+ /** WCAG relative luminance, 0 for black to 1 for white. */
89
+ export function relativeLuminance(_a) {
90
+ var r = _a[0], g = _a[1], b = _a[2];
91
+ return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
92
+ }
93
+ /** WCAG contrast ratio between two opaque colours, 1 to 21. */
94
+ export function contrastRatio(a, b) {
95
+ var la = relativeLuminance(a);
96
+ var lb = relativeLuminance(b);
97
+ return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
98
+ }
99
+ /** Paints a colour with alpha over an opaque one, as the browser does, in sRGB. */
100
+ export function paintOver(top, bottom) {
101
+ var a = top.alpha;
102
+ return [
103
+ a * top.rgb[0] + (1 - a) * bottom[0],
104
+ a * top.rgb[1] + (1 - a) * bottom[1],
105
+ a * top.rgb[2] + (1 - a) * bottom[2]
106
+ ];
107
+ }
108
+ /**
109
+ * Flattens a surface's own layers, listed bottom first (the element, then its
110
+ * ::before, then its ::after), into one fill.
111
+ */
112
+ export function flattenFill(layers) {
113
+ var fill = TRANSPARENT;
114
+ var _loop_1 = function (top) {
115
+ var alpha = top.alpha + fill.alpha * (1 - top.alpha);
116
+ if (alpha === 0)
117
+ return "continue";
118
+ var mix = function (i) {
119
+ return (top.rgb[i] * top.alpha + fill.rgb[i] * fill.alpha * (1 - top.alpha)) / alpha;
120
+ };
121
+ fill = { rgb: [mix(0), mix(1), mix(2)], alpha: alpha };
122
+ };
123
+ for (var _i = 0, layers_1 = layers; _i < layers_1.length; _i++) {
124
+ var top = layers_1[_i];
125
+ _loop_1(top);
126
+ }
127
+ return fill;
128
+ }
129
+ /** Most backdrop candidates one point yields, so gradients cannot blow up the work. */
130
+ export var MAX_CANDIDATES = 16;
131
+ function under(partial, colour) {
132
+ var weight = (1 - partial.cover) * colour.alpha;
133
+ return {
134
+ sum: [
135
+ partial.sum[0] + weight * colour.rgb[0],
136
+ partial.sum[1] + weight * colour.rgb[1],
137
+ partial.sum[2] + weight * colour.rgb[2]
138
+ ],
139
+ cover: partial.cover + weight
140
+ };
141
+ }
142
+ var OPAQUE = 0.999;
143
+ /**
144
+ * The opaque colours that may be behind one point, from its layers front
145
+ * first, over the canvas colour. A gradient gives one candidate per stop.
146
+ * Returns null when an unknown layer shows through, so the point is skipped.
147
+ */
148
+ export function backdropCandidates(layers, canvas) {
149
+ var partials = [{ sum: [0, 0, 0], cover: 0 }];
150
+ for (var _i = 0, layers_2 = layers; _i < layers_2.length; _i++) {
151
+ var layer = layers_2[_i];
152
+ if (layer === null)
153
+ return null;
154
+ var colours = typeof layer === 'string' ? HINT_COLOURS[layer].map(function (rgb) { return ({ rgb: rgb, alpha: 1 }); }) : layer;
155
+ var next = [];
156
+ for (var _a = 0, partials_1 = partials; _a < partials_1.length; _a++) {
157
+ var partial = partials_1[_a];
158
+ for (var _b = 0, colours_1 = colours; _b < colours_1.length; _b++) {
159
+ var colour = colours_1[_b];
160
+ next.push(under(partial, colour));
161
+ }
162
+ }
163
+ partials = next.slice(0, MAX_CANDIDATES);
164
+ if (partials.every(function (p) { return p.cover >= OPAQUE; }))
165
+ break;
166
+ }
167
+ return partials.map(function (p) { return [
168
+ p.sum[0] + (1 - p.cover) * canvas[0],
169
+ p.sum[1] + (1 - p.cover) * canvas[1],
170
+ p.sum[2] + (1 - p.cover) * canvas[2]
171
+ ]; });
172
+ }
173
+ /**
174
+ * The lowest contrast a tone's text reaches over the given surfaces, with the
175
+ * text at `percent` of its colour over each surface, as secondary text is.
176
+ * Infinity for no surfaces.
177
+ */
178
+ export function toneContrast(tone, surfaces, percent) {
179
+ if (percent === void 0) { percent = 100; }
180
+ var text = { rgb: TONE_FOREGROUND[tone], alpha: percent / 100 };
181
+ var lowest = Infinity;
182
+ for (var _i = 0, surfaces_1 = surfaces; _i < surfaces_1.length; _i++) {
183
+ var surface = surfaces_1[_i];
184
+ lowest = Math.min(lowest, contrastRatio(paintOver(text, surface), surface));
185
+ }
186
+ return lowest;
187
+ }
188
+ /**
189
+ * Picks the tone for the surfaces the glass may show, given the tone it has
190
+ * now. With none, returns null so the theme colours stay. A tone is kept while
191
+ * its text holds TEXT_CONTRAST over every surface, which is the hysteresis: it
192
+ * switches only when the text would fail, and only to a tone that does better.
193
+ * Black or white always holds 4.5:1 over one opaque colour, so over a plain
194
+ * backdrop the result always passes.
195
+ */
196
+ export function chooseTone(surfaces, current) {
197
+ if (surfaces.length === 0)
198
+ return null;
199
+ var light = toneContrast('light', surfaces);
200
+ var dark = toneContrast('dark', surfaces);
201
+ var best = light >= dark ? 'light' : 'dark';
202
+ if (current === null)
203
+ return best;
204
+ var kept = current === 'light' ? light : dark;
205
+ var other = current === 'light' ? dark : light;
206
+ if (kept >= TEXT_CONTRAST)
207
+ return current;
208
+ if (other >= TEXT_CONTRAST || other > kept * SWITCH_MARGIN)
209
+ return best;
210
+ return current;
211
+ }
212
+ /**
213
+ * The dimmest secondary step whose text still holds TEXT_CONTRAST over every
214
+ * surface. Falls back to 100, the primary colour, when none does.
215
+ */
216
+ export function secondaryStep(tone, surfaces) {
217
+ for (var _i = 0, SECONDARY_STEPS_1 = SECONDARY_STEPS; _i < SECONDARY_STEPS_1.length; _i++) {
218
+ var step = SECONDARY_STEPS_1[_i];
219
+ if (toneContrast(tone, surfaces, step) >= TEXT_CONTRAST)
220
+ return step;
221
+ }
222
+ return 100;
223
+ }
224
+ /** Where on a surface adaptiveTone looks, as fractions of its width and height. */
225
+ export var SAMPLE_COLUMNS = [0.15, 0.5, 0.85];
226
+ /** Surfaces taller than this are sampled on two rows. */
227
+ export var TWO_ROW_HEIGHT = 56;
228
+ /**
229
+ * The points to sample on a surface, in viewport coordinates. Three across,
230
+ * on one row, or two for a tall surface. Points outside the viewport are
231
+ * dropped, because nothing can be hit there.
232
+ */
233
+ export function samplePoints(box, viewportWidth, viewportHeight) {
234
+ if (box.width <= 0 || box.height <= 0)
235
+ return [];
236
+ var rows = box.height > TWO_ROW_HEIGHT ? [0.25, 0.75] : [0.5];
237
+ var points = [];
238
+ for (var _i = 0, rows_1 = rows; _i < rows_1.length; _i++) {
239
+ var row = rows_1[_i];
240
+ for (var _a = 0, SAMPLE_COLUMNS_1 = SAMPLE_COLUMNS; _a < SAMPLE_COLUMNS_1.length; _a++) {
241
+ var column = SAMPLE_COLUMNS_1[_a];
242
+ var x = box.left + box.width * column;
243
+ var y = box.top + box.height * row;
244
+ if (x >= 0 && y >= 0 && x < viewportWidth && y < viewportHeight)
245
+ points.push([x, y]);
246
+ }
247
+ }
248
+ return points;
249
+ }
250
+ /** Reads a `data-glass-backdrop` value. Anything but light or dark is no hint. */
251
+ export function readBackdropHint(value) {
252
+ return value === 'light' || value === 'dark' ? value : null;
253
+ }
254
+ /** Reads a tone prop. An unknown value falls back to `auto`. */
255
+ export function resolveGlassTone(value) {
256
+ return value === 'light' || value === 'dark' || value === 'theme' ? value : 'auto';
257
+ }
258
+ /** Image sources inside a background-image that adaptiveTone cannot read. */
259
+ var UNREADABLE_IMAGE = /url\(|image-set\(|element\(|cross-fade\(|paint\(|-webkit-/;
260
+ /** A colour inside a computed gradient: any colour function, or a keyword. */
261
+ var COLOUR = String.raw(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)([^)]*)|\b(?:transparent|currentcolor)\b"], ["\\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\\([^)]*\\)|\\b(?:transparent|currentcolor)\\b"])));
262
+ var GRADIENT_COLOUR = new RegExp(COLOUR, 'g');
263
+ /** A colour at the start of a gradient stop. */
264
+ var STOP_COLOUR = new RegExp("^(?:".concat(COLOUR, ")"));
265
+ /**
266
+ * Reads the stops of a computed background-image made only of gradients.
267
+ * Returns null for an image, or for colours `read` cannot read, so the caller
268
+ * treats the layer as unknown. `none` gives an empty list.
269
+ */
270
+ export function gradientColours(backgroundImage, read) {
271
+ if (read === void 0) { read = parseCssColor; }
272
+ var text = backgroundImage.trim().toLowerCase();
273
+ if (text === '' || text === 'none')
274
+ return [];
275
+ if (UNREADABLE_IMAGE.test(text))
276
+ return null;
277
+ var found = text.match(GRADIENT_COLOUR);
278
+ if (!found)
279
+ return null;
280
+ var colours = found.map(read);
281
+ if (colours.some(function (c) { return c === null; }))
282
+ return null;
283
+ return colours;
284
+ }
285
+ /** Splits at commas outside brackets. */
286
+ function topLevel(text) {
287
+ var parts = [];
288
+ var depth = 0;
289
+ var start = 0;
290
+ for (var i = 0; i < text.length; i++) {
291
+ var c = text[i];
292
+ if (c === '(')
293
+ depth++;
294
+ else if (c === ')')
295
+ depth--;
296
+ else if (c === ',' && depth === 0) {
297
+ parts.push(text.slice(start, i).trim());
298
+ start = i + 1;
299
+ }
300
+ }
301
+ parts.push(text.slice(start).trim());
302
+ return parts;
303
+ }
304
+ var SIDES = { left: [-1, 0], right: [1, 0], top: [0, 1], bottom: [0, -1] };
305
+ function readAngle(text) {
306
+ var m = /^(-?[\d.]+)(deg|turn|rad|grad)$/.exec(text);
307
+ if (!m)
308
+ return null;
309
+ var value = Number(m[1]);
310
+ var unit = m[2];
311
+ if (unit === 'turn')
312
+ return value * 360;
313
+ if (unit === 'rad')
314
+ return (value * 180) / Math.PI;
315
+ if (unit === 'grad')
316
+ return value * 0.9;
317
+ return value;
318
+ }
319
+ function readPosition(text) {
320
+ var m = /^(-?[\d.]+)(px|%)$/.exec(text);
321
+ if (!m)
322
+ return undefined;
323
+ return m[2] === '%' ? { fraction: Number(m[1]) / 100 } : { px: Number(m[1]) };
324
+ }
325
+ /**
326
+ * Reads a computed background-image that is one linear gradient. Returns
327
+ * null for anything else, such as several layers, a radial gradient or a
328
+ * position given with calc().
329
+ */
330
+ export function parseLinearGradient(backgroundImage, read) {
331
+ if (read === void 0) { read = parseCssColor; }
332
+ var text = backgroundImage.trim().toLowerCase();
333
+ var m = /^(repeating-)?linear-gradient\((.*)\)$/.exec(text);
334
+ if (!m || topLevel(text).length !== 1)
335
+ return null;
336
+ var args = topLevel(m[2]);
337
+ var direction = 180;
338
+ var first = args[0];
339
+ var angle = readAngle(first);
340
+ if (angle !== null) {
341
+ direction = angle;
342
+ args.shift();
343
+ }
344
+ else if (first.startsWith('to ')) {
345
+ var words = first.slice(3).trim().split(/\s+/);
346
+ var x = 0;
347
+ var y = 0;
348
+ for (var _i = 0, words_1 = words; _i < words_1.length; _i++) {
349
+ var word = words_1[_i];
350
+ var side = SIDES[word];
351
+ if (!side)
352
+ return null;
353
+ x += side[0];
354
+ y += side[1];
355
+ }
356
+ direction = { x: x, y: y };
357
+ args.shift();
358
+ }
359
+ var stops = [];
360
+ for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
361
+ var arg = args_1[_a];
362
+ var found = STOP_COLOUR.exec(arg);
363
+ if (!found)
364
+ return null;
365
+ var colourText = found[0];
366
+ var colour = read(colourText);
367
+ if (!colour)
368
+ return null;
369
+ var rest = arg.slice(colourText.length).trim();
370
+ var positions = rest === '' ? [] : rest.split(/\s+/);
371
+ if (positions.length > 2)
372
+ return null;
373
+ if (positions.length === 0) {
374
+ stops.push({ colour: colour, at: null });
375
+ continue;
376
+ }
377
+ for (var _b = 0, positions_1 = positions; _b < positions_1.length; _b++) {
378
+ var position = positions_1[_b];
379
+ var at = readPosition(position);
380
+ if (at === undefined)
381
+ return null;
382
+ stops.push({ colour: colour, at: at });
383
+ }
384
+ }
385
+ if (stops.length < 2)
386
+ return null;
387
+ return { direction: direction, stops: stops, repeating: !!m[1] };
388
+ }
389
+ /** The gradient line's angle in degrees for a box, resolving a side or corner. */
390
+ function angleFor(direction, width, height) {
391
+ if (typeof direction === 'number')
392
+ return direction;
393
+ // A corner's line is at right angles to the diagonal between the other two corners.
394
+ return (Math.atan2(direction.x * height, direction.y * width) * 180) / Math.PI;
395
+ }
396
+ /** Stop positions as fractions of the gradient line, filled in as CSS does. */
397
+ function stopFractions(stops, length) {
398
+ var raw = stops.map(function (s) { return (s.at === null ? null : 'px' in s.at ? s.at.px / length : s.at.fraction); });
399
+ if (raw[0] === null)
400
+ raw[0] = 0;
401
+ if (raw[raw.length - 1] === null)
402
+ raw[raw.length - 1] = 1;
403
+ // A stop before an earlier one moves up to it.
404
+ var highest = -Infinity;
405
+ for (var i = 0; i < raw.length; i++) {
406
+ if (raw[i] !== null) {
407
+ highest = Math.max(highest, raw[i]);
408
+ raw[i] = highest;
409
+ }
410
+ }
411
+ // Unpositioned stops spread evenly between their positioned neighbours.
412
+ for (var i = 0; i < raw.length; i++) {
413
+ var j = i;
414
+ while (raw[j] === null)
415
+ j++;
416
+ var from = raw[i - 1];
417
+ var to = raw[j];
418
+ for (var k = i; k < j; k++)
419
+ raw[k] = from + ((to - from) * (k - i + 1)) / (j - i + 1);
420
+ }
421
+ return raw;
422
+ }
423
+ function mixPremultiplied(a, b, t) {
424
+ var alpha = a.alpha + (b.alpha - a.alpha) * t;
425
+ if (alpha === 0)
426
+ return TRANSPARENT;
427
+ var channel = function (i) {
428
+ return (a.rgb[i] * a.alpha + (b.rgb[i] * b.alpha - a.rgb[i] * a.alpha) * t) / alpha;
429
+ };
430
+ return { rgb: [channel(0), channel(1), channel(2)], alpha: alpha };
431
+ }
432
+ /**
433
+ * The colour of a linear gradient at a point, with the gradient painted over
434
+ * `box`. Colours mix premultiplied, as browsers paint them.
435
+ */
436
+ export function linearGradientAt(gradient, box, x, y) {
437
+ var radians = (angleFor(gradient.direction, box.width, box.height) * Math.PI) / 180;
438
+ var sin = Math.sin(radians);
439
+ var cos = Math.cos(radians);
440
+ var length = Math.abs(box.width * sin) + Math.abs(box.height * cos);
441
+ var dx = x - (box.left + box.width / 2);
442
+ var dy = y - (box.top + box.height / 2);
443
+ var t = length === 0 ? 0 : (dx * sin - dy * cos) / length + 0.5;
444
+ var at = stopFractions(gradient.stops, length);
445
+ var colours = gradient.stops.map(function (s) { return s.colour; });
446
+ var start = at[0];
447
+ var period = at[at.length - 1] - start;
448
+ if (gradient.repeating && period > 0)
449
+ t = start + ((((t - start) % period) + period) % period);
450
+ if (t <= at[0])
451
+ return colours[0];
452
+ for (var i = 1; i < at.length; i++) {
453
+ if (t <= at[i]) {
454
+ // t is past the previous stop, so the span is never zero here.
455
+ return mixPremultiplied(colours[i - 1], colours[i], (t - at[i - 1]) / (at[i] - at[i - 1]));
456
+ }
457
+ }
458
+ return colours[colours.length - 1];
459
+ }
460
+ /**
461
+ * The CSS saturate() filter on sRGB values, with each channel clipped, using
462
+ * the matrix in the Filter Effects spec. Chromium applies the glass
463
+ * backdrop-filter to the fill and the page under it together. Saturating clips
464
+ * channels, which changes how light a colour is: orange gets lighter and green
465
+ * darker. Firefox and WebKit show the page without it.
466
+ */
467
+ export function saturate(rgb, amount) {
468
+ var r = rgb[0], g = rgb[1], b = rgb[2];
469
+ var s = amount;
470
+ var clip = function (v) { return Math.min(255, Math.max(0, v)); };
471
+ return [
472
+ clip((0.213 + 0.787 * s) * r + (0.715 - 0.715 * s) * g + (0.072 - 0.072 * s) * b),
473
+ clip((0.213 - 0.213 * s) * r + (0.715 + 0.285 * s) * g + (0.072 - 0.072 * s) * b),
474
+ clip((0.213 - 0.213 * s) * r + (0.715 - 0.715 * s) * g + (0.072 + 0.928 * s) * b)
475
+ ];
476
+ }
477
+ /** The saturation a computed backdrop-filter applies, 1 for none. Several multiply. */
478
+ export function readSaturation(backdropFilter) {
479
+ var amount = 1;
480
+ for (var _i = 0, _a = backdropFilter.matchAll(/saturate\(\s*([\d.]+)(%?)\s*\)/g); _i < _a.length; _i++) {
481
+ var m = _a[_i];
482
+ amount *= Number(m[1]) / (m[2] ? 100 : 1);
483
+ }
484
+ return amount;
485
+ }
486
+ /**
487
+ * How much of the accent colour accent text on glass keeps, as a percentage,
488
+ * from the most. The rest is the tone colour. adaptiveTone picks the first
489
+ * that holds TEXT_CONTRAST.
490
+ */
491
+ export var ACCENT_STEPS = [100, 75, 50, 25, 0];
492
+ /** Accent mixed with the tone colour, as `color-mix(in srgb, accent p%, fg)`. */
493
+ export function accentMix(tone, accent, percent) {
494
+ var fg = TONE_FOREGROUND[tone];
495
+ var share = percent / 100;
496
+ return [0, 1, 2].map(function (i) { return share * accent[i] + (1 - share) * fg[i]; });
497
+ }
498
+ /** The most accent the text can keep and still hold TEXT_CONTRAST over every surface. */
499
+ export function accentStep(tone, accent, surfaces) {
500
+ var _loop_2 = function (step) {
501
+ var colour = accentMix(tone, accent, step);
502
+ if (surfaces.every(function (s) { return contrastRatio(colour, s) >= TEXT_CONTRAST; }))
503
+ return { value: step };
504
+ };
505
+ for (var _i = 0, ACCENT_STEPS_1 = ACCENT_STEPS; _i < ACCENT_STEPS_1.length; _i++) {
506
+ var step = ACCENT_STEPS_1[_i];
507
+ var state_1 = _loop_2(step);
508
+ if (typeof state_1 === "object")
509
+ return state_1.value;
510
+ }
511
+ return 0;
512
+ }
513
+ var templateObject_1;