@jonandev/longbeard 0.11.2 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +4 -5
- package/CHANGELOG.md +72 -0
- package/README.md +64 -43
- package/beard.json +124 -28
- package/bin/long-beard.js +171 -36
- package/package.json +1 -1
- package/skills/design/SKILL.md +51 -126
- package/skills/design-brief/SKILL.md +146 -0
- package/skills/design-brief/references/graphic.md +59 -0
- package/skills/design-check/SKILL.md +27 -11
- package/skills/design-check/references/design-rules.md +21 -7
- package/skills/design-check/scripts/capture.mjs +121 -37
- package/skills/design-check/scripts/contrast.mjs +23 -9
- package/skills/design-check/scripts/rules.mjs +613 -0
- package/skills/design-check/scripts/screen-contrast.mjs +428 -0
- package/skills/design-check/scripts/tokens.mjs +346 -0
- package/skills/design-content/SKILL.md +118 -0
- package/skills/design-content/scripts/contact-sheet.mjs +259 -0
- package/skills/design-content/scripts/pictures.mjs +197 -0
- package/skills/design-direction/SKILL.md +115 -200
- package/skills/design-direction/references/controls.md +58 -0
- package/skills/{design → design-direction}/references/design-rules.md +21 -7
- package/skills/design-direction/references/five-second.md +47 -0
- package/skills/design-direction/references/full-build.md +19 -0
- package/skills/design-direction/references/graphic.md +35 -0
- package/skills/design-direction/references/grid.md +114 -0
- package/skills/design-direction/references/library.md +75 -0
- package/skills/design-direction/references/screen-template.html +233 -0
- package/skills/design-direction/references/subject.md +33 -0
- package/skills/design-direction/scripts/capture.mjs +121 -37
- package/skills/design-direction/scripts/contact-sheet.mjs +18 -7
- package/skills/design-direction/scripts/fonts.mjs +276 -0
- package/skills/design-direction/scripts/layout-diff.mjs +118 -0
- package/skills/design-direction/scripts/measure.mjs +1728 -0
- package/skills/design-direction/scripts/moodboard.mjs +123 -32
- package/skills/design-direction/scripts/picture-cast.mjs +270 -0
- package/skills/design-direction/scripts/pictures.mjs +197 -0
- package/skills/design-direction/scripts/screen-contrast.mjs +428 -0
- package/skills/design-direction/scripts/sheet.mjs +67 -0
- package/skills/design-direction/scripts/subject-colours.mjs +410 -0
- package/skills/design-families/SKILL.md +138 -0
- package/skills/design-families/scripts/contact-sheet.mjs +259 -0
- package/skills/design-families/scripts/families.mjs +606 -0
- package/skills/design-families/scripts/measure.mjs +1728 -0
- package/skills/design-families/scripts/moodboard.mjs +467 -0
- package/skills/design-families/scripts/tag.mjs +289 -0
- package/skills/design-figma/SKILL.md +69 -0
- package/skills/design-figma/references/graphic.md +26 -0
- package/skills/design-identity/SKILL.md +134 -0
- package/skills/design-identity/references/graphic.md +8 -0
- package/skills/design-identity/references/identity-board.html +140 -0
- package/skills/{design → design-identity}/scripts/capture.mjs +121 -37
- package/skills/design-identity/scripts/colours.mjs +213 -0
- package/skills/design-identity/scripts/contrast.mjs +41 -0
- package/skills/design-identity/scripts/fonts.mjs +276 -0
- package/skills/design-identity/scripts/measure.mjs +1728 -0
- package/skills/design-identity/scripts/sheet.mjs +67 -0
- package/skills/design-identity/scripts/subject-colours.mjs +410 -0
- package/skills/design-moodboard/SKILL.md +184 -0
- package/skills/design-moodboard/references/graphic.md +25 -0
- package/skills/{design-direction → design-moodboard}/references/sources.md +21 -11
- package/skills/design-moodboard/scripts/capture.mjs +893 -0
- package/skills/design-moodboard/scripts/contact-sheet.mjs +259 -0
- package/skills/design-moodboard/scripts/crops.mjs +256 -0
- package/skills/design-moodboard/scripts/dribbble-cards.mjs +418 -0
- package/skills/design-moodboard/scripts/families.mjs +606 -0
- package/skills/design-moodboard/scripts/group.mjs +221 -0
- package/skills/design-moodboard/scripts/measure.mjs +1728 -0
- package/skills/design-moodboard/scripts/moodboard.mjs +467 -0
- package/skills/{design-direction → design-moodboard}/scripts/picker.mjs +121 -20
- package/skills/design-moodboard/scripts/pictures.mjs +197 -0
- package/skills/design-moodboard/scripts/tag.mjs +289 -0
- package/skills/design-prototype/SKILL.md +149 -0
- package/skills/design-prototype/references/design-md-template.md +82 -0
- package/skills/design-prototype/references/design-rules.md +203 -0
- package/skills/design-prototype/references/five-second.md +47 -0
- package/skills/design-prototype/references/graphic.md +27 -0
- package/skills/design-prototype/scripts/capture.mjs +893 -0
- package/skills/design-prototype/scripts/contrast.mjs +41 -0
- package/skills/design-prototype/scripts/links.mjs +308 -0
- package/skills/design-prototype/scripts/pictures.mjs +197 -0
- package/skills/design-prototype/scripts/rules.mjs +613 -0
- package/skills/design-prototype/scripts/screen-contrast.mjs +428 -0
- package/skills/design-prototype/scripts/sheet.mjs +67 -0
- package/skills/design-prototype/scripts/tokens.mjs +346 -0
- package/skills/design-wordmark/SKILL.md +65 -0
- package/skills/design-wordmark/scripts/capture.mjs +893 -0
- package/skills/design-wordmark/scripts/sheet.mjs +67 -0
- package/skills/framework-skills/SKILL.md +71 -0
- package/skills/idea/SKILL.md +13 -7
- package/skills/idea/references/graphic.md +64 -0
- package/skills/design/references/design-md-template.md +0 -58
- package/skills/design/scripts/contrast.mjs +0 -27
- package/skills/design-direction/scripts/dribbble-cards.mjs +0 -356
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Lay screenshots out as one numbered sheet, so a round of first screens can
|
|
3
|
+
// be seen and picked from together. It writes an HTML page; capture.mjs turns
|
|
4
|
+
// that into the picture to show. No dependencies.
|
|
5
|
+
//
|
|
6
|
+
// node sheet.mjs .longbeard/design/direction/grid/sheet.html .longbeard/design/direction/grid/1.png .longbeard/design/direction/grid/2.png …
|
|
7
|
+
// node sheet.mjs .longbeard/design/direction/grid/vary.html --cols 5 .longbeard/design/direction/grid/v*.png
|
|
8
|
+
// node capture.mjs "file://$PWD/.longbeard/design/direction/grid/sheet.html" --out .longbeard/design/direction/grid/sheet.png
|
|
9
|
+
// node sheet.mjs <out.html> --rough 1,2,4 --note "built without fresh agents: weaker" <shots…>
|
|
10
|
+
//
|
|
11
|
+
// --rough 1,2,4 (or none) labels those tiles "rough" and the rest "full", so a quick screen is never
|
|
12
|
+
// read as a finished one. --note prints a line under the sheet.
|
|
13
|
+
|
|
14
|
+
// Tiles are numbered in the order given, left to right, top to bottom, which
|
|
15
|
+
// is the recipe's number when the shots are passed in order.
|
|
16
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
17
|
+
import { dirname, relative, resolve } from "node:path";
|
|
18
|
+
|
|
19
|
+
const args = process.argv.slice(2);
|
|
20
|
+
const at = args.indexOf("--cols");
|
|
21
|
+
const cols = at === -1 ? 2 : Number(args.splice(at, 2)[1]);
|
|
22
|
+
const take = (name) => {
|
|
23
|
+
const i = args.indexOf(name);
|
|
24
|
+
return i === -1 ? null : (args.splice(i, 2)[1] ?? "");
|
|
25
|
+
};
|
|
26
|
+
const roughArg = take("--rough");
|
|
27
|
+
const note = take("--note");
|
|
28
|
+
const rough = new Set((roughArg ?? "").split(",").filter((n) => n && n !== "none").map(Number));
|
|
29
|
+
const [outArg, ...shots] = args;
|
|
30
|
+
if (!outArg || [...rough].some((n) => !Number.isInteger(n) || n < 1) || roughArg === "" || note === "" || !/\.html$/.test(outArg) || !shots.length || !Number.isInteger(cols) || cols < 1 || cols > 8) {
|
|
31
|
+
console.error('usage: node sheet.mjs <out.html> [--cols 2] [--rough 1,2|none] [--note "text"] <shot.png> …');
|
|
32
|
+
process.exit(2);
|
|
33
|
+
}
|
|
34
|
+
const out = resolve(outArg);
|
|
35
|
+
for (const shot of shots) {
|
|
36
|
+
if (!existsSync(resolve(shot))) {
|
|
37
|
+
console.error(`sheet: no file at ${shot}`);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const escape = (text) => text.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
|
42
|
+
// A local path in a src: each part encoded, so # ? % and spaces stay in the name.
|
|
43
|
+
const urlPath = (path) => path.split("/").map(encodeURIComponent).join("/");
|
|
44
|
+
const tiles = shots
|
|
45
|
+
.map((shot, i) => `<figure><img src="${escape(urlPath(relative(dirname(out), resolve(shot)).split("\\").join("/")))}" alt="Screen ${i + 1}"><figcaption>${i + 1}${roughArg === null ? "" : rough.has(i + 1) ? " · rough" : " · full"}</figcaption></figure>`)
|
|
46
|
+
.join("\n");
|
|
47
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
48
|
+
writeFileSync(
|
|
49
|
+
out,
|
|
50
|
+
`<!doctype html>
|
|
51
|
+
<meta charset="utf-8">
|
|
52
|
+
<title>Sheet</title>
|
|
53
|
+
<style>
|
|
54
|
+
body { margin: 0; padding: 24px; background: #6f6f6f; font: 600 22px/1 system-ui, sans-serif; }
|
|
55
|
+
main { display: grid; grid-template-columns: repeat(${cols}, 720px); gap: 24px; width: max-content; }
|
|
56
|
+
figure { margin: 0; display: flex; flex-direction: column-reverse; gap: 8px; }
|
|
57
|
+
img { display: block; width: 720px; height: auto; }
|
|
58
|
+
/* The number sits above the screen, never on it: a corner of a screen is where its name or menu is. */
|
|
59
|
+
p { margin: 24px 0 0; max-width: 1200px; font-weight: 400; font-size: 20px; line-height: 1.4; color: #fff; }
|
|
60
|
+
figcaption { align-self: flex-start; min-width: 44px; padding: 8px 10px; text-align: center; background: #111; color: #fff; }
|
|
61
|
+
</style>
|
|
62
|
+
<main>
|
|
63
|
+
${tiles}
|
|
64
|
+
</main>
|
|
65
|
+
${note ? `<p>${escape(note)}</p>\n` : ""}`,
|
|
66
|
+
);
|
|
67
|
+
console.log(`${shots.length} screens, ${cols} across: ${outArg}`);
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Colour proposals from the product's subject pictures: three or four photos
|
|
3
|
+
// of what the product is about (loaves, for a bakery). The pictures' raw
|
|
4
|
+
// colours are greys, shadows and backgrounds, so they are never used as
|
|
5
|
+
// found: each role is taken from the right colour and tuned by rule.
|
|
6
|
+
// Node 22+, an installed Chrome (measure.mjs reads the pictures). No
|
|
7
|
+
// dependencies.
|
|
8
|
+
//
|
|
9
|
+
// node subject-colours.mjs .longbeard/design/subject
|
|
10
|
+
// node subject-colours.mjs a.jpg b.jpg c.jpg --json
|
|
11
|
+
// node subject-colours.mjs .longbeard/design/subject --sheet subject.html
|
|
12
|
+
// node subject-colours.mjs --from swatches.json --exclude "#806c71"
|
|
13
|
+
// node subject-colours.mjs .longbeard/design/subject --level AAA
|
|
14
|
+
//
|
|
15
|
+
// measure.mjs --swatches gives each picture's ten colours. They are pooled
|
|
16
|
+
// across pictures, near-twins merged, and noise dropped: under 2%, specular
|
|
17
|
+
// highlights, and black shadows (kept only to give a dark ground).
|
|
18
|
+
//
|
|
19
|
+
// A colour that lies mostly at the pictures' edges is the backdrop, not the
|
|
20
|
+
// subject (a blue wall, a striped cloth): edge over 0.55, or over 0.4 in one
|
|
21
|
+
// picture only. It is marked background and is never the signature or the
|
|
22
|
+
// accent. A colour found in two or more pictures is preferred to one in one.
|
|
23
|
+
// --exclude "#hex,#hex" drops colours near those (OKLab 0.06), for one you saw
|
|
24
|
+
// is not the subject: run again without it.
|
|
25
|
+
//
|
|
26
|
+
// - Signature: the heaviest colour with some chroma (the crust).
|
|
27
|
+
// - Light ground: the heaviest light colour, else the signature's hue, made an
|
|
28
|
+
// off-white that keeps its hue. Dark ground: the same, made a near-black.
|
|
29
|
+
// --level AA|AAA is the design brief's accessibility. AA asks 4.5:1 for text
|
|
30
|
+
// and 3:1 for an accent; AAA asks 7:1 and 4.5:1 (an accent may carry large
|
|
31
|
+
// text). Below, "4.5:1" and "3:1" are those two, at AA.
|
|
32
|
+
//
|
|
33
|
+
// - Ink: the other ground's hue, dark on light and light on dark, 4.5:1 or
|
|
34
|
+
// darker (lighter) until it is.
|
|
35
|
+
// - Accent: the most colourful heavy colour whose hue stands off the ground,
|
|
36
|
+
// else the signature; chroma raised, lightness 0.55 to 0.72, 3:1 on its
|
|
37
|
+
// ground or left out (and said).
|
|
38
|
+
// - Signature ground: the signature at lightness 0.5 to 0.62 where one of the
|
|
39
|
+
// two inks reads on it, or left out (and said).
|
|
40
|
+
//
|
|
41
|
+
// Prints three proposals (light, dark, signature) as a table: role, colour,
|
|
42
|
+
// the colour it came from (its share, in how many pictures), how it was tuned,
|
|
43
|
+
// its contrast. --json prints { signature, colours, proposals, library, notes }:
|
|
44
|
+
// colours are the pooled colours that survived the noise rules, and library
|
|
45
|
+
// holds one or two grounds ({ id, ground, ink, from: ["subject"] }), the
|
|
46
|
+
// subject's palette for library.json, and an accent only when the subject has a second hue of its own
|
|
47
|
+
// (a one-hue accent stays in the proposals, for design-identity to offer).
|
|
48
|
+
// The light ground is a quiet tint. The second may be the signature at full
|
|
49
|
+
// strength: a screen borrows it as a ground only when a reference has a ground
|
|
50
|
+
// that strong, and as an accent otherwise, so the subject's colours never push
|
|
51
|
+
// a board somewhere it is not.
|
|
52
|
+
//
|
|
53
|
+
// --sheet out.html writes a page: each proposal's colours as large swatches,
|
|
54
|
+
// and beside each, every picture with the pixels near its source colour lit
|
|
55
|
+
// and the rest dimmed, so the eye can check each colour is the subject.
|
|
56
|
+
// The pictures are written into the page (a browser will not let a page read
|
|
57
|
+
// the pixels of a file:// picture), so the page is about as large as they are.
|
|
58
|
+
import { spawnSync } from "node:child_process";
|
|
59
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
60
|
+
import { dirname, extname, join, resolve } from "node:path";
|
|
61
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
62
|
+
|
|
63
|
+
const fail = (text) => {
|
|
64
|
+
console.error(`subject-colours: ${text}`);
|
|
65
|
+
process.exit(2);
|
|
66
|
+
};
|
|
67
|
+
const usage = 'usage: node subject-colours.mjs <folder | image …> | --from swatches.json [--json] [--level AA|AAA] [--exclude "#hex,#hex"] [--sheet out.html]';
|
|
68
|
+
const args = process.argv.slice(2);
|
|
69
|
+
const option = (name) => {
|
|
70
|
+
const i = args.indexOf(`--${name}`);
|
|
71
|
+
if (i === -1) return null;
|
|
72
|
+
const value = args[i + 1];
|
|
73
|
+
if (!value || value.startsWith("--")) fail(`--${name} needs a value\n${usage}`);
|
|
74
|
+
args.splice(i, 2);
|
|
75
|
+
return value;
|
|
76
|
+
};
|
|
77
|
+
const json = args.includes("--json");
|
|
78
|
+
if (json) args.splice(args.indexOf("--json"), 1);
|
|
79
|
+
const fromArg = option("from");
|
|
80
|
+
const excludeArg = option("exclude");
|
|
81
|
+
const sheetArg = option("sheet");
|
|
82
|
+
const level = (option("level") || "AA").toUpperCase();
|
|
83
|
+
if (level !== "AA" && level !== "AAA") fail(`--level is AA or AAA, not ${level}`);
|
|
84
|
+
const textNeed = level === "AAA" ? 7 : 4.5;
|
|
85
|
+
const accentNeed = level === "AAA" ? 4.5 : 3;
|
|
86
|
+
if (args.some((a) => a.startsWith("--")) || (fromArg ? args.length : !args.length)) fail(usage);
|
|
87
|
+
const hex6 = (v) => {
|
|
88
|
+
const s = String(v || "").trim().toLowerCase();
|
|
89
|
+
if (/^#[0-9a-f]{3}$/.test(s)) return `#${[...s.slice(1)].map((c) => c + c).join("")}`;
|
|
90
|
+
return /^#[0-9a-f]{6}$/.test(s) ? s : null;
|
|
91
|
+
};
|
|
92
|
+
const excluded = excludeArg ? excludeArg.split(",").map((v) => hex6(v) || fail(`--exclude takes hex colours, not ${v.trim()}`)) : [];
|
|
93
|
+
|
|
94
|
+
let measured;
|
|
95
|
+
let base = process.cwd();
|
|
96
|
+
if (fromArg) {
|
|
97
|
+
if (!existsSync(fromArg)) fail(`no file at ${fromArg}`);
|
|
98
|
+
try {
|
|
99
|
+
measured = JSON.parse(readFileSync(fromArg, "utf8"));
|
|
100
|
+
} catch {
|
|
101
|
+
fail(`${fromArg} is not JSON`);
|
|
102
|
+
}
|
|
103
|
+
base = dirname(resolve(fromArg));
|
|
104
|
+
} else {
|
|
105
|
+
// measure.mjs is copied beside this script.
|
|
106
|
+
const measure = join(dirname(fileURLToPath(import.meta.url)), "measure.mjs");
|
|
107
|
+
const run = spawnSync(process.execPath, [measure, ...args, "--swatches"], { encoding: "utf8", maxBuffer: 64 << 20 });
|
|
108
|
+
if (run.stderr) process.stderr.write(run.stderr);
|
|
109
|
+
if (!run.stdout.trim()) process.exit(run.status || 1);
|
|
110
|
+
measured = JSON.parse(run.stdout);
|
|
111
|
+
}
|
|
112
|
+
if (!Array.isArray(measured)) fail("swatches JSON must be a list of { target, swatches }");
|
|
113
|
+
const pictures = measured.filter((m) => Array.isArray(m?.swatches) && m.swatches.length);
|
|
114
|
+
if (!pictures.length) fail("no picture had any swatches");
|
|
115
|
+
|
|
116
|
+
// sRGB, OKLab and OKLCH, both ways. Out of gamut, chroma is reduced until the colour fits.
|
|
117
|
+
const toLin = (c) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);
|
|
118
|
+
const toGam = (c) => (c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055);
|
|
119
|
+
const rgbOf = (hex) => [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255);
|
|
120
|
+
function labOf(hex) {
|
|
121
|
+
const [r, g, b] = rgbOf(hex).map(toLin);
|
|
122
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
123
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
124
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
125
|
+
return [0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s, 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s, 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s];
|
|
126
|
+
}
|
|
127
|
+
function linOfLab([L, a, b]) {
|
|
128
|
+
const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
|
|
129
|
+
const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
|
|
130
|
+
const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
|
|
131
|
+
return [4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s];
|
|
132
|
+
}
|
|
133
|
+
const lchOfLab = ([L, a, b]) => ({ L, C: Math.hypot(a, b), h: ((Math.atan2(b, a) * 180) / Math.PI + 360) % 360 });
|
|
134
|
+
const labOfLch = ({ L, C, h }) => [L, C * Math.cos((h * Math.PI) / 180), C * Math.sin((h * Math.PI) / 180)];
|
|
135
|
+
const inGamut = (lin) => lin.every((c) => c >= -0.0001 && c <= 1.0001);
|
|
136
|
+
// The colour at this lightness and hue with as much of the chroma asked for as sRGB holds.
|
|
137
|
+
function fit({ L, C, h }) {
|
|
138
|
+
L = Math.min(1, Math.max(0, L));
|
|
139
|
+
if (!inGamut(linOfLab(labOfLch({ L, C, h })))) {
|
|
140
|
+
let lo = 0, hi = C;
|
|
141
|
+
for (let i = 0; i < 24; i++) {
|
|
142
|
+
const mid = (lo + hi) / 2;
|
|
143
|
+
if (inGamut(linOfLab(labOfLch({ L, C: mid, h })))) lo = mid;
|
|
144
|
+
else hi = mid;
|
|
145
|
+
}
|
|
146
|
+
C = lo;
|
|
147
|
+
}
|
|
148
|
+
return { L, C, h };
|
|
149
|
+
}
|
|
150
|
+
const hexOf = (lch) => `#${linOfLab(labOfLch(fit(lch))).map((c) => Math.round(Math.min(1, Math.max(0, toGam(Math.min(1, Math.max(0, c))))) * 255).toString(16).padStart(2, "0")).join("")}`;
|
|
151
|
+
const lumOf = (hex) => {
|
|
152
|
+
const [r, g, b] = rgbOf(hex).map(toLin);
|
|
153
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
154
|
+
};
|
|
155
|
+
const contrast = (x, y) => {
|
|
156
|
+
const [a, b] = [lumOf(x), lumOf(y)].sort((p, q) => q - p);
|
|
157
|
+
return (a + 0.05) / (b + 0.05);
|
|
158
|
+
};
|
|
159
|
+
const distance = (p, q) => Math.hypot(p[0] - q[0], p[1] - q[1], p[2] - q[2]);
|
|
160
|
+
const hueGap = (a, b) => Math.abs(((a - b + 540) % 360) - 180);
|
|
161
|
+
const r2 = (v) => Math.round(v * 100) / 100;
|
|
162
|
+
const r3 = (v) => Math.round(v * 1000) / 1000;
|
|
163
|
+
// A contrast is cut, not rounded, so one just under its need never prints as the need.
|
|
164
|
+
const cut2 = (v) => Math.floor(v * 100) / 100;
|
|
165
|
+
|
|
166
|
+
// Pool every picture's swatches, each share counted as a share of all the pictures.
|
|
167
|
+
const pooled = [];
|
|
168
|
+
pictures.forEach((p, n) => {
|
|
169
|
+
for (const s of p.swatches) if (hex6(s.hex) && s.share > 0) pooled.push({ lab: labOf(hex6(s.hex)), share: s.share / pictures.length, edge: Number.isFinite(s.edge) ? s.edge : 0, by: { [n]: s.share } });
|
|
170
|
+
});
|
|
171
|
+
pooled.sort((a, b) => b.share - a.share);
|
|
172
|
+
// Near-twins (OKLab distance under 0.05) are one colour, averaged by share.
|
|
173
|
+
const clusters = [];
|
|
174
|
+
for (const p of pooled) {
|
|
175
|
+
const twin = clusters.find((c) => distance(c.lab, p.lab) < 0.05);
|
|
176
|
+
if (!twin) {
|
|
177
|
+
clusters.push({ ...p, lab: [...p.lab], by: { ...p.by } });
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const w = twin.share + p.share;
|
|
181
|
+
twin.lab = twin.lab.map((v, i) => (v * twin.share + p.lab[i] * p.share) / w);
|
|
182
|
+
twin.edge = (twin.edge * twin.share + p.edge * p.share) / w;
|
|
183
|
+
twin.share = w;
|
|
184
|
+
for (const [n, s] of Object.entries(p.by)) twin.by[n] = (twin.by[n] || 0) + s;
|
|
185
|
+
}
|
|
186
|
+
for (const c of clusters) {
|
|
187
|
+
Object.assign(c, lchOfLab(c.lab), { hex: hexOf(lchOfLab(c.lab)) });
|
|
188
|
+
c.pictures = Object.values(c.by).filter((s) => s >= 2).length;
|
|
189
|
+
// A backdrop: mostly at the edges, or at the edges of the one picture it is in.
|
|
190
|
+
c.background = c.edge > 0.55 || (c.pictures <= 1 && c.edge > 0.4);
|
|
191
|
+
}
|
|
192
|
+
clusters.sort((a, b) => b.share - a.share);
|
|
193
|
+
const notes = [];
|
|
194
|
+
const out = clusters.filter((c) => excluded.some((x) => distance(labOf(x), c.lab) < 0.06));
|
|
195
|
+
if (excluded.length) notes.push(`excluded: ${out.length ? out.map((c) => c.hex).join(", ") : "nothing was near those colours"}`);
|
|
196
|
+
const counted = clusters.filter((c) => c.share >= 2 && c.L <= 0.97 && !out.includes(c));
|
|
197
|
+
const kept = counted.filter((c) => c.L >= 0.12);
|
|
198
|
+
const dropped = clusters.length - out.length - kept.length;
|
|
199
|
+
if (dropped) notes.push(`${dropped} of ${clusters.length} colours dropped as noise, highlights or black shadow`);
|
|
200
|
+
const backdrop = kept.filter((c) => c.background);
|
|
201
|
+
if (backdrop.length) notes.push(`background, never the signature or accent: ${backdrop.map((c) => c.hex).join(", ")}`);
|
|
202
|
+
if (!kept.length) fail("every colour was noise: try other pictures");
|
|
203
|
+
const subject = kept.filter((c) => !c.background);
|
|
204
|
+
// Colours found in two or more pictures first, each group heaviest first.
|
|
205
|
+
const preferred = (list) => [...list.filter((c) => c.pictures >= 2), ...list.filter((c) => c.pictures < 2)];
|
|
206
|
+
|
|
207
|
+
const from = (c) => ({ hex: c.hex, share: r2(c.share), pictures: c.pictures, edge: r2(c.edge), ...(c.background ? { background: true } : {}) });
|
|
208
|
+
const colour = (role, lch, source, tuned) => ({ role, hex: hexOf(lch), ...fit(lch), from: source ? from(source) : null, tuned });
|
|
209
|
+
|
|
210
|
+
let signature = preferred(subject).find((c) => c.C >= 0.05);
|
|
211
|
+
if (!signature) {
|
|
212
|
+
signature = preferred(subject)[0] || kept[0];
|
|
213
|
+
notes.push(subject.length ? "no subject colour has much chroma: the signature is the heaviest, grey as it is" : "every colour looks like background: the signature is the heaviest of them");
|
|
214
|
+
}
|
|
215
|
+
// A ground keeps its source's hue. A near-grey source (chroma under 0.02: a wall, a table) has no
|
|
216
|
+
// hue worth keeping, so it takes the signature's, and the ground reads as the subject's off-white.
|
|
217
|
+
// A background colour seeds a ground only when no subject colour can.
|
|
218
|
+
function ground(test, list, L, maxC, word) {
|
|
219
|
+
const source = preferred(list.filter((c) => !c.background)).find(test) || (subject.length ? null : list.find(test));
|
|
220
|
+
const own = source && source.C >= 0.02;
|
|
221
|
+
const h = own ? source.h : signature.h;
|
|
222
|
+
const C = own ? Math.min(source.C, maxC) : Math.min(signature.C, maxC, 0.015);
|
|
223
|
+
const said = source ? `${word}${source.background ? " from a background colour" : ""}, ${own ? "its hue kept" : "the signature's hue added"}${own && source.C > maxC ? ", chroma lowered" : ""}` : `the signature's hue ${word}`;
|
|
224
|
+
return colour("ground", { L, C, h }, source || signature, said);
|
|
225
|
+
}
|
|
226
|
+
const light = ground((c) => c.L >= 0.82, kept, 0.96, 0.02, "made an off-white");
|
|
227
|
+
const dark = ground((c) => c.L <= 0.35, counted, 0.17, 0.03, "made a near-black");
|
|
228
|
+
|
|
229
|
+
// Ink in the other ground's hue, moved darker (on light) or lighter (on dark) until it reads at the text level.
|
|
230
|
+
function ink(on, hueOf, startL, step) {
|
|
231
|
+
const C = Math.min(hueOf.C, step < 0 ? 0.03 : 0.02);
|
|
232
|
+
let L = startL;
|
|
233
|
+
while (contrast(hexOf({ L, C, h: hueOf.h }), on.hex) < textNeed && L > 0 && L < 1) L = Math.min(1, Math.max(0, L + step));
|
|
234
|
+
const moved = Math.abs(L - startL) > 0.001;
|
|
235
|
+
return colour("ink", { L, C, h: hueOf.h }, null, `the ${step < 0 ? "dark" : "light"} ground's hue at L ${r2(L)}${moved ? `, ${step < 0 ? "darkened" : "lightened"} to read ${textNeed}:1` : ""}`);
|
|
236
|
+
}
|
|
237
|
+
const darkInk = (on) => ink(on, dark, 0.2, -0.01);
|
|
238
|
+
const lightInk = (on) => ink(on, light, 0.95, 0.01);
|
|
239
|
+
|
|
240
|
+
// The accent: colourful, heavy, a hue apart from the ground, never background; chroma raised, at the accent level on the ground.
|
|
241
|
+
function accent(on, label) {
|
|
242
|
+
const score = (c) => c.C * Math.sqrt(c.share);
|
|
243
|
+
const fits = subject.filter((c) => c.C >= 0.08 && hueGap(c.h, on.h) >= 25);
|
|
244
|
+
const pick = [...fits.filter((c) => c.pictures >= 2).sort((a, b) => score(b) - score(a)), ...fits.filter((c) => c.pictures < 2).sort((a, b) => score(b) - score(a))][0];
|
|
245
|
+
const source = pick || signature;
|
|
246
|
+
const C = Math.max(source.C, 0.13);
|
|
247
|
+
const start = Math.min(0.72, Math.max(0.55, source.L));
|
|
248
|
+
const ok = (L) => contrast(hexOf({ L, C, h: source.h }), on.hex) >= accentNeed;
|
|
249
|
+
let L = null;
|
|
250
|
+
for (let d = 0; d <= 0.17 + 1e-9 && L === null; d += 0.005) for (const x of [start - d, start + d]) if (L === null && x >= 0.55 - 1e-9 && x <= 0.72 + 1e-9 && ok(x)) L = x;
|
|
251
|
+
if (L === null) {
|
|
252
|
+
notes.push(`${label}: no accent reaches ${accentNeed}:1 on this ground at lightness 0.55 to 0.72, so it has none`);
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
const how = [];
|
|
256
|
+
if (!pick) how.push("the signature (no subject colour stands off the ground's hue)");
|
|
257
|
+
if (C > source.C + 0.001) how.push("chroma raised to read as an accent");
|
|
258
|
+
if (Math.abs(L - start) > 0.001) how.push(`${L > start ? "lightened" : "darkened"} to reach ${accentNeed}:1 on the ground`);
|
|
259
|
+
else if (Math.abs(L - source.L) > 0.01) how.push(`${L > source.L ? "lightened" : "darkened"} into the accent range`);
|
|
260
|
+
const made = colour("accent", { L, C, h: source.h }, source, how.join(", ") || "as found");
|
|
261
|
+
// An accent of the ground's own hue makes a one-hue palette: fine to offer, but not a second colour.
|
|
262
|
+
made.ownHue = !pick;
|
|
263
|
+
return made;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const proposal = (name, g, i) => {
|
|
267
|
+
const a = accent(g, name);
|
|
268
|
+
const rows = [g, i, ...(a ? [a] : [])];
|
|
269
|
+
for (const r of rows) if (r.role !== "ground") r.contrast = cut2(contrast(r.hex, g.hex));
|
|
270
|
+
return { name, ground: g.hex, ink: i.hex, accent: a?.hex || null, ownHue: a ? a.ownHue : null, colours: rows };
|
|
271
|
+
};
|
|
272
|
+
const proposals = [proposal("light", light, darkInk(light)), proposal("dark", dark, lightInk(dark))];
|
|
273
|
+
|
|
274
|
+
// The signature itself as a ground, at a lightness where one of the inks reads.
|
|
275
|
+
{
|
|
276
|
+
const own = Math.min(0.62, Math.max(0.5, signature.L));
|
|
277
|
+
let best = null;
|
|
278
|
+
for (let d = 0; d <= 0.12 + 1e-9 && !best; d += 0.005) {
|
|
279
|
+
for (const L of [own - d, own + d]) {
|
|
280
|
+
if (best || L < 0.5 - 1e-9 || L > 0.62 + 1e-9) continue;
|
|
281
|
+
const g = colour("ground", { L, C: signature.C, h: signature.h }, signature, "");
|
|
282
|
+
const inks = [darkInk(light), lightInk(dark)].filter((i) => contrast(i.hex, g.hex) >= textNeed);
|
|
283
|
+
if (inks.length) best = { g, i: inks.sort((x, y) => contrast(y.hex, g.hex) - contrast(x.hex, g.hex))[0], L };
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (!best) notes.push("signature: neither ink reaches ${textNeed}:1 on the signature at lightness 0.5 to 0.62, so there is no signature ground");
|
|
287
|
+
else {
|
|
288
|
+
const { g, i, L } = best;
|
|
289
|
+
g.tuned = Math.abs(L - signature.L) <= 0.01 ? "as found" : `${L > signature.L ? "lightened" : "darkened"} to carry ${i.L > 0.5 ? "light" : "dark"} text`;
|
|
290
|
+
i.tuned = `the ${i.L > 0.5 ? "dark" : "light"} proposal's ink`;
|
|
291
|
+
proposals.push(proposal("signature", g, i));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// One or two grounds and an accent for the library: the light one, and the signature (else the dark).
|
|
296
|
+
const second = proposals.find((p) => p.name === "signature") || proposals.find((p) => p.name === "dark");
|
|
297
|
+
const grounds = [proposals[0], second].map((p) => ({ id: `subject-${p.name}`, ground: p.ground, panel: null, ink: p.ink, gradient: null, pattern: null, from: ["subject"] }));
|
|
298
|
+
// The library takes an accent only when the subject has a second hue of its own. One that is the
|
|
299
|
+
// signature with more chroma would make every subject screen one hue: then accents come from the
|
|
300
|
+
// references, and the one-hue palettes stay in the proposals for design-identity to offer.
|
|
301
|
+
const acc = proposals.find((p) => p.accent && !p.ownHue);
|
|
302
|
+
if (!acc && proposals.some((p) => p.accent)) notes.push("no second hue in the subject: the library takes no subject accent, so accents come from the references; the one-hue palettes are still proposed");
|
|
303
|
+
const library = { grounds, accents: acc ? [{ id: "subject-accent", colour: acc.accent, device: "the subject's own colour", from: ["subject"] }] : [] };
|
|
304
|
+
|
|
305
|
+
if (sheetArg) writeSheet(sheetArg);
|
|
306
|
+
if (json) {
|
|
307
|
+
const clean = (c) => ({ role: c.role, hex: c.hex, L: r3(c.L), C: r3(c.C), h: Math.round(c.h), from: c.from, tuned: c.tuned, ...(c.contrast ? { contrast: c.contrast } : {}) });
|
|
308
|
+
const listed = kept.map((c) => ({ ...from(c), L: r3(c.L), C: r3(c.C), h: Math.round(c.h), background: c.background }));
|
|
309
|
+
console.log(JSON.stringify({ level, signature: from(signature), colours: listed, proposals: proposals.map((p) => ({ ...p, colours: p.colours.map(clean) })), library, notes }, null, 1));
|
|
310
|
+
} else {
|
|
311
|
+
const source = (f) => (f ? `${f.hex} ${f.share}%, ${f.pictures} of ${pictures.length}${f.background ? ", background" : ""}` : "");
|
|
312
|
+
const rows = [["proposal", "role", "colour", "from (share, pictures)", "how", "contrast"]];
|
|
313
|
+
for (const p of proposals) for (const c of p.colours) rows.push([p.name, c.role, c.hex, source(c.from), c.tuned, c.contrast ? `${c.contrast}:1` : ""]);
|
|
314
|
+
const widths = rows[0].map((_, i) => Math.max(...rows.map((r) => r[i].length)));
|
|
315
|
+
console.log(`signature: ${signature.hex} (${r2(signature.share)}% of the pictures, in ${signature.pictures} of ${pictures.length})`);
|
|
316
|
+
for (const r of rows) console.log(r.map((v, i) => v.padEnd(widths[i])).join(" ").trimEnd());
|
|
317
|
+
for (const n of notes) console.log(`note: ${n}`);
|
|
318
|
+
if (sheetArg) console.log(`sheet: ${sheetArg}`);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// The sheet: each colour, and where its source lives in every picture, worked out in the page.
|
|
322
|
+
function writeSheet(path) {
|
|
323
|
+
const pics = pictures.map((p) => {
|
|
324
|
+
const at = [resolve(p.target), resolve(base, p.target)].find((x) => existsSync(x)) || resolve(p.target);
|
|
325
|
+
const type = { ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".webp": "image/webp", ".gif": "image/gif", ".avif": "image/avif" }[extname(at).toLowerCase()];
|
|
326
|
+
const url = existsSync(at) && type ? `data:${type};base64,${readFileSync(at).toString("base64")}` : pathToFileURL(at).href;
|
|
327
|
+
return { name: at.split(/[\\/]/).pop(), url };
|
|
328
|
+
});
|
|
329
|
+
const data = { pictures: pics, proposals: proposals.map((p) => ({ name: p.name, colours: p.colours.map((c) => ({ role: c.role, hex: c.hex, from: c.from, tuned: c.tuned, contrast: c.contrast || null })) })), notes, signature: from(signature) };
|
|
330
|
+
const esc = (s) => JSON.stringify(s).replace(/</g, "\\u003c");
|
|
331
|
+
writeFileSync(
|
|
332
|
+
path,
|
|
333
|
+
`<!doctype html>
|
|
334
|
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
|
335
|
+
<title>Subject colours</title>
|
|
336
|
+
<style>
|
|
337
|
+
:root { --bg: #f6f5f2; --fg: #1d1c1a; --soft: #6b6862; --line: #dcd9d2; }
|
|
338
|
+
body { margin: 0; padding: 24px 16px; background: var(--bg); color: var(--fg); font: 14px/1.4 system-ui, sans-serif; }
|
|
339
|
+
h1 { font-size: 20px; margin: 0 0 4px; } h2 { font-size: 16px; margin: 32px 0 8px; text-transform: capitalize; }
|
|
340
|
+
.note { color: var(--soft); margin: 2px 0; }
|
|
341
|
+
.row { display: grid; grid-template-columns: 220px 1fr; gap: 16px; padding: 12px 0; border-top: 1px solid var(--line); align-items: start; }
|
|
342
|
+
.chip { height: 120px; border-radius: 6px; border: 1px solid var(--line); display: flex; align-items: end; padding: 8px; box-sizing: border-box; font-weight: 600; }
|
|
343
|
+
.meta { margin-top: 6px; } .meta span { display: block; color: var(--soft); }
|
|
344
|
+
.from { display: inline-block; width: 12px; height: 12px; border-radius: 2px; vertical-align: -2px; border: 1px solid var(--line); }
|
|
345
|
+
.pics { display: flex; flex-wrap: wrap; gap: 8px; } canvas { height: 150px; width: auto; border-radius: 4px; background: #000; }
|
|
346
|
+
@media (max-width: 600px) { .row { grid-template-columns: 1fr; } canvas { height: 100px; } }
|
|
347
|
+
</style></head><body>
|
|
348
|
+
<h1>Subject colours</h1>
|
|
349
|
+
<p class="note">Lit: the pixels near each colour's source (OKLab distance under 0.08). Dimmed: the rest. A source lit mostly outside the subject is background: run again with --exclude.</p>
|
|
350
|
+
<div id="notes"></div><div id="out"></div>
|
|
351
|
+
<script>
|
|
352
|
+
const DATA = ${esc(data)};
|
|
353
|
+
const lin = (c) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);
|
|
354
|
+
const LIN = Array.from({ length: 256 }, (_, i) => lin(i / 255));
|
|
355
|
+
function lab(r, g, b) {
|
|
356
|
+
r = LIN[r]; g = LIN[g]; b = LIN[b];
|
|
357
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b), m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b), s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
358
|
+
return [0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s, 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s, 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s];
|
|
359
|
+
}
|
|
360
|
+
const hexLab = (h) => lab(...[1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16)));
|
|
361
|
+
const el = (tag, props = {}, ...kids) => { const e = Object.assign(document.createElement(tag), props); e.append(...kids); return e; };
|
|
362
|
+
for (const n of DATA.notes) document.getElementById("notes").append(el("p", { className: "note", textContent: "Note: " + n }));
|
|
363
|
+
const images = DATA.pictures.map((p) => new Promise((done) => { const img = new Image(); img.onload = () => done(img); img.onerror = () => done(null); img.src = p.url; }));
|
|
364
|
+
Promise.all(images).then((imgs) => {
|
|
365
|
+
const pixels = imgs.map((img) => {
|
|
366
|
+
if (!img) return null;
|
|
367
|
+
const k = Math.min(1, 320 / Math.max(img.naturalWidth, img.naturalHeight));
|
|
368
|
+
const w = Math.round(img.naturalWidth * k), h = Math.round(img.naturalHeight * k);
|
|
369
|
+
const c = Object.assign(document.createElement("canvas"), { width: w, height: h }).getContext("2d");
|
|
370
|
+
c.drawImage(img, 0, 0, w, h);
|
|
371
|
+
try { const d = c.getImageData(0, 0, w, h); const L = new Float32Array(w * h * 3); for (let i = 0, j = 0; i < d.data.length; i += 4, j += 3) L.set(lab(d.data[i], d.data[i + 1], d.data[i + 2]), j); return { w, h, d, L }; }
|
|
372
|
+
catch { return { w, h, img, blocked: true }; }
|
|
373
|
+
});
|
|
374
|
+
const out = document.getElementById("out");
|
|
375
|
+
for (const p of DATA.proposals) {
|
|
376
|
+
out.append(el("h2", { textContent: p.name }));
|
|
377
|
+
const ground = p.colours.find((c) => c.role === "ground");
|
|
378
|
+
for (const c of p.colours) {
|
|
379
|
+
const chip = el("div", { className: "chip", textContent: c.role + " " + c.hex });
|
|
380
|
+
chip.style.background = c.hex;
|
|
381
|
+
chip.style.color = c.role === "ground" ? p.colours.find((x) => x.role === "ink").hex : ground.hex;
|
|
382
|
+
const f = c.from;
|
|
383
|
+
const fromLine = f ? el("span", {}, el("i", { className: "from", style: "background:" + f.hex }), " from " + f.hex + ", " + f.share + "% in " + f.pictures + " of " + DATA.pictures.length + (f.background ? ", background" : "") + ", edge " + f.edge) : el("span", { textContent: "derived, no source colour" });
|
|
384
|
+
const meta = el("div", { className: "meta" }, fromLine, el("span", { textContent: c.tuned + (c.contrast ? " · " + c.contrast + ":1" : "") }));
|
|
385
|
+
const pics = el("div", { className: "pics" });
|
|
386
|
+
if (f) {
|
|
387
|
+
const target = hexLab(f.hex);
|
|
388
|
+
pixels.forEach((px, n) => {
|
|
389
|
+
if (!px) { pics.append(el("span", { className: "note", textContent: DATA.pictures[n].name + ": not found" })); return; }
|
|
390
|
+
const cv = el("canvas", { width: px.w, height: px.h, title: DATA.pictures[n].name });
|
|
391
|
+
const ctx = cv.getContext("2d");
|
|
392
|
+
if (px.blocked) { ctx.drawImage(px.img, 0, 0, px.w, px.h); pics.append(cv, el("span", { className: "note", textContent: "the browser will not let this page read the picture's pixels" })); return; }
|
|
393
|
+
const o = new ImageData(new Uint8ClampedArray(px.d.data), px.w, px.h);
|
|
394
|
+
for (let i = 0, j = 0; i < o.data.length; i += 4, j += 3) {
|
|
395
|
+
const dist = Math.hypot(px.L[j] - target[0], px.L[j + 1] - target[1], px.L[j + 2] - target[2]);
|
|
396
|
+
if (dist >= 0.08) { const g = 0.3 * o.data[i] + 0.59 * o.data[i + 1] + 0.11 * o.data[i + 2]; o.data[i] = o.data[i + 1] = o.data[i + 2] = 20 + g * 0.18; }
|
|
397
|
+
}
|
|
398
|
+
ctx.putImageData(o, 0, 0);
|
|
399
|
+
pics.append(cv);
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
out.append(el("div", { className: "row" }, el("div", {}, chip, meta), pics));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
document.body.dataset.ready = "1";
|
|
406
|
+
});
|
|
407
|
+
</script></body></html>
|
|
408
|
+
`,
|
|
409
|
+
);
|
|
410
|
+
}
|