What does Design-Token Algebra conclude, in numbers, about “Token Algebra: Three Layers, Themes as Functions and the Dependency Graph of Thirty Sites”?
Answer: p20 starts by counting the set as a graph. Of 86 tokens, 38 are primitives, and the semantic and component layers have 24 each. There are 48 alias edges: 20 from semantic to primitive, 23 from component to semantic, 4 from semantic to semantic and 1 from component to component. There are 38 literal values, all of them in the primitive layer. That is the single most important invariant of the whole structure, and the audits later in the chapter check it again and again.
Scope of the numbers: All brands, sites, colours and ratings are invented, every calculation comes from the book's programs, and legal matters are described only in general terms, not as legal advice. Each hypothesis states what would refute it.
Data table
The numbers below are printed verbatim by the chapter's Python program and can be reproduced by running it. standard-library Python onlyprinted values that match the answer: 20, 23, 24, 38, 48, 86
tokens per layer: ref 38 sys 24 comp 24 total 86 alias edges: 48 (comp->comp 1, comp->sys 23, sys->ref 20, sys->sys 4) literal values: 38, all in layer ['ref'] alias hops to a literal (sys + comp): 1 hop(s) 20, 2 hop(s) 24, 3 hop(s) 4 deepest chain: comp.button.ghost.fg comp.button.ghost.fg -> sys.color.link -> sys.color.primary -> ref.color.wine.700 = #8C2F39 most-referenced tokens: sys.color.border 3, ref.color.white 2, ref.color.ink.900 2, sys.color.primary 2 primitives never reached by any of the 8 themes: 8 ref.color.gold.700, ref.radius.3, ref.size.1, ref.size.4 ref.space.1, ref.space.3, ref.space.5, ref.space.8 resolved component table: 30 sites x 2 modes x 24 component tokens = 1440 cells distinct resolved component tables: 8 (= 4 families x 2 modes) hand-maintained entries if every site kept its own table: 1440 ; with the algebra: 102 of which category overrides: 6 entries sites per family: boutique 8, report 8, course 7, card 7 resolution is idempotent (resolving a resolved table changes nothing): True
Show the full program
# Chapter 2. p20: The fictional Hengyu token set as a graph: layers, alias edges, depth, resolved table.
# Three layers: ref (primitive literals), sys (semantic; colour roles are filled in by a theme), comp
# (component). Alias syntax follows the W3C Design Tokens Community Group draft: a value "{a.b.c}" points
# at another token. The 30 fictional sites of chapter 1 (B01-B08, R01-R08, C01-C07, K01-K07), two modes each.
from collections import Counter
import re
ALIAS = re.compile(r"^\{([a-z0-9.\-]+)\}$")
REF = {
"ref.color.ink.950": "#111114", "ref.color.ink.900": "#1B1B20", "ref.color.ink.700": "#3A3A42",
"ref.color.ink.500": "#6B6B76", "ref.color.ink.300": "#A4A4AD", "ref.color.ink.100": "#E4E2DC",
"ref.color.ink.050": "#F7F5F0", "ref.color.white": "#FFFFFF",
"ref.color.gold.700": "#7A5E2A", "ref.color.gold.500": "#B8913F", "ref.color.gold.300": "#DDC58E",
"ref.color.wine.700": "#8C2F39", "ref.color.wine.300": "#EA8F92",
"ref.color.cobalt.700": "#1F5FA8", "ref.color.cobalt.300": "#83B5F3",
"ref.color.jade.600": "#2E7D5B", "ref.color.jade.300": "#4EC894",
"ref.color.violet.700": "#6B4FA0", "ref.color.violet.300": "#B7ABD8",
"ref.color.red.600": "#C33A32", "ref.color.red.400": "#F07A72",
"ref.space.1": "4px", "ref.space.2": "8px", "ref.space.3": "12px", "ref.space.4": "16px",
"ref.space.5": "24px", "ref.space.6": "32px", "ref.space.7": "48px", "ref.space.8": "64px",
"ref.radius.1": "4px", "ref.radius.2": "8px", "ref.radius.3": "16px",
"ref.size.1": "12px", "ref.size.2": "14px", "ref.size.3": "16px", "ref.size.4": "20px",
"ref.size.5": "24px", "ref.size.6": "32px",
}
SYS_SHARED = {
"sys.color.link": "{sys.color.primary}", "sys.color.focus-ring": "{sys.color.accent}",
"sys.space.inset-sm": "{ref.space.2}", "sys.space.inset-md": "{ref.space.4}",
"sys.space.inset-lg": "{ref.space.6}", "sys.space.stack-sm": "{sys.space.inset-sm}",
"sys.space.stack-md": "{sys.space.inset-md}", "sys.space.section": "{ref.space.7}",
"sys.radius.control": "{ref.radius.1}", "sys.radius.card": "{ref.radius.2}",
"sys.size.small": "{ref.size.2}", "sys.size.body": "{ref.size.3}",
"sys.size.heading": "{ref.size.5}", "sys.size.display": "{ref.size.6}",
}
BASE = {
"light": {"canvas": "white", "surface": "ink.050", "text-body": "ink.900", "text-muted": "ink.500",
"border": "ink.100", "primary": "wine.700", "on-primary": "white", "accent": "gold.500",
"on-accent": "ink.900", "danger": "red.600"},
"dark": {"canvas": "ink.950", "surface": "ink.900", "text-body": "ink.050", "text-muted": "ink.300",
"border": "ink.700", "primary": "wine.300", "on-primary": "ink.950", "accent": "gold.300",
"on-accent": "ink.950", "danger": "red.400"},
}
FAMILY_PRIMARY = {"boutique": ("wine.700", "wine.300"), "report": ("cobalt.700", "cobalt.300"),
"course": ("jade.600", "jade.300"), "card": ("violet.700", "violet.300")}
COMP = {
"comp.button.primary.bg": "{sys.color.primary}", "comp.button.primary.fg": "{sys.color.on-primary}",
"comp.button.primary.radius": "{sys.radius.control}", "comp.button.primary.pad": "{sys.space.inset-md}",
"comp.button.ghost.fg": "{sys.color.link}", "comp.button.ghost.border": "{sys.color.border}",
"comp.card.bg": "{sys.color.surface}", "comp.card.border": "{sys.color.border}",
"comp.card.radius": "{sys.radius.card}", "comp.card.pad": "{sys.space.inset-lg}",
"comp.price.fg": "{sys.color.text-body}", "comp.price.size": "{sys.size.heading}",
"comp.price.strike-fg": "{sys.color.text-muted}", "comp.badge.bg": "{sys.color.accent}",
"comp.badge.fg": "{sys.color.on-accent}", "comp.input.bg": "{sys.color.surface}",
"comp.input.border": "{sys.color.border}", "comp.input.focus": "{sys.color.focus-ring}",
"comp.input.error": "{sys.color.danger}", "comp.nav.bg": "{sys.color.canvas}",
"comp.nav.fg": "{sys.color.text-body}", "comp.footer.fg": "{sys.color.text-muted}",
"comp.chart.mark": "{comp.button.primary.bg}", "comp.chart.gap": "{sys.space.stack-sm}",
}
SITES = ([("B%02d" % i, "boutique") for i in range(1, 9)] + [("R%02d" % i, "report") for i in range(1, 9)]
+ [("C%02d" % i, "course") for i in range(1, 8)] + [("K%02d" % i, "card") for i in range(1, 8)])
MODES = ("light", "dark")
def override(cat, mode):
"""Category override as a partial map; boutique's is empty because the base already uses its primary."""
light, dark = FAMILY_PRIMARY[cat]
p = light if mode == "light" else dark
return {} if p == BASE[mode]["primary"] else {"primary": p}
def merge(*maps):
"""Right-biased merge: later maps win. The empty map is the identity."""
out = {}
for m in maps:
out.update(m)
return out
def theme(cat, mode):
return merge(BASE[mode], override(cat, mode))
def token_set(cat, mode, local=None):
"""Flat token table for one site-mode: name -> literal or '{alias}'."""
t = dict(REF)
t.update({"sys.color." + k: "{ref.color.%s}" % v for k, v in theme(cat, mode).items()})
t.update(SYS_SHARED)
t.update(COMP)
t.update(local or {})
return t
def resolve(t, name, stack=()):
"""Follow aliases to a literal. Raises KeyError on a broken link, ValueError on a cycle."""
if name in stack:
raise ValueError(" -> ".join(stack + (name,)))
v = t[name]
m = ALIAS.match(v)
if not m:
return v, len(stack)
return resolve(t, m.group(1), stack + (name,))
def layer(name):
return name.split(".", 1)[0]
def main():
t = token_set("boutique", "light")
by_layer = Counter(layer(n) for n in t)
print("tokens per layer: ref %d sys %d comp %d total %d"
% (by_layer["ref"], by_layer["sys"], by_layer["comp"], len(t)))
edges = [(n, ALIAS.match(v).group(1)) for n, v in t.items() if ALIAS.match(v)]
kinds = Counter("%s->%s" % (layer(a), layer(b)) for a, b in edges)
print("alias edges: %d (%s)" % (len(edges), ", ".join("%s %d" % kv for kv in sorted(kinds.items()))))
literals = [n for n, v in t.items() if not ALIAS.match(v)]
print("literal values: %d, all in layer %s" % (len(literals), sorted(set(layer(n) for n in literals))))
depth = Counter(resolve(t, n)[1] for n in t if layer(n) != "ref")
hops = ", ".join("%d hop(s) %d" % kv for kv in sorted(depth.items()))
print("alias hops to a literal (sys + comp): %s" % hops)
deepest = [n for n in t if resolve(t, n)[1] == max(depth)]
print("deepest chain: %s" % deepest[0])
chain, n = [deepest[0]], deepest[0]
while ALIAS.match(t[n]):
n = ALIAS.match(t[n]).group(1)
chain.append(n)
print(" " + " -> ".join(chain) + " = " + t[n])
fan_in = Counter(b for _, b in edges)
print("most-referenced tokens: %s" % ", ".join("%s %d" % kv for kv in fan_in.most_common(4)))
used = set()
for cat in FAMILY_PRIMARY:
for mode in MODES:
ts = token_set(cat, mode)
for nm in ts:
if layer(nm) != "ref":
x = nm
while ALIAS.match(ts[x]):
x = ALIAS.match(ts[x]).group(1)
used.add(x)
orphans = sorted(n for n in REF if n not in used)
print("primitives never reached by any of the 8 themes: %d" % len(orphans))
for i in range(0, len(orphans), 4):
print(" " + ", ".join(orphans[i:i + 4]))
print()
flat = {}
for sid, cat in SITES:
for mode in MODES:
ts = token_set(cat, mode)
flat[(sid, mode)] = {n: resolve(ts, n)[0] for n in ts if layer(n) == "comp"}
cells = sum(len(v) for v in flat.values())
print("resolved component table: %d sites x %d modes x %d component tokens = %d cells"
% (len(SITES), len(MODES), len(COMP), cells))
distinct = Counter(tuple(sorted(v.items())) for v in flat.values())
print("distinct resolved component tables: %d (= 4 families x 2 modes)" % len(distinct))
ovr = sum(len(override(c, m)) for c in FAMILY_PRIMARY for m in MODES)
print("hand-maintained entries if every site kept its own table: %d ; with the algebra: %d"
% (cells, len(REF) + len(SYS_SHARED) + 2 * len(BASE["light"]) + len(COMP) + ovr))
print(" of which category overrides: %d entries" % ovr)
print("sites per family: %s" % ", ".join("%s %d" % kv for kv in Counter(c for _, c in SITES).items()))
ts = token_set("report", "dark")
again = {n: resolve(ts, n)[0] for n in ts}
print("resolution is idempotent (resolving a resolved table changes nothing): %s"
% all(resolve(again, n)[0] == again[n] for n in again))
if __name__ == "__main__":
main()Single source and citation
Fan, Y. (2026). Design-Token Algebra across Thirty Sites, Perceptually Uniform Colour Spaces and the Millisecond Science of Brand-Authority First Impressions (Version 0.1.0), Chapter 2: Token Algebra: Three Layers, Themes as Functions and the Dependency Graph of Thirty Sites. 5D Library. DOI pending
DOI pending · monograph page goes live after publication · 5DL-B-140 · v0.1.0 · CC-BY-NC-ND-4.0
BibTeX
@book{fan2026140,
author = {Fan, Yafu},
title = {Design-Token Algebra across Thirty Sites, Perceptually Uniform Colour Spaces and the Millisecond Science of Brand-Authority First Impressions},
year = {2026},
publisher = {5D Library},
edition = {0.1.0},
note = {DOI pending}
}Other chapter answers in this book
- Chapter 1 · Method: Three Measures of Multi-Site Visual Drift, Grades of Evidence and the Cai-Fen Module of the Yingzao Fashi
- Chapter 3 · Perceptually Uniform Colour Spaces: From sRGB to CIELAB, CIEDE2000 and OKLab
- Chapter 4 · Perceptually Uniform Ramps: Lightness Steps, Contrast Constraints, Gamut Clipping and Dark-Theme Inversion
- Chapter 5 · The 50-Millisecond First Impression: Exposure, Complexity, Prototypicality and the Distance to a Sale
- Chapter 6 · Governing Thirty Sites: Release, Drift Detection, Per-Brand Re-Theming, Audit and Rollback
- Chapter 7 · Playbooks for the Four Kinds of Site, and the Distillation Spec for the Four Kits
- Chapter 8 · Practice: Unifying the Palettes of Five Representative Hengyu Sites, with a Pre-Registered First-Impression Test