What does Design-Token Algebra conclude, in numbers, about “Practice: Unifying the Palettes of Five Representative Hengyu Sites, with a Pre-Registered First-Impression Test”?

Answer: Chapter 8 takes five sites through the whole process: 17 roles out of tolerance and 746 of 1,655 declarations hard-coded; a theme function that fills four families with 15 colour values and derives a new token because gold reaches a contrast of only 2.69; a migration in which only 0.060 of hard-coded values can be replaced automatically and the group's colours shrink from 103 to 15 tokens plus 37 exceptions; and a pre-registered test of 90 raters, while detecting a 10% lift in sales would take 26.9 weeks. Each hypothesis states what would refute it.

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: 0.060, 10, 103, 15, 37

site strays auto(<2) review(2-10) exception(>10) colours before after primary jump
B033 0 3 0 9 9 4.50
B0814 1 6 7 20 16 12.27
R0628 1 12 15 34 24 4.68
C0318 2 11 5 24 14 6.99
K0621 1 10 10 27 19 5.32
total strays 84: auto 5 (0.060), review 42 (0.500), exception 37
distinct colour values on the five sites114 before, 82 after (per-site sums)
distinct colour values across the five as a group103 before; after: 15 tokens + 37 exceptions
sites whose primary jumps by more than 5.0: B08 C03 K06 - stage these with a notice, not overnight
Show the full program
# Chapter 8. p82: Migrating the five sites onto their themes - stray values, review queue, before and after.
# Each stray hard-coded value (p80 model) is mapped to the nearest semantic token of its family theme by
# CIEDE2000: under 2.0 it is replaced automatically, 2.0 to 10.0 goes to a human review queue, 10.0 or more
# stays as a registered exception until someone decides. Thresholds are this book's registered rules.
# "Jump" = dE00 between what returning visitors saw (the live primary) and the token they will see now.
import math
import random
import statistics

fs = math.fsum
M = ((0.4124564, 0.3575761, 0.1804375), (0.2126729, 0.7151522, 0.0721750), (0.0193339, 0.1191920, 0.9503041))
WHITE = (0.95047, 1.0, 1.08883)


def dec(c):
    return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4


def enc(v):
    v = min(1.0, max(0.0, v))
    return 12.92 * v if v <= 0.0031308 else 1.055 * v ** (1 / 2.4) - 0.055


def hex2rgb(h):
    return tuple(int(h[i:i + 2], 16) / 255 for i in (1, 3, 5))


def rgb2hex(rgb):
    return "#" + "".join("{:02X}".format(round(min(1.0, max(0.0, c)) * 255)) for c in rgb)


def lab(h):
    lin = [dec(c) for c in hex2rgb(h)]
    xyz = [fs(m * v for m, v in zip(row, lin)) / w for row, w in zip(M, WHITE)]

    def f(t):
        return t ** (1 / 3) if t > (6 / 29) ** 3 else t / (3 * (6 / 29) ** 2) + 4 / 29
    fx, fy, fz = (f(t) for t in xyz)
    return (116 * fy - 16, 500 * (fx - fy), 200 * (fy - fz))


def de00(p, q):
    (L1, a1, b1), (L2, a2, b2) = p, q
    cb = (math.hypot(a1, b1) + math.hypot(a2, b2)) / 2
    g = 0.5 * (1 - math.sqrt(cb ** 7 / (cb ** 7 + 25 ** 7)))
    a1p, a2p = (1 + g) * a1, (1 + g) * a2
    c1, c2 = math.hypot(a1p, b1), math.hypot(a2p, b2)
    h1 = math.degrees(math.atan2(b1, a1p)) % 360 if c1 else 0.0
    h2 = math.degrees(math.atan2(b2, a2p)) % 360 if c2 else 0.0
    dh = 0.0
    if c1 * c2:
        dh = h2 - h1
        dh = dh - 360 if dh > 180 else dh + 360 if dh < -180 else dh
    dl, dc = L2 - L1, c2 - c1
    dhh = 2 * math.sqrt(c1 * c2) * math.sin(math.radians(dh / 2))
    lm, cm = (L1 + L2) / 2, (c1 + c2) / 2
    if c1 * c2 == 0:
        hm = h1 + h2
    elif abs(h1 - h2) <= 180:
        hm = (h1 + h2) / 2
    else:
        hm = (h1 + h2 + 360) / 2 if h1 + h2 < 360 else (h1 + h2 - 360) / 2
    t = fs([1, -0.17 * math.cos(math.radians(hm - 30)), 0.24 * math.cos(math.radians(2 * hm)),
            0.32 * math.cos(math.radians(3 * hm + 6)), -0.20 * math.cos(math.radians(4 * hm - 63))])
    rot = 30 * math.exp(-((hm - 275) / 25) ** 2)
    rc = 2 * math.sqrt(cm ** 7 / (cm ** 7 + 25 ** 7))
    sl = 1 + 0.015 * (lm - 50) ** 2 / math.sqrt(20 + (lm - 50) ** 2)
    sc, sh = 1 + 0.045 * cm, 1 + 0.015 * cm * t
    rt = -math.sin(math.radians(2 * rot)) * rc
    return math.sqrt(fs([(dl / sl) ** 2, (dc / sc) ** 2, (dhh / sh) ** 2, rt * (dc / sc) * (dhh / sh)]))


def oklch_of(h):
    r, g, b = (dec(c) for c in hex2rgb(h))
    lms = (fs([0.4122214708 * r, 0.5363325363 * g, 0.0514459929 * b]),
           fs([0.2119034982 * r, 0.6806995451 * g, 0.1073969566 * b]),
           fs([0.0883024619 * r, 0.2817188376 * g, 0.6299787005 * b]))
    l_, m_, s_ = (math.copysign(abs(v) ** (1 / 3), v) for v in lms)
    L = fs([0.2104542553 * l_, 0.7936177850 * m_, -0.0040720468 * s_])
    a = fs([1.9779984951 * l_, -2.4285922050 * m_, 0.4505937099 * s_])
    bb = fs([0.0259040371 * l_, 0.7827717662 * m_, -0.8086757660 * s_])
    return L, math.hypot(a, bb), math.degrees(math.atan2(bb, a)) % 360


def hex_of_oklch(L, C, H):
    a, b = C * math.cos(math.radians(H)), C * math.sin(math.radians(H))
    l_, m_, s_ = (fs([L, 0.3963377774 * a, 0.2158037573 * b]), fs([L, -0.1055613458 * a, -0.0638541728 * b]),
                  fs([L, -0.0894841775 * a, -1.2914855480 * b]))
    l, m, s = l_ ** 3, m_ ** 3, s_ ** 3
    lin = (fs([4.0767416621 * l, -3.3077115913 * m, 0.2309699292 * s]),
           fs([-1.2684380046 * l, 2.6097574011 * m, -0.3413193965 * s]),
           fs([-0.0041960863 * l, -0.7034186147 * m, 1.7076147010 * s]))
    return rgb2hex(tuple(enc(v) for v in lin))


# Self-check against Sharma, Wu & Dalal (2005), test pairs 1 and 17.
for p, q, want in (((50, 2.6772, -79.7751), (50, 0, -82.7485), 2.0425),
                   ((50, 2.5, 0), (73, 25, -18), 27.1492)):
    assert abs(de00(p, q) - want) < 5e-5, (p, q, de00(p, q))

ROLES = ("primary", "accent", "text", "muted", "surface", "border")
CANON = {"accent": "#B8913F", "text": "#1B1B20", "muted": "#6B6B76",
         "surface": "#F7F5F0", "border": "#E4E2DC"}
BRAND = {"B": "#8C2F39", "R": "#1F5FA8", "C": "#2E7D5B", "K": "#6B4FA0"}   # primary: one brand per family
FAMILY = {"B": "boutique", "R": "report", "C": "course", "K": "card"}
SITES = [f + "{:02d}".format(i) for f, n in (("B", 8), ("R", 8), ("C", 7), ("K", 7)) for i in range(1, n + 1)]
KEEP = {"in-house": 0.85, "agency": 0.55, "template": 0.30}
SPREAD = {"in-house": 0.6, "agency": 1.0, "template": 1.8}
TOL = 2.0


def canon_of(code, role):
    return BRAND[code[0]] if role == "primary" else CANON[role]


def build(seed=140):
    rng = random.Random(seed)
    out = []
    for code in SITES:
        builder = rng.choices(("in-house", "agency", "template"), weights=(5, 3, 2))[0]
        live = {}
        for role in ROLES:
            canon = canon_of(code, role)
            if rng.random() < KEEP[builder]:
                live[role] = canon
                continue
            L, C, H = oklch_of(canon)
            s = SPREAD[builder]
            L2 = min(0.99, max(0.05, L + rng.gauss(0, 0.035 * s)))
            C2 = max(0.0, C + rng.gauss(0, 0.025 * s))
            live[role] = hex_of_oklch(L2, C2, H + rng.gauss(0, 7 * s))
        decl = rng.randint(140, 460)
        hard = round(decl * (1 - KEEP[builder]) * rng.uniform(0.5, 1.0))
        out.append((code, builder, live, decl, hard))
    return out


FIVE = ("B03", "B08", "R06", "C03", "K06")


def strays(code, builder, hard, seed=840):
    """Distinct hard-coded colour values on a site: drifted copies of its roles (seeded, invented)."""
    rng = random.Random(seed + SITES.index(code))
    out = set()
    for _ in range(max(3, round(hard / 9))):
        L, C, H = oklch_of(canon_of(code, rng.choice(ROLES)))
        s = 1.5 * SPREAD[builder]
        out.add(hex_of_oklch(min(0.99, max(0.05, L + rng.gauss(0, 0.035 * s))),
                             max(0.0, C + rng.gauss(0, 0.025 * s)), H + rng.gauss(0, 7 * s)))
    return sorted(out)


def lum(h):
    r, g, b = (dec(c) for c in hex2rgb(h))
    return fs([0.2126 * r, 0.7152 * g, 0.0722 * b])


def contrast(a, b):
    hi, lo = sorted((lum(a), lum(b)), reverse=True)
    return (hi + 0.05) / (lo + 0.05)


def solve(h, bg, target):
    """Lower OKLab L in steps of 0.005, keeping hue and chroma, until the pair passes."""
    L, C, H = oklch_of(h)
    out = h
    while contrast(out, bg) < target:
        L -= 0.005
        out = hex_of_oklch(L, C, H)
    return out


def theme(fam):
    """The theme function: family -> semantic tokens, from the primitives and the family brand."""
    brand = BRAND[fam]
    L, C, H = oklch_of(brand)
    t = {"primary": brand, "primary-hover": hex_of_oklch(L - 0.06, C, H),
         "on-primary": "#FFFFFF" if contrast("#FFFFFF", brand) >= 4.5 else CANON["text"],
         "link": solve(brand, CANON["surface"], 4.5), "accent": CANON["accent"],
         "accent-text": solve(CANON["accent"], CANON["surface"], 4.5), "text": CANON["text"],
         "muted": CANON["muted"], "surface": CANON["surface"], "canvas": "#FFFFFF", "border": CANON["border"]}
    return t


PAIRS = (("text", "surface", 4.5), ("muted", "surface", 4.5), ("on-primary", "primary", 4.5),
         ("on-primary", "primary-hover", 4.5), ("link", "surface", 4.5), ("accent-text", "surface", 4.5),
         ("text", "accent", 4.5), ("accent", "surface", 3.0))
rows = {r[0]: r for r in build()}
print("site  strays  auto(<2)  review(2-10)  exception(>=10)  colours before  after  primary jump")
tot = {"auto": 0, "review": 0, "exc": 0, "before": 0, "after": 0}
kept_all = set()
for code in FIVE:
    _, builder, live, decl, hard = rows[code]
    t = theme(code[0])
    tokens = sorted(set(t.values()))
    auto = review = exc = 0
    kept = set()
    for s in strays(code, builder, hard):
        d = min(de00(lab(s), lab(v)) for v in tokens)
        if d < 2.0:
            auto += 1
        elif d < 10.0:
            review += 1
        else:
            exc += 1
            kept.add(s)
    before = len(set(live.values()) | set(strays(code, builder, hard)))
    after = len(set(tokens) | kept)
    kept_all |= kept
    jump = de00(lab(live["primary"]), lab(t["primary"]))
    for k, v in (("auto", auto), ("review", review), ("exc", exc), ("before", before), ("after", after)):
        tot[k] += v
    print("{:4s}  {:6d}  {:8d}  {:12d}  {:15d}  {:14d}  {:5d}  {:12.2f}".format(
        code, auto + review + exc, auto, review, exc, before, after, jump))
n = tot["auto"] + tot["review"] + tot["exc"]
print("total strays {}: auto {} ({:.3f}), review {} ({:.3f}), exception {}".format(
    n, tot["auto"], tot["auto"] / n, tot["review"], tot["review"] / n, tot["exc"]))
print("distinct colour values on the five sites: {} before, {} after (per-site sums)".format(
    tot["before"], tot["after"]))
union_before = set()
for code in FIVE:
    _, builder, live, decl, hard = rows[code]
    union_before |= set(live.values()) | set(strays(code, builder, hard))
union_after = {v for code in FIVE for v in theme(code[0]).values()}
print("distinct colour values across the five as a group: {} before; after: {} tokens + {} exceptions".format(
    len(union_before), len(union_after), len(kept_all - union_after)))
big = [c for c in FIVE if de00(lab(rows[c][2]["primary"]), lab(BRAND[c[0]])) > 5.0]
print("sites whose primary jumps by more than 5.0: {} - stage these with a notice, not overnight".format(
    " ".join(big)))

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 8: Practice: Unifying the Palettes of Five Representative Hengyu Sites, with a Pre-Registered First-Impression Test. 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