Files
JayuCloud/FD-Frontend/src/utils/shop-theme.ts
T

253 lines
11 KiB
TypeScript

/**
* Purpose: SHOP_THEME_CHANGED_EVENT supports shop-theme.ts by holding or executing the shop theme changed event page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const SHOP_THEME_CHANGED_EVENT = 'fingertip-designer-shop-theme-updated'
/**
* Purpose: ShopThemeSource supports shop-theme.ts by holding or executing the shop theme source page or API role.
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
*/
export interface ShopThemeSource {
themePrimaryColor?: string | null
themeSecondaryColor?: string | null
themeAccentColor?: string | null
themePageBgColor?: string | null
themeCardBgColor?: string | null
themeBackgroundImageFileId?: string | number | null
}
/**
* Purpose: ResolvedShopTheme supports shop-theme.ts by holding or executing the resolved shop theme page or API role.
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
*/
export interface ResolvedShopTheme {
primaryColor: string
secondaryColor: string
accentColor: string
tertiaryColor: string
neutralColor: string
pageBgColor: string
cardBgColor: string
textPrimary: string
textSecondary: string
borderColor: string
primarySoftColor: string
secondarySoftColor: string
primaryActionColor: string
gradient: string
softGradient: string
backgroundImageFileId?: string
cssVars: Record<string, string>
}
/**
* Purpose: HEX_COLOR_PATTERN supports shop-theme.ts by holding or executing the hex color pattern page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const HEX_COLOR_PATTERN = /^#[0-9a-fA-F]{6}$/
export const DEFAULT_SHOP_THEME: ResolvedShopTheme = resolveShopThemeSource()
/**
* Purpose: resolveShopTheme supports shop-theme.ts by holding or executing the resolve shop theme page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
export function resolveShopTheme(source?: ShopThemeSource | null): ResolvedShopTheme {
return resolveShopThemeSource(source)
}
/**
* Purpose: applyShopTheme supports shop-theme.ts by holding or executing the apply shop theme page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
export function applyShopTheme(theme: ResolvedShopTheme, backgroundImageUrl?: string | null) {
if (typeof document === 'undefined') {
return
}
const root = document.documentElement
Object.entries(theme.cssVars).forEach(([key, value]) => {
root.style.setProperty(key, value)
})
root.style.setProperty('--theme-background-image', backgroundImageUrl ? `url("${backgroundImageUrl}")` : 'none')
}
/**
* Purpose: notifyShopThemeChanged supports shop-theme.ts by holding or executing the notify shop theme changed page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
export function notifyShopThemeChanged() {
if (typeof window === 'undefined') {
return
}
window.dispatchEvent(new Event(SHOP_THEME_CHANGED_EVENT))
}
/**
* Purpose: resolveShopThemeSource supports shop-theme.ts by holding or executing the resolve shop theme source page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function resolveShopThemeSource(source?: ShopThemeSource | null): ResolvedShopTheme {
const normalizedPrimaryColor = normalizeHexColor(source?.themePrimaryColor)
const primaryColor = normalizedPrimaryColor || '#E8B4B8'
const secondaryColor = normalizeHexColor(source?.themeSecondaryColor)
|| (normalizedPrimaryColor ? mixColor(primaryColor, '#C5A059', 0.35) : '#C5A059')
const neutralColor = normalizeHexColor(source?.themeAccentColor)
|| (normalizedPrimaryColor ? mixColor('#4A443F', primaryColor, 0.14) : '#4A443F')
const accentColor = neutralColor
const tertiaryColor = normalizeHexColor(source?.themePageBgColor) || '#FDFBF9'
const pageBgColor = tertiaryColor
const cardBgColor = normalizeHexColor(source?.themeCardBgColor) || '#FFFFFF'
const textPrimary = readableTextColor(pageBgColor, neutralColor)
const textSecondary = textPrimary === neutralColor
? mixColor(neutralColor, cardBgColor, 0.18)
: mixColor('#FFFFFF', primaryColor, 0.3)
const borderColor = mixColor(primaryColor, cardBgColor, 0.72)
const primarySoftColor = mixColor(primaryColor, cardBgColor, 0.78)
const secondarySoftColor = mixColor(secondaryColor, pageBgColor, 0.66)
const primaryActionColor = mixColor(neutralColor, primaryColor, 0.28)
const gradient = `linear-gradient(135deg, ${primaryActionColor} 0%, ${secondaryColor} 100%)`
const softGradient = `linear-gradient(145deg, ${cardBgColor} 0%, ${secondarySoftColor} 58%, ${mixColor(primaryColor, pageBgColor, 0.62)} 100%)`
const backgroundImageFileId = source?.themeBackgroundImageFileId === undefined || source?.themeBackgroundImageFileId === null
? undefined
: String(source.themeBackgroundImageFileId).trim() || undefined
return {
primaryColor,
secondaryColor,
accentColor,
tertiaryColor,
neutralColor,
pageBgColor,
cardBgColor,
textPrimary,
textSecondary,
borderColor,
primarySoftColor,
secondarySoftColor,
primaryActionColor,
gradient,
softGradient,
backgroundImageFileId,
cssVars: {
'--primary-color': primaryColor,
'--theme-primary': primaryColor,
'--theme-secondary': secondaryColor,
'--theme-accent': accentColor,
'--theme-neutral': neutralColor,
'--theme-tertiary': tertiaryColor,
'--theme-primary-action': primaryActionColor,
'--theme-primary-soft': primarySoftColor,
'--theme-secondary-soft': secondarySoftColor,
'--theme-page-bg': pageBgColor,
'--theme-card-bg': cardBgColor,
'--theme-border': borderColor,
'--theme-gradient': gradient,
'--theme-gradient-soft': softGradient,
'--theme-button-shadow': `0 12px 28px ${rgba(primaryColor, 0.22)}`,
'--theme-focus-shadow': `0 0 0 3px ${rgba(primaryColor, 0.16)}`,
'--bg-color': pageBgColor,
'--secondary-bg-color': mixColor(secondaryColor, pageBgColor, 0.58),
'--surface-color': cardBgColor,
'--text-primary': textPrimary,
'--text-secondary': textSecondary
}
}
}
/**
* Purpose: normalizeHexColor supports shop-theme.ts by holding or executing the normalize hex color page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function normalizeHexColor(value?: string | null): string | undefined {
if (!value) {
return undefined
}
const normalized = value.trim()
return HEX_COLOR_PATTERN.test(normalized) ? normalized.toUpperCase() : undefined
}
/**
* Purpose: mixColor supports shop-theme.ts by holding or executing the mix color page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function mixColor(foreground: string, background: string, backgroundWeight: number): string {
const fg = hexToRgb(foreground)
const bg = hexToRgb(background)
const weight = clamp(backgroundWeight, 0, 1)
return rgbToHex({
r: Math.round(fg.r * (1 - weight) + bg.r * weight),
g: Math.round(fg.g * (1 - weight) + bg.g * weight),
b: Math.round(fg.b * (1 - weight) + bg.b * weight)
})
}
/**
* Purpose: readableTextColor supports shop-theme.ts by holding or executing the readable text color page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function readableTextColor(background: string, preferred: string): string {
return relativeLuminance(hexToRgb(background)) > 0.48 ? preferred : '#FFFFFF'
}
/**
* Purpose: rgba supports shop-theme.ts by holding or executing the rgba page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function rgba(color: string, alpha: number): string {
const rgb = hexToRgb(color)
return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${clamp(alpha, 0, 1)})`
}
/**
* Purpose: relativeLuminance supports shop-theme.ts by holding or executing the relative luminance page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function relativeLuminance(rgb: { r: number; g: number; b: number }): number {
const toLinearChannel = (value: number) => {
const channel = value / 255
return channel <= 0.03928 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4)
}
const r = toLinearChannel(rgb.r)
const g = toLinearChannel(rgb.g)
const b = toLinearChannel(rgb.b)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
}
/**
* Purpose: hexToRgb supports shop-theme.ts by holding or executing the hex to rgb page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function hexToRgb(color: string): { r: number; g: number; b: number } {
const value = color.replace('#', '')
return {
r: Number.parseInt(value.slice(0, 2), 16),
g: Number.parseInt(value.slice(2, 4), 16),
b: Number.parseInt(value.slice(4, 6), 16)
}
}
/**
* Purpose: rgbToHex supports shop-theme.ts by holding or executing the rgb to hex page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function rgbToHex(rgb: { r: number; g: number; b: number }): string {
return `#${toHex(rgb.r)}${toHex(rgb.g)}${toHex(rgb.b)}`
}
/**
* Purpose: toHex supports shop-theme.ts by holding or executing the to hex page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function toHex(value: number): string {
return clamp(value, 0, 255).toString(16).padStart(2, '0').toUpperCase()
}
/**
* Purpose: clamp supports shop-theme.ts by holding or executing the clamp page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function clamp(value: number, min: number, max: number): number {
return Math.min(max, Math.max(min, value))
}