Files
JayuCloud/JY-Artist-WeChat/utils/theme.js
T

202 lines
8.0 KiB
JavaScript
Raw Normal View History

2026-05-23 18:45:41 +08:00
/**
* Purpose: HEX_COLOR_PATTERN supports theme.js 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}$/
2026-05-23 18:45:41 +08:00
/**
* Purpose: normalizeHexColor supports theme.js 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) {
if (!value || typeof value !== 'string') {
return ''
}
const normalized = value.trim()
return HEX_COLOR_PATTERN.test(normalized) ? normalized.toUpperCase() : ''
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: hexToRgb supports theme.js 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) {
const value = color.replace('#', '')
return {
r: parseInt(value.slice(0, 2), 16),
g: parseInt(value.slice(2, 4), 16),
b: parseInt(value.slice(4, 6), 16)
}
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: toHex supports theme.js 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) {
return Math.min(255, Math.max(0, value)).toString(16).padStart(2, '0').toUpperCase()
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: rgbToHex supports theme.js 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) {
return `#${toHex(rgb.r)}${toHex(rgb.g)}${toHex(rgb.b)}`
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: mixColor supports theme.js 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, background, backgroundWeight) {
const fg = hexToRgb(foreground)
const bg = hexToRgb(background)
const weight = Math.min(1, Math.max(0, backgroundWeight))
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)
})
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: buildStyleVars supports theme.js by holding or executing the build style vars 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 buildStyleVars(theme) {
const styleVars = [
`--theme-primary: ${theme.primaryColor};`,
`--theme-secondary: ${theme.secondaryColor};`,
`--theme-accent: ${theme.accentColor};`,
2026-05-29 13:30:56 +08:00
`--theme-neutral: ${theme.neutralColor || theme.accentColor};`,
`--theme-tertiary: ${theme.tertiaryColor || theme.pageBgColor};`,
`--theme-primary-action: ${theme.primaryActionColor};`,
`--theme-page-bg: ${theme.pageBgColor};`,
`--theme-card-bg: ${theme.cardBgColor};`,
`--theme-border: ${theme.borderColor};`,
`--theme-primary-soft: ${theme.primarySoftColor};`,
2026-05-29 13:30:56 +08:00
`--theme-secondary-soft: ${theme.secondarySoftColor};`,
`--theme-gradient: ${theme.gradient};`,
2026-05-29 13:30:56 +08:00
`--theme-gradient-soft: ${theme.softGradient};`,
`--text-primary: ${theme.textPrimary};`,
`--text-secondary: ${theme.textSecondary};`
]
if (theme.backgroundImageUrl) {
styleVars.push(
`background-image: url("${theme.backgroundImageUrl}");`,
'background-size: cover;',
'background-position: center top;'
)
}
return styleVars.join(' ')
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: resolveShopTheme supports theme.js 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.
*/
function resolveShopTheme(source) {
const themeSource = source || {}
const normalizedPrimaryColor = normalizeHexColor(themeSource.themePrimaryColor)
const primaryColor = normalizedPrimaryColor || DEFAULT_SHOP_THEME.primaryColor
const secondaryColor = normalizeHexColor(themeSource.themeSecondaryColor)
2026-05-29 13:30:56 +08:00
|| (normalizedPrimaryColor ? mixColor(primaryColor, DEFAULT_SHOP_THEME.secondaryColor, 0.35) : DEFAULT_SHOP_THEME.secondaryColor)
const neutralColor = normalizeHexColor(themeSource.themeAccentColor)
|| (normalizedPrimaryColor ? mixColor(DEFAULT_SHOP_THEME.neutralColor, primaryColor, 0.14) : DEFAULT_SHOP_THEME.neutralColor)
const accentColor = neutralColor
const tertiaryColor = normalizeHexColor(themeSource.themePageBgColor) || DEFAULT_SHOP_THEME.tertiaryColor
const pageBgColor = tertiaryColor
const cardBgColor = normalizeHexColor(themeSource.themeCardBgColor) || DEFAULT_SHOP_THEME.cardBgColor
2026-05-29 13:30:56 +08:00
const textPrimary = neutralColor
const textSecondary = mixColor(neutralColor, cardBgColor, 0.18)
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 = themeSource.themeBackgroundImageFileId
? String(themeSource.themeBackgroundImageFileId).trim()
: ''
const backgroundImageUrl = themeSource.themeBackgroundImageUrl
? String(themeSource.themeBackgroundImageUrl).trim()
: ''
const theme = {
primaryColor,
secondaryColor,
accentColor,
2026-05-29 13:30:56 +08:00
tertiaryColor,
neutralColor,
pageBgColor,
cardBgColor,
2026-05-29 13:30:56 +08:00
textPrimary,
textSecondary,
borderColor,
primarySoftColor,
2026-05-29 13:30:56 +08:00
secondarySoftColor,
primaryActionColor,
gradient,
softGradient,
backgroundImageFileId,
backgroundImageUrl
}
theme.styleVars = buildStyleVars(theme)
return theme
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: DEFAULT_SHOP_THEME supports theme.js by holding or executing the default shop theme 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 DEFAULT_SHOP_THEME = {
2026-05-29 13:30:56 +08:00
primaryColor: '#E8B4B8',
secondaryColor: '#C5A059',
accentColor: '#4A443F',
tertiaryColor: '#FDFBF9',
neutralColor: '#4A443F',
pageBgColor: '#FDFBF9',
cardBgColor: '#FFFFFF',
2026-05-29 13:30:56 +08:00
textPrimary: '#4A443F',
textSecondary: '#6B6662',
borderColor: '#F9EAEC',
primarySoftColor: '#FAEFF0',
secondarySoftColor: '#EADCC2',
primaryActionColor: '#766361',
gradient: 'linear-gradient(135deg, #766361 0%, #C5A059 100%)',
softGradient: 'linear-gradient(145deg, #FFFFFF 0%, #EADCC2 58%, #F5E0E0 100%)',
backgroundImageFileId: '',
backgroundImageUrl: ''
}
DEFAULT_SHOP_THEME.styleVars = buildStyleVars(DEFAULT_SHOP_THEME)
2026-05-23 18:45:41 +08:00
/**
* Purpose: pageThemeData supports theme.js by holding or executing the page theme data 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 pageThemeData(theme, backgroundImageUrl) {
const resolved = theme || DEFAULT_SHOP_THEME
const themeData = backgroundImageUrl
? {
...resolved,
backgroundImageUrl: String(backgroundImageUrl).trim()
}
: resolved
if (backgroundImageUrl) {
themeData.styleVars = buildStyleVars(themeData)
}
return {
theme: themeData,
themeVars: themeData.styleVars,
navThemeColor: themeData.primaryColor,
navThemeBackground: themeData.pageBgColor
}
}
module.exports = {
DEFAULT_SHOP_THEME,
resolveShopTheme,
pageThemeData
}