Colors v0.1.0

API Reference

All exports are available from the main package: import { ... } from '@sveltopia/colors'

Core

generatePalette(options)

Generates a complete palette from brand colors.

typescript
import { generatePalette } from '@sveltopia/colors';

// Returns a single-mode LightPalette — call once per mode
const light = generatePalette({
  brandColors: ['#FF4F00', '#1A1A1A'],
  // Optional: 'light' (default) | 'dark'
  mode: 'light',
  // Optional: override the auto-detected tuning profile
  tuningProfile: undefined
});

// light.scales['orange'][9] → '#FF4F00' (or close to it)
// light.meta.anchoredSlots → ['orange']
// light.meta.tuningProfile → { hueShift, chromaMultiplier, ... }

// Generate dark mode with the same inputs
const dark = generatePalette({
  brandColors: ['#FF4F00', '#1A1A1A'],
  mode: 'dark'
});

analyzeBrandColors(colors, mode?)

Analyzes brand colors and returns a tuning profile describing how they relate to the baseline palette. Used internally by generatePalette, but useful for understanding what the generator will do.

typescript
import { analyzeBrandColors } from '@sveltopia/colors';

const profile = analyzeBrandColors(['#FF4F00']);

// profile.hueShift → average hue offset from baseline
// profile.chromaMultiplier → brand chroma relative to baseline
// profile.anchors → { '#FF4F00': { slot: 'orange', step: 9 } }

Export functions

All export functions take a Palette (with both light and dark scales) and return a formatted string.

exportCSS(palette, options?)

CSS custom properties for all scales.

typescript
import { exportCSS } from '@sveltopia/colors';

const css = exportCSS(palette, {
  lightSelector: ':root',              // default
  darkSelector: '.dark, .dark-theme',  // default
  includeAlpha: true,             // default
  includeP3: true,                // default
  includeSemantic: true,          // default
  scales: undefined,              // all scales, or ['orange', 'blue']
  prefix: '',                     // CSS variable prefix
  mode: 'both'                    // 'light' | 'dark' | 'both'
});

exportJSON(palette, options?)

Structured JSON with hex, OKLCH, and P3 formats for each color.

typescript
import { exportJSON } from '@sveltopia/colors';

const data = exportJSON(palette, {
  scales: undefined,              // all scales, or specific ones
  includeAlpha: true,
  includeP3: true,
  includeSemantic: true
});

// data.light['orange']['9'] → { hex: '#FF4F00', oklch: '...', p3: '...' }

exportTailwindV4CSS(palette, options?)

Tailwind v4 CSS with @theme block for first-class utility support.

typescript
import { exportTailwindV4CSS } from '@sveltopia/colors';

const css = exportTailwindV4CSS(palette, {
  includeSemanticRoles: true,     // primary, secondary, tertiary
  darkSelector: '.dark',
  lightSelector: ':root',
  includeThemeBlock: true         // @theme {} registration
});

exportTailwind(palette, options?)

Tailwind v3 JavaScript preset. Use this if you're still on Tailwind v3 (--format tailwind-v3 in the CLI).

typescript
import { exportTailwind } from '@sveltopia/colors';

const js = exportTailwind(palette, {
  scale: '50-950',          // '50-950' | '1-12'
  darkMode: 'class',        // 'class' | 'media'
  includeAlpha: false        // include alpha variants
});

exportTailwindWithCSSVars is also available — same options, but outputs CSS custom properties instead of hardcoded hex values.

exportShadcn(palette, options?)

shadcn/ui compatible CSS with semantic tokens.

typescript
import { exportShadcn } from '@sveltopia/colors';

const css = exportShadcn(palette, {
  radius: '0.625rem',
  neutralHue: 'slate',
  destructiveHue: 'red',
  includeCharts: true,
  includeSidebar: true,
  includeThemeBlock: true,
  includeThemeInlineBlock: true,
  lightSelector: ':root',
  darkSelector: '.dark'
});

exportRadix(palette, options?)

Radix Colors compatible JavaScript export.

typescript
import { exportRadix } from '@sveltopia/colors';

const js = exportRadix(palette, {
  format: 'esm',         // 'esm' | 'cjs'
  includeAlpha: true,
  includeP3: true
});

exportPanda(palette, brandColorInfo, options?)

Panda CSS preset. Requires brand color info for semantic token mapping.

typescript
import { exportPanda } from '@sveltopia/colors';

const brandColorInfo = [
  { hex: '#FF4F00', hue: 'orange', anchorStep: 9, isCustomRow: false }
];

const ts = exportPanda(palette, brandColorInfo, {
  includeSemantic: true   // include semantic tokens (default: true)
});

Validation

calculateAPCA(textHex, bgHex)

Returns the APCA contrast value between a text color and background color. Higher values mean more contrast. Typical range is 0–108.

typescript
import { calculateAPCA } from '@sveltopia/colors';

const contrast = calculateAPCA('#1A1A1A', '#FFFFFF');
// → ~106 (very high contrast)

validatePaletteContrast(palette, options?)

Validates all step combinations in a palette against APCA thresholds. Returns a detailed report.

typescript
import { validatePaletteContrast } from '@sveltopia/colors';

const report = validatePaletteContrast(palette, {
  hues: undefined,        // all hues, or ['orange', 'blue']
  errorsOnly: false       // true to suppress warnings
});

// report.passed → boolean
// report.totalChecks → number
// report.passedChecks → number
// report.issues → [{ hue, mode, textStep, bgStep, expected, actual, severity }]

Key types

typescript
interface OklchColor {
  l: number;      // Lightness (0–1)
  c: number;      // Chroma (0–~0.4)
  h: number;      // Hue (0–360)
  alpha?: number;
}

interface Scale {
  1: string;  // lightest (backgrounds)
  2: string;
  3: string;
  4: string;
  5: string;
  6: string;
  7: string;
  8: string;
  9: string;  // primary solid (brand anchor)
  10: string;
  11: string;
  12: string; // darkest (text)
}

interface Palette {
  light: Record<string, Scale>;
  dark: Record<string, Scale>;
  _meta: {
    tuningProfile: TuningProfile;
    inputColors: string[];
    generatedAt: string;
  };
}

interface TuningProfile {
  hueShift: number;
  chromaMultiplier: number;
  lightnessShift: number;
  anchors: Record<string, AnchorInfo>;
  customRows?: CustomRowInfo[];
}

interface ContrastReport {
  passed: boolean;
  totalChecks: number;
  passedChecks: number;
  issues: ContrastIssue[];
}

For the full type definitions, see the TypeScript source.