@cdx-ui/styles 0.0.1-beta.7 → 0.0.1-beta.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +116 -20
- package/css/theme.css +190 -83
- package/css/vanilla.css +117 -53
- package/lib/commonjs/applyThemeOverride.js +154 -0
- package/lib/commonjs/applyThemeOverride.js.map +1 -0
- package/lib/commonjs/index.js +82 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/palette.js +262 -0
- package/lib/commonjs/palette.js.map +1 -0
- package/lib/commonjs/theming.js +255 -0
- package/lib/commonjs/theming.js.map +1 -0
- package/lib/commonjs/types.js +75 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/useCdxFonts.js +7 -231
- package/lib/commonjs/useCdxFonts.js.map +1 -1
- package/lib/commonjs/useForgeFonts.js +237 -0
- package/lib/commonjs/useForgeFonts.js.map +1 -0
- package/lib/module/applyThemeOverride.js +149 -0
- package/lib/module/applyThemeOverride.js.map +1 -0
- package/lib/module/index.js +11 -20
- package/lib/module/index.js.map +1 -1
- package/lib/module/palette.js +257 -0
- package/lib/module/palette.js.map +1 -0
- package/lib/module/theming.js +239 -0
- package/lib/module/theming.js.map +1 -0
- package/lib/module/types.js +71 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/useCdxFonts.js +2 -220
- package/lib/module/useCdxFonts.js.map +1 -1
- package/lib/module/useForgeFonts.js +223 -0
- package/lib/module/useForgeFonts.js.map +1 -0
- package/lib/runtime/prestige-vs-default.json +1 -0
- package/lib/runtime/pulse-vs-default.json +1 -0
- package/lib/runtime/token-to-css-var.json +666 -0
- package/lib/typescript/applyThemeOverride.d.ts +26 -0
- package/lib/typescript/applyThemeOverride.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +8 -57
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/palette.d.ts +60 -0
- package/lib/typescript/palette.d.ts.map +1 -0
- package/lib/typescript/theming.d.ts +40 -0
- package/lib/typescript/theming.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +90 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/useCdxFonts.d.ts +2 -11
- package/lib/typescript/useCdxFonts.d.ts.map +1 -1
- package/lib/typescript/useForgeFonts.d.ts +12 -0
- package/lib/typescript/useForgeFonts.d.ts.map +1 -0
- package/package.json +27 -8
- package/runtime/prestige-vs-default.json +1 -0
- package/runtime/pulse-vs-default.json +1 -0
- package/runtime/token-to-css-var.json +666 -0
- package/src/__tests__/applyThemeOverride.test.ts +552 -0
- package/src/__tests__/generateColorScale.test.ts +296 -0
- package/src/__tests__/theming.test.ts +647 -0
- package/src/applyThemeOverride.ts +139 -0
- package/src/index.ts +36 -60
- package/src/palette.ts +307 -0
- package/src/theming.ts +268 -0
- package/src/types.ts +112 -0
- package/src/useCdxFonts.ts +2 -230
- package/src/useForgeFonts.ts +230 -0
- package/tokens/presets/.manifest.json +3 -3
- package/tokens/presets/poise.json +294 -38
- package/tokens/presets/prestige.json +1 -1
- package/tokens/presets/pulse.json +1 -1
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import prestigePatch from '../runtime/prestige-vs-default.json';
|
|
2
|
+
import pulsePatch from '../runtime/pulse-vs-default.json';
|
|
3
|
+
import defaultRuntimeMap from '../runtime/token-to-css-var.json';
|
|
4
|
+
import type { Platform, Preset, ThemeOverride, TokenGroup } from './types';
|
|
5
|
+
import {
|
|
6
|
+
isSchemaVersionSupported,
|
|
7
|
+
presetPatchToUniwindMaps,
|
|
8
|
+
themeOverrideToUniwindMaps,
|
|
9
|
+
type CssVariableMaps,
|
|
10
|
+
type RuntimeMap,
|
|
11
|
+
} from './theming';
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// Constants
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
/** The build-time default preset. Apps ship with Poise baked into CSS. */
|
|
18
|
+
export const DEFAULT_PRESET: Preset = 'poise';
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Preset patch lookup
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
const PRESET_PATCHES: Record<Preset, TokenGroup | null> = {
|
|
25
|
+
poise: null,
|
|
26
|
+
prestige: prestigePatch,
|
|
27
|
+
pulse: pulsePatch,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
// Platform detection
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
|
|
34
|
+
function detectPlatform(): Platform {
|
|
35
|
+
try {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
37
|
+
const { Platform: RNPlatform } = require('react-native');
|
|
38
|
+
const os = RNPlatform.OS as string;
|
|
39
|
+
if (os === 'ios' || os === 'android') return os;
|
|
40
|
+
return 'web';
|
|
41
|
+
} catch {
|
|
42
|
+
return 'web';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
// Result type
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
export type ApplyThemeOverrideResult =
|
|
51
|
+
| { applied: true; light: Record<string, string>; dark: Record<string, string> }
|
|
52
|
+
| { applied: false; reason: 'unsupported_schema_version' | 'no_theme_changes' };
|
|
53
|
+
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// Options
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
export interface ApplyThemeOverrideOptions {
|
|
59
|
+
runtimeMap?: RuntimeMap;
|
|
60
|
+
runtimePlatform?: Platform;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
// Orchestrator
|
|
65
|
+
// ---------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Apply a `ThemeOverride` end-to-end: schema version gate, preset patch
|
|
69
|
+
* selection, palette generation, and `Uniwind.updateCSSVariables` calls.
|
|
70
|
+
*
|
|
71
|
+
* Encapsulates the full runtime theme application sequence so consuming
|
|
72
|
+
* apps don't need to orchestrate lower-level utilities or import Uniwind
|
|
73
|
+
* directly.
|
|
74
|
+
*/
|
|
75
|
+
export function applyThemeOverride(
|
|
76
|
+
override: ThemeOverride,
|
|
77
|
+
options: ApplyThemeOverrideOptions = {},
|
|
78
|
+
): ApplyThemeOverrideResult {
|
|
79
|
+
const { runtimeMap = defaultRuntimeMap, runtimePlatform = detectPlatform() } = options;
|
|
80
|
+
|
|
81
|
+
// --- Schema version gate ---
|
|
82
|
+
if (!isSchemaVersionSupported(override)) {
|
|
83
|
+
return { applied: false, reason: 'unsupported_schema_version' };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const basePreset = override.$extensions['com.forge.ui.themeOverride'].basePreset;
|
|
87
|
+
const hasInputs = override.inputs !== undefined && Object.keys(override.inputs).length > 0;
|
|
88
|
+
const hasOverrides =
|
|
89
|
+
override.overrides !== undefined && Object.keys(override.overrides).length > 0;
|
|
90
|
+
|
|
91
|
+
// --- Metadata-only check ---
|
|
92
|
+
const isDefaultPreset = basePreset === DEFAULT_PRESET;
|
|
93
|
+
if (isDefaultPreset && !hasInputs && !hasOverrides) {
|
|
94
|
+
return { applied: false, reason: 'no_theme_changes' };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// --- Preset patch step ---
|
|
98
|
+
let presetMaps: CssVariableMaps = { light: {}, dark: {} };
|
|
99
|
+
if (!isDefaultPreset) {
|
|
100
|
+
const patch = PRESET_PATCHES[basePreset];
|
|
101
|
+
if (patch) {
|
|
102
|
+
presetMaps = presetPatchToUniwindMaps(patch, runtimeMap, runtimePlatform);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// --- FI override step ---
|
|
107
|
+
let overrideMaps: CssVariableMaps = { light: {}, dark: {} };
|
|
108
|
+
if (hasInputs || hasOverrides) {
|
|
109
|
+
overrideMaps = themeOverrideToUniwindMaps(override, runtimeMap);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// --- Merge per mode (FI wins on collision) ---
|
|
113
|
+
const mergedLight = { ...presetMaps.light, ...overrideMaps.light };
|
|
114
|
+
const mergedDark = { ...presetMaps.dark, ...overrideMaps.dark };
|
|
115
|
+
|
|
116
|
+
// --- Non-default preset with no FI changes still needs patch application ---
|
|
117
|
+
if (Object.keys(mergedLight).length === 0 && Object.keys(mergedDark).length === 0) {
|
|
118
|
+
return { applied: false, reason: 'no_theme_changes' };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// --- Apply via Uniwind ---
|
|
122
|
+
try {
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
124
|
+
const { Uniwind } = require('uniwind');
|
|
125
|
+
if (Object.keys(mergedLight).length > 0) {
|
|
126
|
+
Uniwind.updateCSSVariables('light', mergedLight);
|
|
127
|
+
}
|
|
128
|
+
if (Object.keys(mergedDark).length > 0) {
|
|
129
|
+
Uniwind.updateCSSVariables('dark', mergedDark);
|
|
130
|
+
}
|
|
131
|
+
} catch {
|
|
132
|
+
throw new Error(
|
|
133
|
+
'applyThemeOverride requires "uniwind" to be installed. ' +
|
|
134
|
+
'Add it as a dependency in your app.',
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return { applied: true, light: mergedLight, dark: mergedDark };
|
|
139
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,73 +1,49 @@
|
|
|
1
1
|
// ---------------------------------------------------------------------------
|
|
2
|
-
// Types
|
|
2
|
+
// Types & Constants
|
|
3
3
|
// ---------------------------------------------------------------------------
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
5
|
+
export {
|
|
6
|
+
OVERRIDE_SCHEMA_VERSION,
|
|
7
|
+
SUPPORTED_OVERRIDE_SCHEMA_VERSIONS,
|
|
8
|
+
INPUT_TOKEN_MAP,
|
|
9
|
+
presetFonts,
|
|
10
|
+
} from './types';
|
|
11
|
+
export type {
|
|
12
|
+
TokenValue,
|
|
13
|
+
TokenGroup,
|
|
14
|
+
Preset,
|
|
15
|
+
ThemeMetadata,
|
|
16
|
+
ThemeOverrideMetadata,
|
|
17
|
+
ThemeObject,
|
|
18
|
+
ThemeOverride,
|
|
19
|
+
Mode,
|
|
20
|
+
Platform,
|
|
21
|
+
} from './types';
|
|
11
22
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** Theme metadata stored under `$extensions.com.candescent.theme`. */
|
|
18
|
-
export interface ThemeMetadata {
|
|
19
|
-
name: string;
|
|
20
|
-
preset: string;
|
|
21
|
-
schemaVersion: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** Override metadata stored under `$extensions.com.candescent.themeOverride`. */
|
|
25
|
-
export interface ThemeOverrideMetadata {
|
|
26
|
-
basePreset: string;
|
|
27
|
-
fiId: string;
|
|
28
|
-
fiName: string;
|
|
29
|
-
schemaVersion: string;
|
|
30
|
-
}
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Palette generation
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
31
26
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*
|
|
35
|
-
* Presets (Poise, Prestige, Pulse) are full theme objects. At runtime the
|
|
36
|
-
* build-time default preset is augmented by FI overrides via
|
|
37
|
-
* `applyThemeOverrides`.
|
|
38
|
-
*/
|
|
39
|
-
export type ThemeObject = TokenGroup & {
|
|
40
|
-
$extensions: {
|
|
41
|
-
'com.candescent.theme': ThemeMetadata;
|
|
42
|
-
[key: string]: unknown;
|
|
43
|
-
};
|
|
44
|
-
modes: {
|
|
45
|
-
light: TokenGroup;
|
|
46
|
-
dark: TokenGroup;
|
|
47
|
-
};
|
|
48
|
-
platform: {
|
|
49
|
-
web: TokenGroup;
|
|
50
|
-
ios: TokenGroup;
|
|
51
|
-
android: TokenGroup;
|
|
52
|
-
};
|
|
53
|
-
};
|
|
27
|
+
export { generateColorScale, generatePalettesFromInputs, deriveBaseColorKey } from './palette';
|
|
28
|
+
export type { PaletteCategory, PaletteScale, PaletteStep, PaletteTokenMap } from './palette';
|
|
54
29
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
*/
|
|
59
|
-
export type ThemeOverride = TokenGroup & {
|
|
60
|
-
$extensions?: {
|
|
61
|
-
'com.candescent.themeOverride'?: ThemeOverrideMetadata;
|
|
62
|
-
[key: string]: unknown;
|
|
63
|
-
};
|
|
64
|
-
};
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
// Theming utilities
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
65
33
|
|
|
66
|
-
export
|
|
67
|
-
|
|
34
|
+
export {
|
|
35
|
+
isSchemaVersionSupported,
|
|
36
|
+
presetPatchToUniwindMaps,
|
|
37
|
+
themeOverrideToUniwindMaps,
|
|
38
|
+
applyThemeOverride,
|
|
39
|
+
DEFAULT_PRESET,
|
|
40
|
+
} from './theming';
|
|
41
|
+
export type { CssVariableMaps, RuntimeMap } from './theming';
|
|
42
|
+
export type { ApplyThemeOverrideOptions, ApplyThemeOverrideResult } from './applyThemeOverride';
|
|
68
43
|
|
|
69
44
|
// ---------------------------------------------------------------------------
|
|
70
45
|
// Hooks
|
|
71
46
|
// ---------------------------------------------------------------------------
|
|
72
47
|
|
|
48
|
+
export { useForgeFonts } from './useForgeFonts';
|
|
73
49
|
export { useCdxFonts } from './useCdxFonts';
|
package/src/palette.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { BackgroundColor, Color, Theme, type CssColor } from '@adobe/leonardo-contrast-colors';
|
|
2
|
+
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// Types
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
/** Palette step keys produced by `generateColorScale`. */
|
|
8
|
+
export type PaletteStep =
|
|
9
|
+
| '50'
|
|
10
|
+
| '100'
|
|
11
|
+
| '200'
|
|
12
|
+
| '300'
|
|
13
|
+
| '400'
|
|
14
|
+
| '500'
|
|
15
|
+
| '600'
|
|
16
|
+
| '700'
|
|
17
|
+
| '800'
|
|
18
|
+
| '900'
|
|
19
|
+
| '950'
|
|
20
|
+
| 'input';
|
|
21
|
+
|
|
22
|
+
/** Token category that supports palette generation. */
|
|
23
|
+
export type PaletteCategory = 'brand' | 'accent' | 'base';
|
|
24
|
+
|
|
25
|
+
/** Map of palette step → resolved hex color. */
|
|
26
|
+
export type PaletteScale = Record<PaletteStep, string>;
|
|
27
|
+
|
|
28
|
+
/** Map of token dot-path → resolved hex color (e.g. `color.brand.500` → `#548cdc`). */
|
|
29
|
+
export type PaletteTokenMap = Record<string, string>;
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
// Constants
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
const REFERENCE_BACKGROUND = '#ffffff';
|
|
36
|
+
|
|
37
|
+
const PALETTE_STEPS = [
|
|
38
|
+
'50',
|
|
39
|
+
'100',
|
|
40
|
+
'200',
|
|
41
|
+
'300',
|
|
42
|
+
'400',
|
|
43
|
+
'500',
|
|
44
|
+
'600',
|
|
45
|
+
'700',
|
|
46
|
+
'800',
|
|
47
|
+
'900',
|
|
48
|
+
'950',
|
|
49
|
+
] as const;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Contrast ratios for each palette step against white (#ffffff).
|
|
53
|
+
*
|
|
54
|
+
* Step 700 targets WCAG AA for normal text (≥ 4.5:1).
|
|
55
|
+
*/
|
|
56
|
+
const CONTRAST_RATIOS: Record<(typeof PALETTE_STEPS)[number], number> = {
|
|
57
|
+
'50': 1.04,
|
|
58
|
+
'100': 1.16,
|
|
59
|
+
'200': 1.46,
|
|
60
|
+
'300': 1.85,
|
|
61
|
+
'400': 2.45,
|
|
62
|
+
'500': 3.4,
|
|
63
|
+
'600': 4.8,
|
|
64
|
+
'700': 6.4,
|
|
65
|
+
'800': 8.6,
|
|
66
|
+
'900': 12,
|
|
67
|
+
'950': 16.8,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const HEX_REGEX = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
71
|
+
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Validation
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Validate and normalise a hex color string.
|
|
78
|
+
*
|
|
79
|
+
* Accepts `#RGB` (4 chars) or `#RRGGBB` (7 chars). Throws a descriptive
|
|
80
|
+
* `TypeError` for any other input.
|
|
81
|
+
*
|
|
82
|
+
* @returns The normalised 7-character hex string (e.g. `#aabbcc`).
|
|
83
|
+
*/
|
|
84
|
+
function validateHex(hex: string): CssColor {
|
|
85
|
+
if (typeof hex !== 'string') {
|
|
86
|
+
throw new TypeError(`Invalid hex color: expected a string, received ${typeof hex}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!HEX_REGEX.test(hex)) {
|
|
90
|
+
throw new TypeError(
|
|
91
|
+
`Invalid hex color "${hex}": must be a 4-character (#RGB) or 7-character (#RRGGBB) hex string starting with "#"`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (hex.length === 4) {
|
|
96
|
+
const [, r, g, b] = hex;
|
|
97
|
+
return `#${r}${r}${g}${g}${b}${b}`.toLowerCase() as CssColor;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return hex.toLowerCase() as CssColor;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
// Core generation — wraps Leonardo behind a thin interface
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Generate an 11-step contrast-based color scale from a single hex color.
|
|
109
|
+
*
|
|
110
|
+
* Uses `@adobe/leonardo-contrast-colors` internally. The generation algorithm
|
|
111
|
+
* is wrapped behind this function so the underlying library is swappable
|
|
112
|
+
* without changing the public API.
|
|
113
|
+
*
|
|
114
|
+
* @param hex - A valid hex color string (`#RGB` or `#RRGGBB`).
|
|
115
|
+
* @param category - The token namespace (`'brand'` or `'accent'`).
|
|
116
|
+
* @returns A `PaletteTokenMap` keyed by token dot-paths
|
|
117
|
+
* (e.g. `"color.brand.50"` through `"color.brand.950"` plus `"color.brand.input"`).
|
|
118
|
+
*/
|
|
119
|
+
export function generateColorScale(hex: string, category: PaletteCategory): PaletteTokenMap {
|
|
120
|
+
const normalised = validateHex(hex);
|
|
121
|
+
|
|
122
|
+
const ratiosObject: Record<string, number> = {};
|
|
123
|
+
for (const step of PALETTE_STEPS) {
|
|
124
|
+
ratiosObject[step] = CONTRAST_RATIOS[step];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const color = new Color({
|
|
128
|
+
name: 'palette',
|
|
129
|
+
colorKeys: [normalised],
|
|
130
|
+
colorSpace: 'CAM02p',
|
|
131
|
+
ratios: ratiosObject,
|
|
132
|
+
smooth: true,
|
|
133
|
+
output: 'HEX',
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const bg = new BackgroundColor({
|
|
137
|
+
name: 'background',
|
|
138
|
+
colorKeys: [REFERENCE_BACKGROUND],
|
|
139
|
+
colorSpace: 'CAM02p',
|
|
140
|
+
smooth: true,
|
|
141
|
+
ratios: [],
|
|
142
|
+
output: 'HEX',
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const theme = new Theme({
|
|
146
|
+
colors: [color],
|
|
147
|
+
backgroundColor: bg,
|
|
148
|
+
lightness: 100,
|
|
149
|
+
contrast: 1,
|
|
150
|
+
saturation: 100,
|
|
151
|
+
output: 'HEX',
|
|
152
|
+
formula: 'wcag2',
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const pairs = theme.contrastColorPairs;
|
|
156
|
+
|
|
157
|
+
const tokenPrefix = `color.${category}`;
|
|
158
|
+
const result: PaletteTokenMap = {};
|
|
159
|
+
|
|
160
|
+
for (const step of PALETTE_STEPS) {
|
|
161
|
+
result[`${tokenPrefix}.${step}`] = pairs[step].toLowerCase();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
result[`${tokenPrefix}.input`] = normalised;
|
|
165
|
+
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
// Base color-key derivation
|
|
171
|
+
// ---------------------------------------------------------------------------
|
|
172
|
+
|
|
173
|
+
/** Saturation (0–1) applied to the brand hue to produce the base color key. */
|
|
174
|
+
const BASE_SATURATION = 0.05;
|
|
175
|
+
|
|
176
|
+
/** Convert a normalised hex string to HSL (`h` in degrees, `s`/`l` in 0–1). */
|
|
177
|
+
function hexToHsl(hex: CssColor): { h: number; s: number; l: number } {
|
|
178
|
+
const r = parseInt(hex.slice(1, 3), 16) / 255;
|
|
179
|
+
const g = parseInt(hex.slice(3, 5), 16) / 255;
|
|
180
|
+
const b = parseInt(hex.slice(5, 7), 16) / 255;
|
|
181
|
+
|
|
182
|
+
const max = Math.max(r, g, b);
|
|
183
|
+
const min = Math.min(r, g, b);
|
|
184
|
+
const delta = max - min;
|
|
185
|
+
const l = (max + min) / 2;
|
|
186
|
+
|
|
187
|
+
let h = 0;
|
|
188
|
+
let s = 0;
|
|
189
|
+
if (delta !== 0) {
|
|
190
|
+
s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
|
|
191
|
+
switch (max) {
|
|
192
|
+
case r:
|
|
193
|
+
h = (g - b) / delta + (g < b ? 6 : 0);
|
|
194
|
+
break;
|
|
195
|
+
case g:
|
|
196
|
+
h = (b - r) / delta + 2;
|
|
197
|
+
break;
|
|
198
|
+
default:
|
|
199
|
+
h = (r - g) / delta + 4;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
h *= 60;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { h, s, l };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Convert HSL (`h` in degrees, `s`/`l` in 0–1) to a normalised hex string. */
|
|
209
|
+
function hslToHex({ h, s, l }: { h: number; s: number; l: number }): CssColor {
|
|
210
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
211
|
+
const hp = (((h % 360) + 360) % 360) / 60;
|
|
212
|
+
const x = c * (1 - Math.abs((hp % 2) - 1));
|
|
213
|
+
|
|
214
|
+
let r = 0;
|
|
215
|
+
let g = 0;
|
|
216
|
+
let b = 0;
|
|
217
|
+
if (hp < 1) [r, g, b] = [c, x, 0];
|
|
218
|
+
else if (hp < 2) [r, g, b] = [x, c, 0];
|
|
219
|
+
else if (hp < 3) [r, g, b] = [0, c, x];
|
|
220
|
+
else if (hp < 4) [r, g, b] = [0, x, c];
|
|
221
|
+
else if (hp < 5) [r, g, b] = [x, 0, c];
|
|
222
|
+
else [r, g, b] = [c, 0, x];
|
|
223
|
+
|
|
224
|
+
const m = l - c / 2;
|
|
225
|
+
const toHex = (v: number) =>
|
|
226
|
+
Math.round((v + m) * 255)
|
|
227
|
+
.toString(16)
|
|
228
|
+
.padStart(2, '0');
|
|
229
|
+
|
|
230
|
+
return `#${toHex(r)}${toHex(g)}${toHex(b)}` as CssColor;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Derive the base palette color key from a brand color.
|
|
235
|
+
*
|
|
236
|
+
* The base scale shares the brand's hue and lightness but is nearly neutral:
|
|
237
|
+
* the brand color is converted to HSL and its saturation is lowered to
|
|
238
|
+
* {@link BASE_SATURATION} (5%). The resulting hex is the color key fed to
|
|
239
|
+
* {@link generateColorScale} for the `base` category.
|
|
240
|
+
*
|
|
241
|
+
* @param brandHex - A valid hex color string (`#RGB` or `#RRGGBB`).
|
|
242
|
+
* @returns The normalised hex color key for the base scale.
|
|
243
|
+
*/
|
|
244
|
+
export function deriveBaseColorKey(brandHex: string): string {
|
|
245
|
+
const { h, l } = hexToHsl(validateHex(brandHex));
|
|
246
|
+
return hslToHex({ h, s: BASE_SATURATION, l });
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
// Convenience wrapper
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Colour-input keys whose token path starts with `color.` — these trigger
|
|
255
|
+
* palette generation. Mirrors the colour entries from `INPUT_TOKEN_MAP`
|
|
256
|
+
* (defined in the package barrel) without creating a circular import.
|
|
257
|
+
*
|
|
258
|
+
* Keep in sync with `INPUT_TOKEN_MAP` in `./index.ts`.
|
|
259
|
+
*/
|
|
260
|
+
const COLOR_INPUT_ENTRIES: readonly (readonly [string, string])[] = [
|
|
261
|
+
['brandPrimary', 'color.brand'],
|
|
262
|
+
['accentPrimary', 'color.accent'],
|
|
263
|
+
['basePrimary', 'color.base'],
|
|
264
|
+
] as const;
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Generate palettes for all colour inputs in a theme override `inputs` object.
|
|
268
|
+
*
|
|
269
|
+
* Iterates over known colour-input keys that map to a `color.*` token
|
|
270
|
+
* namespace. For each matching key present in `inputs`, runs
|
|
271
|
+
* `generateColorScale` and merges the results into a single flat map of
|
|
272
|
+
* token dot-paths to hex values — ready for the S5 override application
|
|
273
|
+
* merge step.
|
|
274
|
+
*
|
|
275
|
+
* Non-colour inputs (e.g. `displayFont`) are silently skipped.
|
|
276
|
+
*
|
|
277
|
+
* @param inputs - The `inputs` object from a `ThemeOverride`.
|
|
278
|
+
* @returns A merged `Record<string, string>` of all generated token
|
|
279
|
+
* dot-paths to hex values.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* const inputs = { brandPrimary: '#0052cc', accentPrimary: '#FF8C42', displayFont: 'Poppins' };
|
|
284
|
+
* const result = generatePalettesFromInputs(inputs);
|
|
285
|
+
* // {
|
|
286
|
+
* // "color.brand.50": "#...", …, "color.brand.input": "#0052cc",
|
|
287
|
+
* // "color.accent.50": "#...", …, "color.accent.input": "#ff8c42"
|
|
288
|
+
* // }
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
291
|
+
export function generatePalettesFromInputs(
|
|
292
|
+
inputs: Record<string, string | number>,
|
|
293
|
+
): PaletteTokenMap {
|
|
294
|
+
const result: PaletteTokenMap = {};
|
|
295
|
+
|
|
296
|
+
for (const [inputKey, tokenPath] of COLOR_INPUT_ENTRIES) {
|
|
297
|
+
const value = inputs[inputKey];
|
|
298
|
+
if (typeof value !== 'string') continue;
|
|
299
|
+
|
|
300
|
+
const category = tokenPath.replace('color.', '') as PaletteCategory;
|
|
301
|
+
const scale = generateColorScale(value, category);
|
|
302
|
+
|
|
303
|
+
Object.assign(result, scale);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return result;
|
|
307
|
+
}
|