-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial-swatch-playground.html
More file actions
341 lines (313 loc) · 17.1 KB
/
Copy pathmaterial-swatch-playground.html
File metadata and controls
341 lines (313 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>TigerSystem — the material swatch, reference renderer</title>
<!--
The reference renderer for the TigerSystem material-swatch convention.
Spec: ./material-swatch.md — that document is normative, this page shows it.
Self-contained on purpose: no build, no server, no network. Open the file.
The implementation below is the convention expressed as code — port it, or
check your own port against the CSS it prints for each case.
-->
<style>
:root {
--bg:#f6f7fb; --surface:#fff; --surface-2:#f1f3f9; --border:#e3e6ef;
--text:#1c2030; --muted:#6b7287; --accent:#ff7a18;
--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
:root { --bg:#12141c; --surface:#191c26; --surface-2:#1f2330; --border:#2b3040;
--text:#e6e8f0; --muted:#98a0b5; }
}
* { box-sizing: border-box; }
body { margin:0; padding:26px 24px 80px; background:var(--bg); color:var(--text);
font:14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
h1 { font-size:20px; margin:0 0 6px; }
.intro { color:var(--muted); font-size:13px; max-width:820px; margin:0 0 14px; }
.intro code, code { font-family:var(--mono); font-size:12px; color:var(--accent); }
.bar { display:flex; flex-wrap:wrap; gap:8px; align-items:center; margin-bottom:20px; }
.bar button { height:28px; padding:0 12px; font-size:12px; border-radius:99px;
border:1px solid var(--border); background:var(--surface); color:var(--muted); cursor:pointer; }
.bar button:hover { border-color:var(--accent); color:var(--accent); }
.case { border:1px solid var(--border); border-radius:12px; background:var(--surface);
margin-bottom:14px; overflow:hidden; }
.head { padding:12px 16px; background:var(--surface-2); }
.title { font-weight:700; font-size:14px; }
.rule { font-size:11.5px; color:var(--muted); margin-left:8px; }
.css { font-family:var(--mono); font-size:11.5px; color:#2f9e68; margin-top:6px; word-break:break-all; }
@media (prefers-color-scheme: dark) { .css { color:#7fd1a8; } }
.verdict { font-size:11.5px; color:var(--muted); margin-top:4px; }
.verdict b { color:var(--text); }
.note { font-size:12px; color:var(--muted); margin-top:6px; }
.pickers { display:flex; flex-wrap:wrap; align-items:center; gap:8px; margin-top:10px; }
.pick { display:inline-flex; align-items:center; gap:6px; }
.pick input { width:30px; height:30px; padding:0; border:1px solid var(--border);
border-radius:7px; background:none; cursor:pointer; }
.pick input::-webkit-color-swatch-wrapper { padding:2px; }
.pick input::-webkit-color-swatch { border:none; border-radius:5px; }
.pick span { font-family:var(--mono); font-size:10.5px; color:var(--muted); }
.pick.unused { opacity:.38; }
.pick.unused span::after { content:" · unused"; }
.pickers button { height:26px; padding:0 9px; font-size:11px; border:1px solid var(--border);
border-radius:7px; background:var(--surface); color:var(--muted); cursor:pointer; }
.pickers .field { font-family:var(--mono); font-size:10.5px; color:var(--muted); opacity:.75; }
/* The BOX SHAPES. The convention has to hold on every one of them — that is
the whole point of an angular geometry: a circle, a square, a wide banner,
a narrow column and a partially-filled bar must all show the same picture. */
.boxes { display:flex; flex-wrap:wrap; align-items:flex-end; gap:20px; padding:18px 16px; }
figure { margin:0; display:flex; flex-direction:column; align-items:center; gap:8px; }
figcaption { font-size:10.5px; color:var(--muted); text-align:center; }
.b-circle { width:78px; height:78px; border-radius:50%; }
.b-square { width:110px; height:110px; border-radius:10px; }
.b-wide { width:180px; height:70px; border-radius:10px; }
.b-tall { width:60px; height:130px; border-radius:10px; }
.b-clip { width:70px; height:130px; border-radius:10px; border:1px solid var(--border);
position:relative; overflow:hidden; background:var(--surface-2); }
.b-clip > i { position:absolute; left:0; right:0; bottom:0; height:60%; display:block; }
</style>
</head>
<body>
<h1>The material swatch — reference renderer</h1>
<p class="intro">
The <a href="./material-swatch.md">TigerSystem material-swatch convention</a>, running.
Every case of the decision ladder, on every box shape a real interface uses —
because the rule has to survive a circle, a square, a banner and a clipped
fill bar alike. <b>Pick a colour and watch all five follow.</b> The green line
is the exact CSS produced; check your own implementation against it.
</p>
<div class="bar"><button type="button" id="resetAll">Reset every case</button></div>
<div id="out"></div>
<script>
/* ═══════════════════════════════════════════════════════════════════════════
THE SWATCH CONVENTION, AS CODE. This is the normative behaviour of
./material-swatch.md — keep the two in step.
═══════════════════════════════════════════════════════════════════════════ */
// N colours, hard edges → N equal conic sectors, first colour at 12 o'clock,
// clockwise. With N = 2 the boundary lands on the vertical axis, which is why
// bicolor is a guaranteed vertical split and not a special case.
const pieSplit = colors => {
const step = 360 / colors.length;
return `conic-gradient(${colors.map((c, i) => `${c} ${i * step}deg ${(i + 1) * step}deg`).join(', ')})`;
};
// One angle for every smooth ramp in the system.
const RAMP_ANGLE = '135deg';
// Aspect ids that change the shape. Match on the ID, never on the label.
const ASPECT = { BICOLOR: 252, TRICOLOR: 24, RAINBOW: 145 };
function normalize(c) {
const s = String(c || '').trim().replace(/^#/, '');
const hex6 = s.length === 8 ? s.slice(0, 6) : s;
return /^[0-9a-fA-F]{6}$/.test(hex6) ? `#${hex6}` : null;
}
const slotHex = (r, g, b) =>
[r, g, b].every(v => typeof v === 'number')
? `#${[r, g, b].map(v => Math.max(0, Math.min(255, v | 0)).toString(16).padStart(2, '0')).join('')}`
: null;
function colorBg(doc) {
const aspects = [doc.id_aspect1, doc.id_aspect2];
const isRainbow = aspects.includes(ASPECT.RAINBOW);
const isTricolor = aspects.includes(ASPECT.TRICOLOR);
const isBicolor = aspects.includes(ASPECT.BICOLOR);
const list = (doc.online_color_list || []).map(normalize).filter(Boolean);
const type = doc.online_color_type || '';
const slots = [
slotHex(doc.color_r, doc.color_g, doc.color_b),
slotHex(doc.color_r2, doc.color_g2, doc.color_b2),
slotHex(doc.color_r3, doc.color_g3, doc.color_b3),
].filter(Boolean);
if (list.length >= 2 && type === 'conic_gradient') // 1
return `conic-gradient(from 0deg, ${list.join(', ')}, ${list[0]})`;
if (list.length >= 2 && type === 'gradient') // 2
return `linear-gradient(${RAMP_ANGLE}, ${list.join(', ')})`;
if (list.length >= 2) return pieSplit(list); // 3
if (list.length === 1) return list[0]; // 4
if (isRainbow && isTricolor) { // 5
const [c1 = '#ff4d4d', c2 = '#ffd93d', c3 = '#4da3ff'] = list;
return `linear-gradient(${RAMP_ANGLE}, ${c1} 0%, ${c2} 50%, ${c3} 100%)`;
}
if (isRainbow && isBicolor) { // 6
const [c1 = '#ff7a00', c2 = '#8a2be2'] = list;
return `linear-gradient(${RAMP_ANGLE}, ${c1} 0%, ${c2} 100%)`;
}
if (isRainbow) { // 7
if (slots.length >= 2) return `linear-gradient(${RAMP_ANGLE}, ${slots.join(', ')})`;
if (slots.length === 1) return slots[0];
return `linear-gradient(${RAMP_ANGLE}, #ff0000, #ff8800, #ffff00, #00cc00, #0000ff, #8b00ff)`;
}
if (isTricolor) { // 8
const [c1 = '#cccccc', c2 = '#888888', c3] = slots;
return pieSplit([c1, c2, c3 || c1]);
}
if (isBicolor) { // 9
const [c1 = '#cccccc', c2 = '#ffffff'] = slots;
return pieSplit([c1, c2]); // → vertical split
}
return slots[0] || '#1c2030'; // 10
}
// Which logo file goes on top. 8 digits FIRST: with {6} tried first, an
// #RRGGBBAA matches its first six digits, fails the trailing \b, and the whole
// match is lost — the colour then reads as "light" and a black logo lands on a
// black spool.
function isColorDark(bg) {
const m = String(bg).match(/#([0-9a-fA-F]{8}|[0-9a-fA-F]{6}|[0-9a-fA-F]{4}|[0-9a-fA-F]{3})\b/);
if (!m) return false;
let h = m[1];
if (h.length === 8) h = h.slice(0, 6);
else if (h.length === 4) h = h.slice(0, 3);
if (h.length === 3) h = h.split('').map(c => c + c).join('');
const [r, g, b] = [0, 2, 4].map(i => parseInt(h.slice(i, i + 2), 16));
return (0.299 * r + 0.587 * g + 0.114 * b) / 255 < 0.5;
}
function luminance(bg) {
const m = String(bg).match(/#([0-9a-fA-F]{8}|[0-9a-fA-F]{6}|[0-9a-fA-F]{4}|[0-9a-fA-F]{3})\b/);
if (!m) return null;
let h = m[1];
if (h.length === 8) h = h.slice(0, 6);
else if (h.length === 4) h = h.slice(0, 3);
if (h.length === 3) h = h.split('').map(c => c + c).join('');
const [r, g, b] = [0, 2, 4].map(i => parseInt(h.slice(i, i + 2), 16));
return (0.299 * r + 0.587 * g + 0.114 * b) / 255;
}
/* ═══════════════════════════ cases & rendering ═══════════════════════════ */
const CASES = [
{ t:'Mono — one colour in the list', rule:'rule 4',
doc:{ online_color_list:['FF5722'] } },
{ t:'Mono — chip slot only', rule:'rule 10',
doc:{ color_r:36, color_g:99, color_b:224 } },
{ t:'Black catalogue product — #000000FF', rule:'rule 4, 8-digit normalised',
note:'The luminance trap: an unnormalised #000000FF read as "light" puts the black logo on a black spool.',
doc:{ online_color_list:['000000FF'] } },
{ t:'Bicolor — chip aspect 252', rule:'rule 9 — 2 sectors ⇒ vertical split',
note:'Look at all five boxes: the boundary stays vertical on the circle, the square, the banner, the column and the clipped bar. That is what the angular geometry buys.',
doc:{ color_r:224, color_g:36, color_b:36, color_r2:36, color_g2:99, color_b2:224,
color_r3:0, color_g3:0, color_b3:0, id_aspect2:ASPECT.BICOLOR } },
{ t:'Tricolor — chip aspect 24', rule:'rule 8 — 3 sectors',
doc:{ color_r:224, color_g:36, color_b:36, color_r2:36, color_g2:99, color_b2:224,
color_r3:34, color_g3:160, color_b3:107, id_aspect2:ASPECT.TRICOLOR } },
{ t:'Two colours in the list', rule:'rule 3',
doc:{ online_color_list:['e02424','2463e0'] } },
{ t:'Four colours in the list (multi)', rule:'rule 3',
doc:{ online_color_list:['e02424','f0a020','2463e0','22a06b'], online_color_type:'multi' } },
{ t:'Declared type: gradient', rule:'rule 2 — ramp 135°',
doc:{ online_color_list:['e02424','f0a020','2463e0'], online_color_type:'gradient' } },
{ t:'Declared type: gradient, TWO colours', rule:'rule 2 — still a ramp',
note:'Deliberately NOT the vertical split: the catalogue asked for a ramp.',
doc:{ online_color_list:['e02424','2463e0'], online_color_type:'gradient' } },
{ t:'Declared type: conic_gradient', rule:'rule 1 — catalogue only, never on a chip',
doc:{ online_color_list:['e02424','f0a020','22a06b','2463e0'], online_color_type:'conic_gradient' } },
{ t:'Rainbow — aspect 145, no colours', rule:'rule 7 — 6-stop default',
doc:{ id_aspect1:ASPECT.RAINBOW } },
{ t:'Rainbow — aspect 145 + chip slots', rule:'rule 7',
doc:{ color_r:224, color_g:36, color_b:36, color_r2:240, color_g2:160, color_b2:32,
color_r3:36, color_g3:99, color_b3:224, id_aspect1:ASPECT.RAINBOW } },
{ t:'Rainbow + Bicolor', rule:'rule 6 — ramp, not a split',
doc:{ id_aspect1:ASPECT.RAINBOW, id_aspect2:ASPECT.BICOLOR } },
{ t:'Rainbow + Tricolor', rule:'rule 5',
doc:{ id_aspect1:ASPECT.RAINBOW, id_aspect2:ASPECT.TRICOLOR } },
{ t:'No colour at all', rule:'rule 10 — last resort',
doc:{} },
];
const BOXES = [
{ cls:'b-circle', cap:'Circle<br>swatch' },
{ cls:'b-square', cap:'Square<br>tile / card' },
{ cls:'b-wide', cap:'Wide<br>banner' },
{ cls:'b-tall', cap:'Narrow<br>column' },
{ cls:'b-clip', cap:'Clipped<br>fill bar · 60 %', clip:true },
];
// Where this case's editable colours live — the list, or the chip slots.
const kindOf = c => (c.doc.online_color_list || []).length ? 'list'
: (typeof c.doc.color_r === 'number' ? 'slots' : null);
function colorsOf(c) {
const k = kindOf(c);
if (k === 'list') return c.doc.online_color_list.map(h => normalize(h) || '#888888');
if (k === 'slots') return [
slotHex(c.doc.color_r, c.doc.color_g, c.doc.color_b),
slotHex(c.doc.color_r2, c.doc.color_g2, c.doc.color_b2),
slotHex(c.doc.color_r3, c.doc.color_g3, c.doc.color_b3),
].filter(Boolean);
return [];
}
function applyColors(c, colors) {
const k = kindOf(c);
if (k === 'list') { c.doc = { ...c.doc, online_color_list: colors.map(h => h.replace(/^#/, '')) }; return; }
if (k !== 'slots') return;
const rgb = h => [1, 3, 5].map(i => parseInt(h.slice(i, i + 2), 16));
const d = { ...c.doc };
colors.forEach((h, i) => {
const [r, g, b] = rgb(h), s = i === 0 ? '' : String(i + 1);
d['color_r' + s] = r; d['color_g' + s] = g; d['color_b' + s] = b;
});
c.doc = d;
}
const boxesHTML = c => {
const bg = colorBg(c.doc);
return BOXES.map(b => `<figure>
${b.clip ? `<div class="${b.cls}"><i style="background:${bg}"></i></div>`
: `<div class="${b.cls}" style="background:${bg}"></div>`}
<figcaption>${b.cap}</figcaption>
</figure>`).join('');
};
function headHTML(c, i) {
const bg = colorBg(c.doc), lum = luminance(bg), colors = colorsOf(c);
const verdict = lum == null
? 'No flat colour to measure — the watermark falls back to the light rule.'
: `luminance <b>${lum.toFixed(3)}</b> → <b>${isColorDark(bg) ? 'dark' : 'light'}</b> background → the <b>${isColorDark(bg) ? 'white' : 'contoured black'}</b> logo`;
const k = kindOf(c);
return `<div class="head">
<span class="title">${c.t}</span><span class="rule">${c.rule}</span>
<div class="css">${bg}</div>
<div class="verdict">${verdict}</div>
${c.note ? `<div class="note">${c.note}</div>` : ''}
${k ? `<div class="pickers">
${colors.map((h, j) => `<label class="pick${bg.toLowerCase().includes(h.toLowerCase()) ? '' : ' unused'}">
<input type="color" value="${h}" data-case="${i}" data-idx="${j}" />
<span>${h.toUpperCase()}</span></label>`).join('')}
<button type="button" data-reset="${i}">Reset</button>
<span class="field">${k === 'list' ? 'online_color_list' : 'color_r/g/b (+2, +3)'}</span>
</div>` : ''}
</div>`;
}
const out = document.getElementById('out');
const PRISTINE = CASES.map(c => JSON.parse(JSON.stringify(c.doc)));
out.innerHTML = CASES.map((c, i) =>
`<section class="case" id="c${i}">${headHTML(c, i)}<div class="boxes">${boxesHTML(c)}</div></section>`).join('');
// Patch, never rebuild: the native colour picker is anchored to the live
// <input> and fires `input` continuously while the spectrum is dragged, so
// replacing that input tears the picker down mid-gesture.
function repaint(i) {
const c = CASES[i], sec = document.getElementById('c' + i), bg = colorBg(c.doc), lum = luminance(bg);
sec.querySelector('.css').textContent = bg;
sec.querySelector('.verdict').innerHTML = lum == null
? 'No flat colour to measure — the watermark falls back to the light rule.'
: `luminance <b>${lum.toFixed(3)}</b> → <b>${isColorDark(bg) ? 'dark' : 'light'}</b> background → the <b>${isColorDark(bg) ? 'white' : 'contoured black'}</b> logo`;
sec.querySelector('.boxes').innerHTML = boxesHTML(c);
const colors = colorsOf(c), low = bg.toLowerCase();
sec.querySelectorAll('.pick').forEach((el, j) => {
const hex = colors[j] || '';
el.querySelector('span').textContent = hex.toUpperCase();
el.classList.toggle('unused', !low.includes(hex.toLowerCase()));
});
}
out.addEventListener('input', e => {
const inp = e.target.closest('input[type="color"]');
if (!inp) return;
const i = +inp.dataset.case, colors = colorsOf(CASES[i]);
colors[+inp.dataset.idx] = inp.value;
applyColors(CASES[i], colors);
repaint(i);
});
function reset(i) {
CASES[i].doc = JSON.parse(JSON.stringify(PRISTINE[i]));
const colors = colorsOf(CASES[i]);
document.querySelectorAll(`#c${i} input[type="color"]`).forEach((inp, j) => { if (colors[j]) inp.value = colors[j]; });
repaint(i);
}
out.addEventListener('click', e => {
const b = e.target.closest('button[data-reset]');
if (b) reset(+b.dataset.reset);
});
document.getElementById('resetAll').addEventListener('click', () => CASES.forEach((_, i) => reset(i)));
</script>
</body>
</html>