API overview

The main public exports of @flexpixels/cortex, grouped by purpose, with the entry point for each.

@flexpixels/cortex turns a theme config into tokens and CSS. Apart from the ThemeEngine class, which holds one config, its exports are functions and data whose results depend only on their inputs. It touches no DOM, and only the config loader reads files. Every export below comes from the package root; everything except the config loader also comes from @flexpixels/cortex/browser.

Entry points

PathWhen to use
@flexpixels/cortexEverything, including the Node-only config loader. Under a bundler’s browser condition it resolves to the browser build.
@flexpixels/cortex/browserEverything except loadUserConfig, loadConfigFromPath and mergeWithDefaults. Safe to bundle for the browser.
@flexpixels/cortex/runtimeLive theming in the browser: CSS generation and checkContrast, without zod. See Runtime entry.
@flexpixels/cortex/nodeThe Node build by name; the same as the root under Node and Bun.
@flexpixels/cortex/theme-config.schema.jsonThe JSON Schema of theme-config.json, for editors.
@flexpixels/cortex/a2ui-bridge.cssA stylesheet that maps the A2UI v0.9 basic-catalog variables onto cortex tokens. See A2UI bridge.

Theme stylesheets

  • generateThemeCSS(config, options?) — the stylesheet a page loads by default. It honours config.output: the lean token set unless output.tokens is "full". Options: reads (the token names your components use; defaults to what FlexPixels components read) and full (a full stylesheet you already generated, to skip generating it again).
  • generateFullThemeCSS(config) — every token the engine computes. Alias: generateCSSOutput.
  • generateThemeSubsetCSS(config, { roots, name?, full? }) — a stylesheet with only the tokens named in roots and everything they reference.
  • subsetThemeCSS(css, roots) — the same, from a stylesheet you already have.
  • diffThemeCSS(base, next, { reset? }) — the declarations of next that differ from base, and a reset for each custom property only base declares: what to load after base to get next. reset: false only adds and overrides. A diff cannot undo an @property registration: after a prefix change, the base’s registered motion switches keep their initial values.
  • declaredCustomProperties(css), themeTokenNames(css) — the custom property names a stylesheet declares (the second also counts @property registrations).
  • leanThemeRoots(css, config, reads?), LEAN_FAMILIES, CONTRAST_MATRIX, COMPONENT_READS — what decides the lean set.
  • generateSizeHostCSS(config), generateMotionHostCSS(config), generateMotionCSS(prefix, scope) (the scope is what sits inside the root, such as ":root *"; the root itself has no parent to read the switches from and is refused) — the size-axis and motion sheets FlexPixels components adopt in their shadow roots.
  • CONDITIONAL_THEME_TOKENS — tokens a theme declares only under some configs (glow-size, surface-app-image, vision-variants).
  • buildModernThemeCSS(config) — the full stylesheet before the compatibility fallbacks and the vision blocks are added.
  • parseCssNodes(css) — parses a stylesheet into CssNodes (blocks, declarations, block-less at-rules). It is written for the theme’s own output and throws on an unbalanced brace.
  • retargetRoot(css, selector, drop?) — moves every :root rule to selector and leaves out the blocks whose prelude drop returns true for.

See Build & Import for choosing between lean and full.

Token tree

  • ThemeEngine — holds a validated config and its token tree (getTokens, getConfig, getPrefix, getContrast, updateConfig to merge a change, setConfig to replace).
  • generateAllTokens(config) — the token tree as plain data (AllTokens), without serializing it.
  • Per-domain generators, for advanced pipelines: generateColorTokens, generateDataVizTokens, generateSpacingTokens, generateRadiusTokens, generateBorderTokens, generateElevationTokens, generateInsetTokens, generateGapTokens, generateSizeTokens, generateInteractionTokens, generateLayoutTokens, generateScrollTokens, generateTypographyTokens.

Config: schemas, validation, loading

Export formats

The public token tier

The closed set of tokens the AI-facing outputs (DESIGN.md with tier: "public", the MCP token tools) offer by default:

  • PUBLIC_TIER — the tokens, each with a group and a purpose; TIER_GROUPS — the group names.
  • isPublicToken(name, prefix?), publicTierByGroup() — look-ups.
  • publicTierValues(config) — each public token’s resolved value for a config.
  • resolveTokens(css, names, prefix?, scheme?) — resolve token names to values in a stylesheet. names are bare ("primary-9", not "--fp-primary-9"), and a name the stylesheet does not declare throws.

Contrast and colour math

  • calcLc, calcLcHex, wcagRatio, checkContrast, buildContrastMatrix — APCA and WCAG primitives.
  • LC_TARGETS, WCAG_FLOORS, WCAG_FLOORS_AAA — the per-tier targets the engine applies (the AAA floors when settings.accessibility is "AAA").
  • srgbChromaCeil(h) — the highest seed chroma at hue h that keeps steps 7–10 inside sRGB; maxSrgbChroma(h, l) — the highest chroma sRGB can show at a hue and lightness.
  • hoverOf(seed, scheme), HOVER_DELTA_L — how step 10 is derived from a brand seed.
  • textColor(base, page) — the nearest colour to base that reaches 4.5:1 on every page colour.
  • resolveSurfaceAnchor(seeds, neutralSeed, scheme) — the neutral steps that seeds.surface overrides in a scheme ("dark" when left out).

See Contrast for how they fit together.

Soft warnings

Typography metadata

  • getResolvedTypeStyle(typography, role) — the type style for a role: yours from typography.typeStyles if you define it, otherwise the built-in one.
  • listBuiltInTextRoles() — the 14 built-in roles: caption, label, body, body-lg, title, headline, display, button, input, helper, code, link, quote, data.
  • computeLineHeights(fontSizes, formula?, roundTo?) — the default line-height ladder for a scale.

Font metrics and shadow visibility

  • INTER_METRICS, GEIST_MONO_METRICS — bundled FontMetrics for the default families.
  • computeTrim(metrics, lineHeightEm) — leading-trim offsets for cap-height and baseline alignment.
  • computeShadowVisibility(shadowL, bgL, alpha) — { contrast, perceivedDepth } for a shadow on a background.
  • SURFACE_BACKGROUND_LIGHTNESS, FLAT_SHADOW_LIGHTNESS — the lightness constants behind the opt-in perceptual elevation tokens. FLAT_SHADOW_LIGHTNESS is also the lightness of the surface-aware shadow tints.

Token helpers and DTCG types

  • Factories: dimToken, dimPx, aliasToken, colorToken, cssExprToken, shadowToken, numberToken, durationToken, cubicBezierToken, fontFamilyToken, compositeTypographyToken.
  • Type guards: isDimension, isColor, isShadowObject, isShadowArray.
  • Types: DTCGValue, DTCGDimension, DTCGColor, DTCGShadowObject, W3CToken, TokenTree.

Constants

Plain data, safe to read from tooling and agent code:

  • HUE_ANCHORS — hues of the generated semantic roles (success 145, danger 25, warning 85, info 250).
  • CHROMA_LIGHTNESS_RAMP_LIGHT, CHROMA_LIGHTNESS_RAMP_DARK — the 12-step OKLCH lightness ramps.
  • CHROMA_CURVE — the per-step share of the seed’s chroma.
  • ALPHA_RAMP — the static white / black alpha ramp; ALPHA_STOPS — the a1..a5 opacities of each role.
  • STEP_USAGE — what each of the 12 steps is for.
  • TYPOGRAPHY_SCENARIOS, ELEVATION_SCENARIOS — the bundles behind typography.scenario and elevation.scenario.
  • COLOR_ROLES — primary, secondary, neutral, success, danger, warning, info.
  • DEFAULT_BREAKPOINTS — xs 320, sm 640, md 768, lg 1024, xl 1280, 2xl 1920.
  • GRADIENT_PRESETS — none, primary-secondary, primary-neutral, secondary-neutral.

Vision palettes and colour-vision simulation

How the palettes are used is on Vision variants.

  • visionPalettes(config, kinds?, policy?) — one VisionPalette per kind (all four by default): the seeds and settings the variant changes, the variant’s full config, and a report on whether it met its targets.
  • adaptPaletteForVision(config, kind, policy?) — the same for one kind.
  • generateVisionCSS(config) — each variant’s block (the declarations its palette changes, on :root[data-vision="…"]), keyed by kind. generateFullThemeCSS inlines these blocks unless vision.output is "separate".
  • variantConfig(config, seeds, settings?) — your config with a variant’s seeds and settings applied, and no variants of its own.
  • VISION_KINDS — red-green, blue-yellow, monochrome, high-contrast. VISION_SIMULATES — the deficiencies each kind is measured under. VISION_TARGETS — the smallest CIEDE2000 between two roles’ step 9 that each kind aims for.
  • SEMANTIC_HUE_WINDOWS, inHueWindow(role, hue) — the OKLCH hue range each semantic role stays in, and whether a hue is inside it.
  • simulateCvd(color, kind) — a colour as seen with protanopia, deuteranopia or tritanopia (the MACHADO_2009 matrices on linear sRGB), or achromatopsia (a grey of the same luminance). cvdDeltaE(a, b, kind) — the CIEDE2000 between two colours as that vision sees them.

Auto-contrast

For a background colour that is known only at runtime, so no token can be solved for it:

  • autoContrastFg(r, g, b) — "#ffffff" or "#000000", picked by luminance. It clears 4.5:1 on every sRGB colour.
  • autoContrastTint(r, g, b, tint?) — that colour mixed tint (default 0.25) of the way back toward the background in oklab. For icons and boundaries (3:1), not text.
  • autoLuminance(r, g, b) — the luminance the CSS rule computes (channels 0–255, linearised with pow(x, 2.2)).
  • autoContrastCss() — the --fp-auto-text and --fp-auto-icon declarations that @flexpixels/ui ships in theme/auto-contrast.css.
  • AUTO_CONTRAST — the rule’s gamma, threshold, luminance coefficients, tint and floors.

Rendered colours and contrast pairs

  • renderedColors(css, { overlay? }) — returns a lookup (name, mode) => string: the colour a browser paints for a token of css in "light" or "dark". name is the full custom property name ("--fp-primary-9", not "primary-9" as resolveTokens takes). overlay lays extra declarations over :root.
  • renderedContrast(a, b) — the WCAG 2 ratio of two CSS colours. Throws if either is not a colour.
  • colorChannels(css) — a CSS colour as sRGB channels on 0–1 with alpha, or null.
  • tokenAliases(css) — returns (name) => string[]: the tokens a token is a plain var() alias of, nearest first.
  • contrastPartners(background, roles?) — the foreground tokens the contrast contract pairs with a background, for text, soft text and icons, with the background’s kind and a one-line note.
  • isContrastPartner(foreground, background, roles?) — whether a foreground token is one of those partners.
  • tokenName(value) — the bare token name: --fp-primary-9, var(--fp-primary-9) and primary-9 all give primary-9. It returns null for a value it does not recognise.

Their types are exported too: VisionKind, VisionPolicy, VisionPalette, VisionReport, VisionMetrics, CvdKind, Rendered, RenderedOptions, BackgroundKind, ContrastPartners, CssNode.

The sidebar pages cover the most-used groups in depth; Helpers & constants covers the token factories, constants and metrics.