Files
JayuCloud/WeChatProgram/utils/theme.js
T

138 lines
4.5 KiB
JavaScript
Raw Normal View History

const HEX_COLOR_PATTERN = /^#[0-9a-fA-F]{6}$/
function normalizeHexColor(value) {
if (!value || typeof value !== 'string') {
return ''
}
const normalized = value.trim()
return HEX_COLOR_PATTERN.test(normalized) ? normalized.toUpperCase() : ''
}
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)
}
}
function toHex(value) {
return Math.min(255, Math.max(0, value)).toString(16).padStart(2, '0').toUpperCase()
}
function rgbToHex(rgb) {
return `#${toHex(rgb.r)}${toHex(rgb.g)}${toHex(rgb.b)}`
}
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)
})
}
function buildStyleVars(theme) {
const styleVars = [
`--theme-primary: ${theme.primaryColor};`,
`--theme-secondary: ${theme.secondaryColor};`,
`--theme-accent: ${theme.accentColor};`,
`--theme-page-bg: ${theme.pageBgColor};`,
`--theme-card-bg: ${theme.cardBgColor};`,
`--theme-border: ${theme.borderColor};`,
`--theme-primary-soft: ${theme.primarySoftColor};`,
`--theme-gradient: ${theme.gradient};`,
`--theme-gradient-soft: ${theme.softGradient};`
]
if (theme.backgroundImageUrl) {
styleVars.push(
`background-image: url("${theme.backgroundImageUrl}");`,
'background-size: cover;',
'background-position: center top;'
)
}
return styleVars.join(' ')
}
function resolveShopTheme(source) {
const themeSource = source || {}
const normalizedPrimaryColor = normalizeHexColor(themeSource.themePrimaryColor)
const primaryColor = normalizedPrimaryColor || DEFAULT_SHOP_THEME.primaryColor
const secondaryColor = normalizeHexColor(themeSource.themeSecondaryColor)
|| (normalizedPrimaryColor ? mixColor(primaryColor, '#FFFFFF', 0.68) : DEFAULT_SHOP_THEME.secondaryColor)
const accentColor = normalizeHexColor(themeSource.themeAccentColor)
|| (normalizedPrimaryColor ? mixColor(primaryColor, '#7A3F56', 0.38) : DEFAULT_SHOP_THEME.accentColor)
const pageBgColor = normalizeHexColor(themeSource.themePageBgColor) || DEFAULT_SHOP_THEME.pageBgColor
const cardBgColor = normalizeHexColor(themeSource.themeCardBgColor) || DEFAULT_SHOP_THEME.cardBgColor
const borderColor = mixColor(primaryColor, cardBgColor, 0.82)
const primarySoftColor = mixColor(primaryColor, cardBgColor, 0.88)
const gradient = `linear-gradient(135deg, ${primaryColor} 0%, ${secondaryColor} 100%)`
const softGradient = `linear-gradient(145deg, ${cardBgColor} 0%, ${mixColor(secondaryColor, pageBgColor, 0.55)} 58%, ${mixColor(primaryColor, pageBgColor, 0.82)} 100%)`
const backgroundImageFileId = themeSource.themeBackgroundImageFileId
? String(themeSource.themeBackgroundImageFileId).trim()
: ''
const backgroundImageUrl = themeSource.themeBackgroundImageUrl
? String(themeSource.themeBackgroundImageUrl).trim()
: ''
const theme = {
primaryColor,
secondaryColor,
accentColor,
pageBgColor,
cardBgColor,
borderColor,
primarySoftColor,
gradient,
softGradient,
backgroundImageFileId,
backgroundImageUrl
}
theme.styleVars = buildStyleVars(theme)
return theme
}
const DEFAULT_SHOP_THEME = {
primaryColor: '#C6597D',
secondaryColor: '#E9B7C7',
accentColor: '#9F4967',
pageBgColor: '#FFF7F9',
cardBgColor: '#FFFFFF',
borderColor: '#EFD8E0',
primarySoftColor: '#FBF1F4',
gradient: 'linear-gradient(135deg, #C6597D 0%, #E9B7C7 100%)',
softGradient: 'linear-gradient(145deg, #FFFFFF 0%, #FFF1F5 58%, #FFE8EF 100%)',
backgroundImageFileId: '',
backgroundImageUrl: ''
}
DEFAULT_SHOP_THEME.styleVars = buildStyleVars(DEFAULT_SHOP_THEME)
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
}