deckhearth/lib/frame-palette.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

/**
* Palette contract for card frames. Starter and custom frames share the
* same slots; CardFrame resolves a palette from these keys.
*/
export const PALETTE_SLOTS = [
{ key: 'outer', label: 'Card body' },
{ key: 'border', label: 'Frame border' },
{ key: 'titleBar', label: 'Title bar' },
{ key: 'typeBar', label: 'Type bar' },
{ key: 'textBox', label: 'Text box' },
{ key: 'artBacking', label: 'Art backing' },
{ key: 'text', label: 'Body text' },
{ key: 'titleText', label: 'Title text' },
{ key: 'accent', label: 'Accent' },
];
const HEX_RE = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
/**
* Validate/normalize a palette object. Returns { palette } with all nine
* slots as lowercase hex, or { error } when a slot is missing/invalid.
*/
export function validatePalette(input) {
if (!input || typeof input !== 'object' || Array.isArray(input)) {
return { error: 'Palette must be an object' };
}
const palette = {};
for (const { key } of PALETTE_SLOTS) {
const value = input[key];
if (typeof value !== 'string' || !HEX_RE.test(value.trim())) {
return { error: `Invalid or missing color for "${key}"` };
}
palette[key] = value.trim().toLowerCase();
}
return { palette };
}