diff --git a/admin/src/hooks/useStatsChartPalette.ts b/admin/src/hooks/useStatsChartPalette.ts new file mode 100644 index 000000000..650e80a45 --- /dev/null +++ b/admin/src/hooks/useStatsChartPalette.ts @@ -0,0 +1,85 @@ +import { onActivated, onDeactivated, onMounted, onScopeDispose, shallowRef } from 'vue' + +type RGB = [number, number, number] +const unpack = (hex: string): RGB => [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16)) as RGB +const pack = (c: number[]) => '#' + c.map(n => Math.round(n).toString(16).padStart(2, '0')).join('') +const mix = (a: string, b: string, weight: number) => { + const left = unpack(a), right = unpack(b) + return pack(left.map((v, i) => v + (right[i] - v) * weight)) +} +const luminance = (hex: string) => { + const [r, g, b] = unpack(hex).map(n => { + const x = n / 255 + return x <= 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4 + }) + return r * 0.2126 + g * 0.7152 + b * 0.0722 +} +const legible = (color: string, bg: string, ink: string, minimum: number) => { + const s = luminance(bg) + for (let i = 0; i <= 64; i++) { + const value = i ? mix(color, ink, i / 64) : color + const l = luminance(value) + if ((Math.max(l, s) + .05) / (Math.min(l, s) + .05) >= minimum) return value + } + return ink +} +function concrete(value: string, fallback: string) { + const str = value.trim() + if (/^#[\da-f]{6}$/i.test(str)) return str.toLowerCase() + if (/^#[\da-f]{3}$/i.test(str)) return '#' + [...str.slice(1)].map(c => c + c).join('').toLowerCase() + const rgb = str.match(/^rgb\(\s*(\d+)\s*[, ]\s*(\d+)\s*[, ]\s*(\d+)\s*\)$/i) + return rgb && rgb.slice(1).every(x => +x <= 255) ? pack(rgb.slice(1).map(Number)) : fallback +} + +// Canvas doesn't inherit CSS variables: resolve the active screen theme at the boundary. +export function statsChartPalette(read: (key: string) => string, dark: boolean) { + const get = (key: string, fallback: string) => concrete(read(key), fallback) + const surface = get('--zyt-surface', dark ? '#161618' : '#ffffff') + const ink = get('--zyt-ink', dark ? '#e3e3e8' : '#323251') + const text = get('--zyt-text', dark ? '#ababba' : '#4d5875') + const accent = get('--el-color-primary', '#5d87ff') + return { + dark, accent, surface, ink, text, + accentInk: legible(accent, surface, ink, 3), + accentText: legible(accent, surface, ink, 4.5), + muted: legible(get('--zyt-muted', text), surface, ink, 4.5), + soft: get('--zyt-accent-soft', mix(surface, accent, dark ? .19 : .10)), + grid: mix(surface, text, dark ? .20 : .14), + border: get('--zyt-border', mix(surface, text, .25)) + } +} + +export function useStatsChartPalette() { + const palette = shallowRef | null>(null) + let observer: MutationObserver | undefined + let paletteSheet: Element | null = null + function refresh() { + const root = document.documentElement + // Settings writes the accent into a stylesheet, not the root's inline style. + const sheet = document.getElementById('art-theme-vars') + if (sheet && sheet !== paletteSheet && observer) { + observer.observe(sheet, { childList: true }) + paletteSheet = sheet + } + if (root.dataset.adminUi !== 'v2') { palette.value = null; return } + const css = getComputedStyle(root) + const next = statsChartPalette(key => css.getPropertyValue(key), root.classList.contains('dark')) + const previous = palette.value + if (!previous || (Object.keys(next) as (keyof typeof next)[]).some(key => previous[key] !== next[key])) { + palette.value = next + } + } + function start() { + if (observer || typeof document === 'undefined') return + observer = new MutationObserver(refresh) + observer.observe(document.documentElement, { attributes: true, attributeFilter: ['style', 'class', 'data-admin-ui'] }) + observer.observe(document.head, { childList: true }) + refresh() + } + function stop() { observer?.disconnect(); observer = undefined; paletteSheet = null } + onMounted(start) + onActivated(start) + onDeactivated(stop) + onScopeDispose(stop) + return palette +} diff --git a/admin/src/views/stats/conversion/index.vue b/admin/src/views/stats/conversion/index.vue index 738c9b649..9c8dcc556 100755 --- a/admin/src/views/stats/conversion/index.vue +++ b/admin/src/views/stats/conversion/index.vue @@ -1,8 +1,12 @@