You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.6 KiB

import { hasVisibleText } from './competitionBranding'
export const PARTICIPANT_DEFAULT_PRIMARY = '#052d62'
function primaryHexToRgbTriplet(hex: string): string | null {
const s = hex.trim().replace('#', '')
if (!/^[0-9a-f]{6}$/i.test(s)) return null
const r = parseInt(s.slice(0, 2), 16)
const g = parseInt(s.slice(2, 4), 16)
const b = parseInt(s.slice(4, 6), 16)
return `${r}, ${g}, ${b}`
}
/** 与选手报名端 MainLayout 一致:写入 body 主题色,覆盖 prototype.css 默认洋红 */
export function applyParticipantBodyTheme(themePrimary?: string): void {
if (typeof document === 'undefined') return
const custom = themePrimary?.trim() ?? ''
const theme = hasVisibleText(custom)
? /^(?:#|rgb|hsl)/i.test(custom)
? custom
: `#${custom.replace(/^#/, '')}`
: PARTICIPANT_DEFAULT_PRIMARY
document.body.style.setProperty('--primary', theme)
document.body.style.setProperty('--bs-primary', theme)
if (theme.startsWith('#')) {
document.body.style.setProperty('--primary-soft', `${theme}14`)
const rgb = primaryHexToRgbTriplet(theme)
if (rgb) document.body.style.setProperty('--bs-primary-rgb', rgb)
} else {
document.body.style.removeProperty('--primary-soft')
document.body.style.removeProperty('--bs-primary-rgb')
}
}
export function clearParticipantBodyTheme(): void {
if (typeof document === 'undefined') return
document.body.style.removeProperty('--primary')
document.body.style.removeProperty('--primary-soft')
document.body.style.removeProperty('--bs-primary')
document.body.style.removeProperty('--bs-primary-rgb')
}