pi-pocket 0.26.0 → 0.27.0-beta.2
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/CONTRIBUTING.md +1 -1
- package/docs/design/design-system.md +165 -0
- package/docs/design/ds-notes-b.md +61 -0
- package/docs/design/ds-notes-g.md +170 -0
- package/docs/design/ds-notes-t.md +303 -0
- package/docs/design/ds-notes-u.md +220 -0
- package/docs/design/ds-notes-v.md +186 -0
- package/docs/design/ds-rounds.md +160 -0
- package/docs/design/ds-u-bridge.patch +732 -0
- package/docs/examples/design-system/README.md +15 -0
- package/docs/examples/design-system/pocket/.pi/design/components/badge.html +112 -0
- package/docs/examples/design-system/pocket/.pi/design/components/button.html +203 -0
- package/docs/examples/design-system/pocket/.pi/design/components/card.html +125 -0
- package/docs/examples/design-system/pocket/.pi/design/components/code-block.html +155 -0
- package/docs/examples/design-system/pocket/.pi/design/components/input.html +156 -0
- package/docs/examples/design-system/pocket/.pi/design/components/menu-row.html +167 -0
- package/docs/examples/design-system/pocket/.pi/design/components/notice.html +171 -0
- package/docs/examples/design-system/pocket/.pi/design/components/segmented.html +120 -0
- package/docs/examples/design-system/pocket/.pi/design/components/switch.html +117 -0
- package/docs/examples/design-system/pocket/.pi/design/components/tool-step.html +163 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/components.css +730 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/names.md +495 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tailwind.css +1132 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.css +1126 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.dark.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.high-contrast.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.light.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.resolved.json +371 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.studio.json +5915 -0
- package/docs/examples/design-system/pocket/.pi/design/guidelines.md +81 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.dark.json +182 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.high-contrast.json +185 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.json +2034 -0
- package/docs/features.md +6 -2
- package/docs/index.md +1 -0
- package/docs/map.md +34 -25
- package/docs/release-notes.md +1 -1
- package/docs/releases.md +45 -0
- package/package.json +2 -1
- package/src/server/design/check-cli.ts +283 -0
- package/src/server/design/color.ts +654 -0
- package/src/server/design/components.ts +328 -0
- package/src/server/design/docsite-assets.ts +29 -0
- package/src/server/design/docsite-color-section.ts +366 -0
- package/src/server/design/docsite-color.ts +303 -0
- package/src/server/design/docsite-components.ts +89 -0
- package/src/server/design/docsite-core.ts +634 -0
- package/src/server/design/docsite-css-base.ts +397 -0
- package/src/server/design/docsite-css-content.ts +477 -0
- package/src/server/design/docsite-css-phone.ts +251 -0
- package/src/server/design/docsite-css-reference.ts +374 -0
- package/src/server/design/docsite-css-specimens.ts +465 -0
- package/src/server/design/docsite-js-code.ts +96 -0
- package/src/server/design/docsite-js-core.ts +72 -0
- package/src/server/design/docsite-js-demos.ts +24 -0
- package/src/server/design/docsite-js-links.ts +25 -0
- package/src/server/design/docsite-js-nav.ts +73 -0
- package/src/server/design/docsite-js-stages.ts +81 -0
- package/src/server/design/docsite-js-table.ts +81 -0
- package/src/server/design/docsite-markdown.ts +221 -0
- package/src/server/design/docsite-motion.ts +110 -0
- package/src/server/design/docsite-reference.ts +269 -0
- package/src/server/design/docsite-sanitize.ts +193 -0
- package/src/server/design/docsite-shadows.ts +45 -0
- package/src/server/design/docsite-space.ts +289 -0
- package/src/server/design/docsite-type.ts +167 -0
- package/src/server/design/docsite.ts +508 -0
- package/src/server/design/export.ts +86 -9
- package/src/server/design/fix.ts +666 -0
- package/src/server/design/guide.ts +137 -22
- package/src/server/design/index.ts +368 -15
- package/src/server/design/lint-color.ts +293 -0
- package/src/server/design/lint-page-checks.ts +204 -0
- package/src/server/design/lint-page-elements.ts +252 -0
- package/src/server/design/lint-page-focus.ts +165 -0
- package/src/server/design/lint-page-literals.ts +293 -0
- package/src/server/design/lint-page-setup.ts +102 -0
- package/src/server/design/lint-page-states.ts +121 -0
- package/src/server/design/lint-page-values.ts +329 -0
- package/src/server/design/lint-page.ts +101 -0
- package/src/server/design/lint-suggest.ts +340 -0
- package/src/server/design/lint.ts +1136 -0
- package/src/server/design/render.ts +369 -18
- package/src/server/design/scan-braces.ts +115 -0
- package/src/server/design/scan-categorize.ts +187 -0
- package/src/server/design/scan-channels.ts +113 -0
- package/src/server/design/scan-color-base.ts +209 -0
- package/src/server/design/scan-color-names.ts +403 -0
- package/src/server/design/scan-color-palette.ts +91 -0
- package/src/server/design/scan-color-roles.ts +565 -0
- package/src/server/design/scan-colors.ts +22 -0
- package/src/server/design/scan-facts-antd.ts +171 -0
- package/src/server/design/scan-facts-files.ts +300 -0
- package/src/server/design/scan-facts-lists.ts +538 -0
- package/src/server/design/scan-facts-sass.ts +113 -0
- package/src/server/design/scan-facts-theme.ts +400 -0
- package/src/server/design/scan-facts-use.ts +674 -0
- package/src/server/design/scan-facts-wordpress.ts +374 -0
- package/src/server/design/scan-facts.ts +285 -0
- package/src/server/design/scan-files.ts +384 -0
- package/src/server/design/scan-import-convert.ts +277 -0
- package/src/server/design/scan-import-eval.ts +583 -0
- package/src/server/design/scan-import-material.ts +745 -0
- package/src/server/design/scan-import-sd-config.ts +370 -0
- package/src/server/design/scan-import-sd.ts +381 -0
- package/src/server/design/scan-import-studio.ts +242 -0
- package/src/server/design/scan-import.ts +336 -0
- package/src/server/design/scan-infer.ts +313 -0
- package/src/server/design/scan-js-eval.ts +1267 -0
- package/src/server/design/scan-palette.ts +485 -0
- package/src/server/design/scan-parse.ts +1134 -0
- package/src/server/design/scan-prepare.ts +402 -0
- package/src/server/design/scan-render-pages.ts +367 -0
- package/src/server/design/scan-render-probe.ts +229 -0
- package/src/server/design/scan-render.ts +524 -0
- package/src/server/design/scan-review.ts +54 -0
- package/src/server/design/scan-roles.ts +259 -0
- package/src/server/design/scan-sass-colors.ts +275 -0
- package/src/server/design/scan-sass-eval.ts +269 -0
- package/src/server/design/scan-sass-expr.ts +911 -0
- package/src/server/design/scan-sass-modules.ts +353 -0
- package/src/server/design/scan-sass-run.ts +1003 -0
- package/src/server/design/scan-sass-stmt.ts +256 -0
- package/src/server/design/scan-sass-values.ts +886 -0
- package/src/server/design/scan-scales.ts +983 -0
- package/src/server/design/scan-tailwind.ts +34 -0
- package/src/server/design/scan-theme-shapes.ts +579 -0
- package/src/server/design/scan-tree.ts +290 -0
- package/src/server/design/scan-util.ts +44 -0
- package/src/server/design/scan-variables.ts +470 -0
- package/src/server/design/scan.ts +703 -0
- package/src/server/design/system-schema.ts +68 -0
- package/src/server/design/system-types.ts +279 -0
- package/src/server/design/system.ts +510 -82
- package/src/server/design/tokens-base.ts +123 -0
- package/src/server/design/tokens-compile.ts +440 -0
- package/src/server/design/tokens-dtcg.ts +354 -0
- package/src/server/design/tokens-io.ts +417 -0
- package/src/server/design/tokens-model.ts +1163 -0
- package/src/server/design/tokens-names.ts +112 -0
- package/src/server/design/tokens-ramps.ts +112 -0
- package/src/server/design/tokens-summary.ts +317 -0
- package/src/server/design/tokens-tailwind.ts +324 -0
- package/src/server/design/tokens-values.ts +921 -0
- package/src/server/design/tokens.ts +533 -0
- package/src/server/docs.ts +8 -0
- package/src/server/extensions/artifacts.ts +87 -2
- package/src/server/extensions/design/app.js +280 -0
- package/src/server/extensions/design/canvas.js +142 -17
- package/src/server/extensions/design/check.js +656 -0
- package/src/server/extensions/design/color.js +383 -0
- package/src/server/extensions/design/data.js +102 -7
- package/src/server/extensions/design/devices.js +26 -0
- package/src/server/extensions/design/docks.js +117 -3
- package/src/server/extensions/design/index.js +171 -219
- package/src/server/extensions/design/messages.js +49 -0
- package/src/server/extensions/design/parts.js +15 -1
- package/src/server/extensions/design/paths.js +4 -0
- package/src/server/extensions/design/picker.js +123 -0
- package/src/server/extensions/design/specimen.js +55 -0
- package/src/server/extensions/design/state.js +72 -7
- package/src/server/extensions/design/style.css +546 -0
- package/src/server/extensions/design/system-actions.js +214 -0
- package/src/server/extensions/design/system-api.ts +62 -0
- package/src/server/extensions/design/system-data.js +315 -0
- package/src/server/extensions/design/system-preview.js +41 -0
- package/src/server/extensions/design/system-routes.ts +1122 -0
- package/src/server/extensions/design/system-sections.js +1344 -0
- package/src/server/extensions/design/system-ui.js +32 -0
- package/src/server/extensions/design/system-view.js +1196 -0
- package/src/server/extensions/design/system-widgets.js +942 -0
- package/src/server/extensions/design/system.css +2313 -0
- package/src/server/extensions/design/system.js +1479 -0
- package/src/server/extensions/design/unsaved.js +20 -0
- package/src/server/extensions/design/views.js +2 -2
- package/src/server/extensions/design/workspace.js +200 -105
- package/src/server/extensions/design.ts +89 -6
- package/src/server/extensions/updates/index.js +31 -9
- package/src/server/extensions/updates/style.css +11 -0
- package/src/server/extensions/updates.ts +29 -1
- package/src/server/host.ts +4 -1
- package/src/server/http/artifact-inspect.ts +338 -24
- package/src/server/updates.ts +235 -53
- package/src/server/versions.ts +71 -0
- package/web/artifact-bridge.js +34 -1
- package/npm-shrinkwrap.json +0 -3162
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
/** The color roles of the scan (stream T): a color for each role, from the names of the project's variables first and from how the colors are used second. */
|
|
2
|
+
import { contrastRatio } from "./color.ts";
|
|
3
|
+
import { type Kinds } from "./scan-facts.ts";
|
|
4
|
+
import { type ColorEntry, distance, type RoleName, ROLES, w } from "./scan-color-base.ts";
|
|
5
|
+
import { nameScore, words } from "./scan-color-names.ts";
|
|
6
|
+
|
|
7
|
+
/** What decides a role's color. */
|
|
8
|
+
export interface RoleChoice {
|
|
9
|
+
role: RoleName;
|
|
10
|
+
hex: string;
|
|
11
|
+
/** `name` when a variable's name chose it; `use` when how the color is used did. */
|
|
12
|
+
basis: "name" | "use";
|
|
13
|
+
/** The variable (or the usage) that chose it, for the report. */
|
|
14
|
+
source: string;
|
|
15
|
+
score: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface VarColor {
|
|
19
|
+
name: string;
|
|
20
|
+
hex: string;
|
|
21
|
+
uses: number;
|
|
22
|
+
/** How the variable is used as a color: as a background, as text, as a border. */
|
|
23
|
+
kinds?: Kinds;
|
|
24
|
+
/** The platform whose naming this variable follows, when that says more than the name does. */
|
|
25
|
+
platform?: "wordpress";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The words of a variable's name. WordPress block themes call the page `base` and the text `contrast` (Twenty Twenty-Four
|
|
30
|
+
* and after), which no other system reads that way.
|
|
31
|
+
*/
|
|
32
|
+
const wordsOf = (variable: VarColor): string[] => {
|
|
33
|
+
const all = words(variable.name);
|
|
34
|
+
|
|
35
|
+
if (variable.platform === "wordpress" && all.length === 1) {
|
|
36
|
+
return all[0] === "base" ? ["bg"] : all[0] === "contrast" ? ["text"] : all;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return all;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const hueOk = (role: RoleName, color: ColorEntry): boolean => {
|
|
43
|
+
if (color.c < 0.06) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
switch (role) {
|
|
48
|
+
case "danger":
|
|
49
|
+
return color.h >= 345 || color.h < 45;
|
|
50
|
+
case "warning":
|
|
51
|
+
return color.h >= 35 && color.h < 115;
|
|
52
|
+
case "success":
|
|
53
|
+
return color.h >= 105 && color.h < 185;
|
|
54
|
+
default:
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Chooses a color for each role. Variable names come first (a variable called `--bg-surface` is the surface), the way
|
|
61
|
+
* the colors are used second. `colors` are all the project's colors; `named` are those that variables hold.
|
|
62
|
+
*/
|
|
63
|
+
export function chooseRoles(
|
|
64
|
+
colors: Map<string, ColorEntry>,
|
|
65
|
+
named: VarColor[],
|
|
66
|
+
/** The hue of a status color that the other mode names (`--success` in the dark scope), to find its pair here. */
|
|
67
|
+
hints: Partial<Record<"danger" | "warning" | "success", number>> = {},
|
|
68
|
+
): Partial<Record<RoleName, RoleChoice>> {
|
|
69
|
+
const chosen: Partial<Record<RoleName, RoleChoice>> = {};
|
|
70
|
+
const entries = [...colors.values()];
|
|
71
|
+
const byUse = (list: ColorEntry[], pick: (entry: ColorEntry) => number) =>
|
|
72
|
+
[...list].sort(
|
|
73
|
+
(a, b) => pick(b) - pick(a) || b.uses - a.uses || a.hex.localeCompare(b.hex),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const set = (
|
|
77
|
+
role: RoleName,
|
|
78
|
+
color: string,
|
|
79
|
+
basis: "name" | "use",
|
|
80
|
+
source: string,
|
|
81
|
+
score: number,
|
|
82
|
+
) => {
|
|
83
|
+
chosen[role] = { role, hex: color, basis, source, score };
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const entry = (hex: string) => colors.get(hex);
|
|
87
|
+
|
|
88
|
+
// 1. Names: the best fitting variable for each role, the page background first (the others are judged against it).
|
|
89
|
+
const pickNamed = (
|
|
90
|
+
role: RoleName,
|
|
91
|
+
bg: ColorEntry | undefined,
|
|
92
|
+
): { variable: VarColor; score: number } | undefined => {
|
|
93
|
+
let best: { variable: VarColor; score: number } | undefined;
|
|
94
|
+
const scored: { variable: VarColor; score: number }[] = [];
|
|
95
|
+
|
|
96
|
+
// A family of numbered steps (`ink-0` … `ink-9`) is a ramp, not the roles that a step's word names.
|
|
97
|
+
const steps = new Map<string, number>();
|
|
98
|
+
|
|
99
|
+
for (const variable of named) {
|
|
100
|
+
const family = /^(.*?)[-_]?(\d{1,3})$/.exec(variable.name)?.[1];
|
|
101
|
+
|
|
102
|
+
if (family !== undefined && family !== "") {
|
|
103
|
+
steps.set(family, (steps.get(family) ?? 0) + 1);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
for (const variable of named) {
|
|
108
|
+
const color = entry(variable.hex);
|
|
109
|
+
const segs = wordsOf(variable);
|
|
110
|
+
|
|
111
|
+
if (
|
|
112
|
+
(role === "background" ||
|
|
113
|
+
role === "surface" ||
|
|
114
|
+
role === "text" ||
|
|
115
|
+
role === "muted" ||
|
|
116
|
+
role === "border") &&
|
|
117
|
+
(steps.get(/^(.*?)[-_]?\d{1,3}$/.exec(variable.name)?.[1] ?? "") ?? 0) >= 6
|
|
118
|
+
) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (
|
|
123
|
+
color === undefined ||
|
|
124
|
+
Object.values(chosen).some(
|
|
125
|
+
(each) =>
|
|
126
|
+
each.hex === variable.hex &&
|
|
127
|
+
each.basis === "name" &&
|
|
128
|
+
each.source === variable.name,
|
|
129
|
+
)
|
|
130
|
+
) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let score = nameScore(role, segs);
|
|
135
|
+
|
|
136
|
+
// A color called `warn` that is red is the error color (Angular Material's `warn` palette).
|
|
137
|
+
if (role === "danger" && score === 0 && color.c >= 0.1 && hueOk("danger", color)) {
|
|
138
|
+
const asWarning = nameScore("warning", segs);
|
|
139
|
+
|
|
140
|
+
score = asWarning > 0 ? asWarning - 15 : 0;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (score === 0) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Of a palette's steps, the middle one is the color (`warn-500`).
|
|
148
|
+
if (
|
|
149
|
+
(role === "danger" || role === "warning" || role === "success") &&
|
|
150
|
+
segs.some((seg) => /^\d{3}$/.test(seg))
|
|
151
|
+
) {
|
|
152
|
+
score += segs.includes("500") ? 8 : segs.includes("600") ? 5 : 0;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Sanity from the color itself.
|
|
156
|
+
if (
|
|
157
|
+
(role === "danger" || role === "warning" || role === "success") &&
|
|
158
|
+
!hueOk(role, color)
|
|
159
|
+
) {
|
|
160
|
+
score -= 45;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (role === "accent") {
|
|
164
|
+
// A color is the brand when it has a hue; a gray one (shadcn's hover surface) is not. How vivid it is only
|
|
165
|
+
// breaks a tie: a muted brand `primary` still beats a vivid `accent-1`.
|
|
166
|
+
score += color.c >= 0.03 ? 20 + Math.min(10, color.c * 40) : -10;
|
|
167
|
+
|
|
168
|
+
// The role's own word beats a synonym, among colors that have a hue.
|
|
169
|
+
if (
|
|
170
|
+
segs.includes("accent") &&
|
|
171
|
+
!segs.includes("primary") &&
|
|
172
|
+
!segs.includes("brand") &&
|
|
173
|
+
color.c >= 0.03
|
|
174
|
+
) {
|
|
175
|
+
score += 10;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// `accent-1`, `accent-2`: one of a set, not the role.
|
|
179
|
+
if (segs.some((seg) => /^\d$/.test(seg))) {
|
|
180
|
+
score -= 15;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// A long name that only holds a role word (`tooltipExternalLinkColor`) is not the brand.
|
|
184
|
+
const strangers = segs.filter(
|
|
185
|
+
(seg) =>
|
|
186
|
+
!/^\d+$/.test(seg) &&
|
|
187
|
+
!w(
|
|
188
|
+
"accent primary brand link highlight cta action interactive main theme key default base 500 600 text dark light hover active focus pressed",
|
|
189
|
+
).includes(seg),
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
score -= Math.min(24, strangers.length * 8);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// How the variable is really used beats what it is called: a "text" color that paints the page is the page.
|
|
196
|
+
const kinds = variable.kinds;
|
|
197
|
+
|
|
198
|
+
if (kinds !== undefined) {
|
|
199
|
+
const fill = kinds.bg;
|
|
200
|
+
const ink = kinds.text;
|
|
201
|
+
|
|
202
|
+
if ((role === "text" || role === "muted") && fill >= 2 && fill > ink * 2) {
|
|
203
|
+
score -= 45;
|
|
204
|
+
} else if (
|
|
205
|
+
(role === "background" || role === "surface") &&
|
|
206
|
+
ink >= 2 &&
|
|
207
|
+
ink > fill * 2
|
|
208
|
+
) {
|
|
209
|
+
score -= 40;
|
|
210
|
+
} else if (role === "border" && fill >= 3 && fill > kinds.border * 3) {
|
|
211
|
+
score -= 30;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (role === "background" && color.c > 0.1) {
|
|
216
|
+
score -= 50;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if ((role === "text" || role === "muted") && bg !== undefined) {
|
|
220
|
+
const ratio = contrastRatio(color.rgba, bg.rgba);
|
|
221
|
+
|
|
222
|
+
// A quiet text a little under 3:1 (a gray on a gray page) is still quiet text; far under is not text.
|
|
223
|
+
score += ratio >= 4.5 ? 8 : ratio >= 3 ? 0 : ratio >= 2.5 ? -18 : -45;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// The most referenced variable wins a tie.
|
|
227
|
+
score += Math.min(8, Math.log2(1 + variable.uses));
|
|
228
|
+
|
|
229
|
+
scored.push({ variable, score });
|
|
230
|
+
|
|
231
|
+
if (best === undefined || score > best.score) {
|
|
232
|
+
best = { variable, score };
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// A surface that is the page's own color is no surface (`--surface-1` is the page, `--surface-2` the card): when
|
|
237
|
+
// another surface has a different color, that one is the role.
|
|
238
|
+
if (
|
|
239
|
+
role === "surface" &&
|
|
240
|
+
best !== undefined &&
|
|
241
|
+
bg !== undefined &&
|
|
242
|
+
best.variable.hex === bg.hex
|
|
243
|
+
) {
|
|
244
|
+
const other = scored
|
|
245
|
+
.filter((each) => each.variable.hex !== bg.hex && each.score >= 60)
|
|
246
|
+
.sort((a, b) => b.score - a.score)[0];
|
|
247
|
+
|
|
248
|
+
if (other !== undefined) {
|
|
249
|
+
best = other;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return best !== undefined && best.score >= 60 ? best : undefined;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const bgChoice = pickNamed("background", undefined);
|
|
257
|
+
|
|
258
|
+
if (bgChoice !== undefined) {
|
|
259
|
+
set("background", bgChoice.variable.hex, "name", bgChoice.variable.name, bgChoice.score);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// The page the text sits on: the named background, else the neutral color most used on the page's root. The text
|
|
263
|
+
// and the quiet text are judged against it (a near-black `--ink` on a near-black page is not the text).
|
|
264
|
+
const bg =
|
|
265
|
+
chosen.background === undefined
|
|
266
|
+
? [...colors.values()]
|
|
267
|
+
.filter(
|
|
268
|
+
(color) =>
|
|
269
|
+
color.c < 0.06 && color.rgba.a > 0.9 && (color.fact?.root ?? 0) > 0,
|
|
270
|
+
)
|
|
271
|
+
.sort((a, b) => (b.fact?.root ?? 0) - (a.fact?.root ?? 0))[0]
|
|
272
|
+
: entry(chosen.background.hex);
|
|
273
|
+
|
|
274
|
+
// A name that fits two roles (`--muted`) goes to the role where the color makes sense.
|
|
275
|
+
for (const role of ROLES.filter((each) => each !== "background")) {
|
|
276
|
+
const found = pickNamed(role, bg);
|
|
277
|
+
|
|
278
|
+
if (found !== undefined) {
|
|
279
|
+
set(role, found.variable.hex, "name", found.variable.name, found.score);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// `--muted` alone: text if it reads on the page, a surface if not.
|
|
284
|
+
if (chosen.muted === undefined || chosen.surface === undefined) {
|
|
285
|
+
const lone = named.find(
|
|
286
|
+
(variable) =>
|
|
287
|
+
variable.name.toLowerCase() === "muted" ||
|
|
288
|
+
variable.name.toLowerCase() === "color-muted",
|
|
289
|
+
);
|
|
290
|
+
const color = lone === undefined ? undefined : entry(lone.hex);
|
|
291
|
+
|
|
292
|
+
if (lone !== undefined && color !== undefined && bg !== undefined) {
|
|
293
|
+
const text = contrastRatio(color.rgba, bg.rgba) >= 3 && distance(color, bg) > 0.2;
|
|
294
|
+
|
|
295
|
+
if (text && chosen.muted === undefined) {
|
|
296
|
+
set("muted", lone.hex, "name", lone.name, 62);
|
|
297
|
+
} else if (
|
|
298
|
+
!text &&
|
|
299
|
+
chosen.surface === undefined &&
|
|
300
|
+
lone.hex !== chosen.background?.hex
|
|
301
|
+
) {
|
|
302
|
+
set("surface", lone.hex, "name", lone.name, 62);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// 2. Use: what the names did not settle.
|
|
308
|
+
const neutral = entries.filter((color) => color.c < 0.06 && color.rgba.a > 0.9);
|
|
309
|
+
const pageSource = (hex: string) => `used on the page ${hex}`;
|
|
310
|
+
|
|
311
|
+
if (chosen.background === undefined) {
|
|
312
|
+
const top =
|
|
313
|
+
byUse(
|
|
314
|
+
neutral.filter((color) => (color.fact?.root ?? 0) > 0),
|
|
315
|
+
(color) => color.fact?.root ?? 0,
|
|
316
|
+
)[0] ??
|
|
317
|
+
byUse(
|
|
318
|
+
neutral.filter((color) => (color.fact?.bg ?? 0) > 0),
|
|
319
|
+
(color) => color.fact?.bg ?? 0,
|
|
320
|
+
)[0];
|
|
321
|
+
|
|
322
|
+
if (top !== undefined) {
|
|
323
|
+
set("background", top.hex, "use", pageSource(top.hex), 40);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const page = chosen.background === undefined ? undefined : entry(chosen.background.hex);
|
|
328
|
+
const used = (hex: string) => Object.values(chosen).some((each) => each.hex === hex);
|
|
329
|
+
|
|
330
|
+
if (chosen.surface === undefined && page !== undefined) {
|
|
331
|
+
const top = byUse(
|
|
332
|
+
neutral.filter(
|
|
333
|
+
(color) =>
|
|
334
|
+
!used(color.hex) &&
|
|
335
|
+
(color.fact?.bg ?? 0) > 0 &&
|
|
336
|
+
distance(color, page) > 0.012 &&
|
|
337
|
+
distance(color, page) < 0.3,
|
|
338
|
+
),
|
|
339
|
+
// A background on a card or a panel counts three times.
|
|
340
|
+
(color) => (color.fact?.surface ?? 0) * 3 + (color.fact?.bg ?? 0),
|
|
341
|
+
)[0];
|
|
342
|
+
|
|
343
|
+
if (top !== undefined) {
|
|
344
|
+
set("surface", top.hex, "use", "most used background of cards and panels", 35);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (chosen.text === undefined) {
|
|
349
|
+
const candidates = entries.filter(
|
|
350
|
+
(color) =>
|
|
351
|
+
!used(color.hex) &&
|
|
352
|
+
color.rgba.a > 0.9 &&
|
|
353
|
+
color.c < 0.1 &&
|
|
354
|
+
(color.fact?.text ?? 0) > 0 &&
|
|
355
|
+
(page === undefined || contrastRatio(color.rgba, page.rgba) >= 4.5),
|
|
356
|
+
);
|
|
357
|
+
const top = byUse(
|
|
358
|
+
candidates,
|
|
359
|
+
(color) => (color.fact?.body ?? 0) * 6 + (color.fact?.text ?? 0),
|
|
360
|
+
)[0];
|
|
361
|
+
|
|
362
|
+
if (top !== undefined) {
|
|
363
|
+
set("text", top.hex, "use", "most used text color", 40);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const ink = chosen.text === undefined ? undefined : entry(chosen.text.hex);
|
|
368
|
+
|
|
369
|
+
if (chosen.muted === undefined && ink !== undefined && page !== undefined) {
|
|
370
|
+
const top = byUse(
|
|
371
|
+
entries.filter((color) => {
|
|
372
|
+
const ratio = contrastRatio(color.rgba, page.rgba);
|
|
373
|
+
|
|
374
|
+
return (
|
|
375
|
+
!used(color.hex) &&
|
|
376
|
+
color.rgba.a > 0.9 &&
|
|
377
|
+
color.c < 0.1 &&
|
|
378
|
+
(color.fact?.text ?? 0) > 0 &&
|
|
379
|
+
ratio >= 3 &&
|
|
380
|
+
ratio < contrastRatio(ink.rgba, page.rgba) - 0.5
|
|
381
|
+
);
|
|
382
|
+
}),
|
|
383
|
+
// A quiet text is a gray: a tinted color (a light link color) is a weaker candidate than a gray.
|
|
384
|
+
// A quiet text is also clearly softer than the body text.
|
|
385
|
+
(color) =>
|
|
386
|
+
(color.fact?.text ?? 0) *
|
|
387
|
+
(color.c < 0.05 ? 1 : 0.3) *
|
|
388
|
+
(contrastRatio(color.rgba, page.rgba) <= contrastRatio(ink.rgba, page.rgba) * 0.7
|
|
389
|
+
? 1
|
|
390
|
+
: 0.5),
|
|
391
|
+
)[0];
|
|
392
|
+
|
|
393
|
+
if (top !== undefined) {
|
|
394
|
+
set("muted", top.hex, "use", "second most used text color", 35);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (chosen.border === undefined) {
|
|
399
|
+
const top =
|
|
400
|
+
byUse(
|
|
401
|
+
neutral.filter((color) => !used(color.hex) && (color.fact?.border ?? 0) > 0),
|
|
402
|
+
(color) => color.fact?.border ?? 0,
|
|
403
|
+
)[0] ??
|
|
404
|
+
byUse(
|
|
405
|
+
entries.filter(
|
|
406
|
+
(color) => !used(color.hex) && color.c < 0.08 && (color.fact?.border ?? 0) > 0,
|
|
407
|
+
),
|
|
408
|
+
(color) => color.fact?.border ?? 0,
|
|
409
|
+
)[0];
|
|
410
|
+
|
|
411
|
+
if (top !== undefined) {
|
|
412
|
+
set("border", top.hex, "use", "most used border color", 35);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const chroma = entries.filter((color) => color.c >= 0.07 && color.rgba.a > 0.9);
|
|
417
|
+
|
|
418
|
+
// A status color that the other mode names has its pair in this mode: the used color of the same hue, before the
|
|
419
|
+
// most used chromatic color is taken for the accent.
|
|
420
|
+
for (const role of ["danger", "warning", "success"] as const) {
|
|
421
|
+
const hue = hints[role];
|
|
422
|
+
|
|
423
|
+
if (hue === undefined || chosen[role] !== undefined) {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const pair = byUse(
|
|
428
|
+
chroma.filter(
|
|
429
|
+
(color) =>
|
|
430
|
+
!used(color.hex) &&
|
|
431
|
+
color.c >= 0.1 &&
|
|
432
|
+
color.uses >= 1 &&
|
|
433
|
+
Math.abs(((color.h - hue + 540) % 360) - 180) <= 30,
|
|
434
|
+
),
|
|
435
|
+
(color) => color.uses,
|
|
436
|
+
)[0];
|
|
437
|
+
|
|
438
|
+
if (pair !== undefined) {
|
|
439
|
+
set(role, pair.hex, "use", `used ${pair.uses}× with the hue of the dark ${role}`, 32);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (chosen.accent === undefined) {
|
|
444
|
+
const statusSingles = new Set(
|
|
445
|
+
chroma
|
|
446
|
+
.filter(
|
|
447
|
+
(color) =>
|
|
448
|
+
!used(color.hex) &&
|
|
449
|
+
color.uses <= 1 &&
|
|
450
|
+
color.names.length === 0 &&
|
|
451
|
+
color.c >= 0.1,
|
|
452
|
+
)
|
|
453
|
+
.flatMap((color) =>
|
|
454
|
+
(["danger", "warning", "success"] as const).filter((role) =>
|
|
455
|
+
hueOk(role, color),
|
|
456
|
+
),
|
|
457
|
+
),
|
|
458
|
+
).size;
|
|
459
|
+
const top = byUse(
|
|
460
|
+
// Reds, oranges, and greens that are each used once (all of the statuses at once) are more likely the
|
|
461
|
+
// statuses than the brand.
|
|
462
|
+
chroma.filter(
|
|
463
|
+
(color) =>
|
|
464
|
+
!used(color.hex) &&
|
|
465
|
+
// A color that a variable names for a status or a notice (`--invalid`, `--note`) is not the brand.
|
|
466
|
+
!color.names.some(
|
|
467
|
+
(each) =>
|
|
468
|
+
(["danger", "warning", "success"] as const).some(
|
|
469
|
+
(role) => nameScore(role, words(each.name)) >= 48,
|
|
470
|
+
) ||
|
|
471
|
+
words(each.name).some((word) =>
|
|
472
|
+
["info", "note", "notice"].includes(word),
|
|
473
|
+
),
|
|
474
|
+
) &&
|
|
475
|
+
!(
|
|
476
|
+
statusSingles >= 2 &&
|
|
477
|
+
color.uses <= 1 &&
|
|
478
|
+
color.names.length === 0 &&
|
|
479
|
+
color.c >= 0.1 &&
|
|
480
|
+
(["danger", "warning", "success"] as const).some((role) =>
|
|
481
|
+
hueOk(role, color),
|
|
482
|
+
)
|
|
483
|
+
),
|
|
484
|
+
),
|
|
485
|
+
(color) =>
|
|
486
|
+
(color.fact?.bg ?? 0) * 1.4 +
|
|
487
|
+
(color.fact?.text ?? 0) +
|
|
488
|
+
(color.fact?.border ?? 0) +
|
|
489
|
+
(color.fact?.fill ?? 0) +
|
|
490
|
+
// A color a variable names is a token of the design; a literal in a gradient is a decoration.
|
|
491
|
+
(color.names.length > 0 ? 2 : 0) +
|
|
492
|
+
color.c * 5,
|
|
493
|
+
)[0];
|
|
494
|
+
|
|
495
|
+
if (top !== undefined) {
|
|
496
|
+
set("accent", top.hex, "use", "most used chromatic color", 35);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// The hue each status color sits nearest to (Oklch degrees): a red close to 27° is an error, not an orange.
|
|
501
|
+
const centers = { danger: 27, warning: 70, success: 148 } as const;
|
|
502
|
+
const accentColor = chosen.accent === undefined ? undefined : entry(chosen.accent.hex);
|
|
503
|
+
// A color named for the accent's variable and a suffix (`--glow-deep`, `--brandHover`) is a step of the accent.
|
|
504
|
+
const variantOfAccent = (color: ColorEntry) =>
|
|
505
|
+
(accentColor?.names ?? []).some((stem) =>
|
|
506
|
+
color.names.some(
|
|
507
|
+
(each) =>
|
|
508
|
+
each.name !== stem.name &&
|
|
509
|
+
each.name.startsWith(stem.name) &&
|
|
510
|
+
/^[-_A-Z0-9]/.test(each.name.slice(stem.name.length)),
|
|
511
|
+
),
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
for (const role of ["danger", "warning", "success"] as const) {
|
|
515
|
+
if (chosen[role] === undefined) {
|
|
516
|
+
const closeness = (color: ColorEntry) => {
|
|
517
|
+
const gap = Math.abs(((color.h - centers[role] + 540) % 360) - 180);
|
|
518
|
+
|
|
519
|
+
return 1 - Math.min(1, gap / 40);
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
const top = byUse(
|
|
523
|
+
chroma.filter(
|
|
524
|
+
(color) =>
|
|
525
|
+
!used(color.hex) &&
|
|
526
|
+
hueOk(role, color) &&
|
|
527
|
+
color.c >= 0.1 &&
|
|
528
|
+
// Used, or named by a variable (`--stop` is defined, and no rule needs to use it).
|
|
529
|
+
(color.uses >= 1 || color.names.length > 0) &&
|
|
530
|
+
!variantOfAccent(color) &&
|
|
531
|
+
// A shade of the accent (near it, or of the same hue) is not a status color; a color that has a
|
|
532
|
+
// name of its own (`--go` beside `--glow`) is its own.
|
|
533
|
+
(accentColor === undefined ||
|
|
534
|
+
(distance(color, accentColor) > 0.06 &&
|
|
535
|
+
!(
|
|
536
|
+
accentColor.c >= 0.07 &&
|
|
537
|
+
color.names.length === 0 &&
|
|
538
|
+
Math.abs(((color.h - accentColor.h + 540) % 360) - 180) < 8
|
|
539
|
+
))),
|
|
540
|
+
),
|
|
541
|
+
// A color a variable names is the design's; a literal used once may be a decoration.
|
|
542
|
+
(color) =>
|
|
543
|
+
Math.log2(1 + color.uses) * 2 +
|
|
544
|
+
closeness(color) * 3 +
|
|
545
|
+
(color.names.length > 0 ? 3 : 0),
|
|
546
|
+
)[0];
|
|
547
|
+
|
|
548
|
+
if (top !== undefined) {
|
|
549
|
+
set(
|
|
550
|
+
role,
|
|
551
|
+
top.hex,
|
|
552
|
+
"use",
|
|
553
|
+
top.uses === 0
|
|
554
|
+
? `named ${top.names[0]!.name}, with the hue of ${role}`
|
|
555
|
+
: `used ${top.uses}× with the hue of ${role}`,
|
|
556
|
+
30,
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
return chosen;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// --- Palette names ----------------------------------------------------------------------------------------------------
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colors for the design system scan (stream T): merges near-identical colors, decides which color plays which role
|
|
3
|
+
* (page background, surface, text, muted text, border, accent, danger, warning, success) from the names of the
|
|
4
|
+
* project's variables first and from how the colors are used second, and names the palette by hue and lightness. The
|
|
5
|
+
* work is in `scan-color-base.ts` (the entries and the merging), `scan-color-names.ts` (how a name fits a role),
|
|
6
|
+
* `scan-color-roles.ts` (the choice of a color for each role), and `scan-color-palette.ts` (the palette's names); this
|
|
7
|
+
* file is the one place the rest of the scan imports them from.
|
|
8
|
+
*/
|
|
9
|
+
export {
|
|
10
|
+
ROLE_PATHS,
|
|
11
|
+
type ColorEntry,
|
|
12
|
+
type RoleName,
|
|
13
|
+
distance,
|
|
14
|
+
entryOf,
|
|
15
|
+
merge,
|
|
16
|
+
mergeColors,
|
|
17
|
+
nearColors,
|
|
18
|
+
totalUses,
|
|
19
|
+
} from "./scan-color-base.ts";
|
|
20
|
+
export { nameScore, words } from "./scan-color-names.ts";
|
|
21
|
+
export { STEPS, family, paletteName, step } from "./scan-color-palette.ts";
|
|
22
|
+
export { type RoleChoice, type VarColor, chooseRoles } from "./scan-color-roles.ts";
|