Helpers, constants, metrics

Token factories, DTCG type guards, schema constants, font metrics and typography metadata.

These are the building blocks the engine uses internally, exported for tools built on top of it: bridge libraries, MCP servers, audits. Everything below comes from both @flexpixels/cortex and @flexpixels/cortex/browser.

Token factories

Used by every engine module to keep DTCG structure consistent. Build your own engine extensions on top of these instead of hand-rolling $value/$type objects.

ts
import {
  dimToken,
  dimPx,
  aliasToken,
  colorToken,
  cssExprToken,
  shadowToken,
  numberToken,
  durationToken,
  cubicBezierToken,
  fontFamilyToken,
  compositeTypographyToken,
} from "@flexpixels/cortex";
Helper$typeNotes
dimToken(value, unit?)dimensionunit defaults to "rem"; px values are converted to rem (value / 16).
dimPx(value)dimensionShorthand for dimToken(value, "px").
aliasToken(path, type)<passed type>Emits "{path}" as the alias value; the CSS serializer writes it as a var(--fp-…) reference.
colorToken(colorSpace, components, alpha?, hex?)colorBuild a DTCG color literal (sRGB, OKLCH, P3, …).
cssExprToken(type, css)<passed type>Escape hatch for raw CSS strings (e.g. oklch(from …)); stored as $value and in $extensions["com.flexpixels"].css.
shadowToken(shadowOrShadows)shadowOne or many DTCGShadowObjects.
numberToken(value)numberPlain numeric token (multipliers, opacity, axis values).
durationToken(ms)durationEmits "<ms>ms" (the DTCG export writes it as { value, unit: "ms" }).
cubicBezierToken(x1, y1, x2, y2)cubicBezierFour-element control-point array.
fontFamilyToken(family)fontFamilyCSS-ready font-family stack.
compositeTypographyToken({ fontFamily, fontSize, lineHeight, fontWeight, letterSpacing })typographyDTCG composite (DTCG export only — no CSS shorthand maps cleanly).

DTCG type guards + types

ts
import {
  isDimension,
  isColor,
  isShadowObject,
  isShadowArray,
  type DTCGValue,
  type DTCGDimension,
  type DTCGColor,
  type DTCGShadowObject,
  type W3CToken,
  type TokenTree,
} from "@flexpixels/cortex";

The guards narrow DTCGValue so consumers can walk the token tree without hand-typing. W3CToken is the leaf shape ({ $value, $type, $description?, $extensions? }); TokenTree is the recursive group shape ({ [key]: W3CToken | TokenTree }).

Schema constants

The constants below are plain data, frozen at every depth (copy one before changing it: a write throws in strict mode); all but TYPOGRAPHY_SCENARIOS, ELEVATION_SCENARIOS and DEFAULT_BREAKPOINTS are typed as const. The engine, the MCP server and this site read them, so code built on these lists follows cortex when it changes.

ExportDescription
HUE_ANCHORSAuto-generated semantic hues: { success: 145, danger: 25, warning: 85, info: 250 }.
CHROMA_LIGHTNESS_RAMP_LIGHT12 OKLCH L values for the light-mode ramp (0.985 … 0.22).
CHROMA_LIGHTNESS_RAMP_DARK12 OKLCH L values for the dark-mode ramp (0.15 … 0.92).
CHROMA_CURVE12 Gaussian multipliers applied to seed chroma (peak at step 9).
ALPHA_RAMP12 opacities for the static white / black roles.
ALPHA_STOPS5 opacities for dynamic role tints (a1..a5 = [5%, 10%, 20%, 30%, 45%]).
STEP_USAGEPer-step usage guide ({ 1: "App Background (Page canvas)", 2: "Subtle Background (Sidebars, Cards)", … }).
TYPOGRAPHY_SCENARIOSBundles applied when typography.scenario is set (marketing, app, editorial, dashboard).
ELEVATION_SCENARIOSBundles applied when elevation.scenario is set (same intent keys as typography).
COLOR_ROLES["primary","secondary","neutral","success","danger","warning","info"] as const.
DEFAULT_BREAKPOINTS{ xs:320, sm:640, md:768, lg:1024, xl:1280, "2xl":1920 }.
GRADIENT_PRESETS["none","primary-secondary","primary-neutral","secondary-neutral"] as const.

Font metrics + shadow visibility

ts
import {
  INTER_METRICS,
  GEIST_MONO_METRICS,
  computeTrim,
  computeShadowVisibility,
  SURFACE_BACKGROUND_LIGHTNESS,
  FLAT_SHADOW_LIGHTNESS,
  type FontMetrics,
} from "@flexpixels/cortex";
  • INTER_METRICS, GEIST_MONO_METRICS — bundled FontMetrics for the default Inter (display + body) and Geist Mono (mono) family slots. INTER_METRICS is sourced from @capsizecss/metrics; GEIST_MONO_METRICS is extracted from the shipped GeistMono-Variable woff2 via @capsizecss/unpack.
  • computeTrim(metrics, lineHeightEm) — returns { trimTop, trimBottom } in em (always ≥ 0). Consumers negate them in CSS: margin-top: calc(-1em * 0.522).
  • computeShadowVisibility(shadowL, bgL, alpha) — returns { contrast: 0..1, perceivedDepth: 0..10 } for a shadow of lightness shadowL and opacity alpha on a background of lightness bgL. The elevation engine uses it for the opt-in --fp-elevation-{n}-perceived-depth, -contrast-on-app-light and -contrast-on-app-dark tokens (elevation.perceptual, in the flat, liquid-glass or auto style).
  • SURFACE_BACKGROUND_LIGHTNESS — { appLight: 0.985, appDark: 0.15, subtleLight: 0.96, subtleDark: 0.19 }. The canonical OKLCH L for --fp-neutral-1 / --fp-neutral-2 in each mode.
  • FLAT_SHADOW_LIGHTNESS — 0.1. The L of the flat-style shadow tint.
ts
const trim = computeTrim(INTER_METRICS, 1.5);
// { trimTop: 0.5225, trimBottom: 0.25 }

const vis = computeShadowVisibility(
  FLAT_SHADOW_LIGHTNESS,
  SURFACE_BACKGROUND_LIGHTNESS.appDark,
  0.12,
);
// { contrast: 0.006, perceivedDepth: 0.3 } — a 12% shadow all but disappears on the dark app background

Typography metadata accessors

ts
import { getResolvedTypeStyle, listBuiltInTextRoles, type TypeStyleDef } from "@flexpixels/cortex";
  • listBuiltInTextRoles() — canonical role list (caption, label, body, body-lg, title, headline, display, button, input, helper, code, link, quote, data).
  • getResolvedTypeStyle(typography, role) — returns your typography.typeStyles[role] when you define it (it replaces the built-in definition whole), otherwise the built-in definition; undefined for an unknown role. Built-in definitions carry intent, whenToUse, whenNotToUse and examples, and some carry minViewportWidth: the fields the MCP resource flexpixels://typography/roles/{role} serves.

computeLineHeights

ts
import { computeLineHeights, type LineHeightFormula } from "@flexpixels/cortex";

computeLineHeights([12, 14, 16, 18], "ratio-1.5");
// [18, 22, 24, 28]

computeLineHeights([13, 15], "compact", /* roundTo */ 1);
// [17, 20]

The same helper the typography engine uses to fill in missing lineHeights entries. roundTo defaults to 2 (the 2px grid); pass 1 to disable snapping when strictGrid: false.