// Erscheinungsbild: Live-Vorschau der Farben (gleiche Formeln wie app/branding.py)
(function () {
const form = document.getElementById('brand-form');
if (!form) return;
const $ = (id) => document.getElementById(id);
const rgb = (h) => { h = h.replace('#', ''); if (h.length === 3) h = h.split('').map(c => c + c).join('');
return [0, 2, 4].map(i => parseInt(h.substr(i, 2), 16)); };
const hex = (a) => '#' + a.map(c => Math.max(0, Math.min(255, Math.round(c))).toString(16).padStart(2, '0')).join('');
const mix = (a, b, t) => { const x = rgb(a), y = rgb(b); return hex(x.map((v, i) => v + (y[i] - v) * t)); };
const norm = (v) => { v = (v || '').trim().toLowerCase(); if (!v.startsWith('#')) v = '#' + v;
return /^#([0-9a-f]{3}|[0-9a-f]{6})$/.test(v) ? hex(rgb(v)) : null; };
const lum = (c) => { const f = (x) => { x /= 255; return x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); };
const [r, g, b] = rgb(c); return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b); };
const contrast = (a, b) => { const [x, y] = [lum(a), lum(b)].sort((p, q) => q - p); return (x + 0.05) / (y + 0.05); };
function palette(brand, side) {
const w = '#ffffff', k = '#000000';
return {
'--brand': brand, '--brand-dark': mix(brand, k, .18), '--brand-deep': mix(brand, k, .3),
'--brand-soft': mix(brand, w, .88), '--brand-tint': mix(brand, w, .94), '--brand-line': mix(brand, w, .72),
'--brand-ring': mix(brand, w, .5), '--side': side, '--side-2': mix(side, brand, .4),
'--side-ink': mix(side, w, .82), '--side-ink-2': mix(side, w, .88), '--side-muted': mix(side, w, .6),
'--side-label': mix(side, w, .5), '--side-dim': mix(side, w, .42), '--side-accent': mix(brand, w, .12),
'--side-focus': mix(brand, w, .35), '--side-sub': mix(side, w, .7),
};
}
function apply() {
const b = norm($('brand_color').value), s = norm($('side_color').value);
const warn = $('contrast-warn');
if (!b || !s) { warn.textContent = 'Bitte gültige Farben im Format #rrggbb angeben.'; warn.hidden = false; return; }
const msgs = [];
if (contrast(b, '#ffffff') < 3) msgs.push('Hauptfarbe zu hell: Weiße Schrift auf Knöpfen wäre kaum lesbar.');
if (contrast(s, '#ffffff') < 7) msgs.push('Seitenleiste zu hell: Die helle Menüschrift wäre kaum lesbar.');
warn.textContent = msgs.join(' '); warn.hidden = !msgs.length;
const root = document.documentElement.style;
for (const [k, v] of Object.entries(palette(b, s))) root.setProperty(k, v);
}
function bind(pick, text) {
pick.addEventListener('input', () => { text.value = pick.value; apply(); });
text.addEventListener('input', () => { const v = norm(text.value); if (v) pick.value = v; apply(); });
}
bind($('brand_pick'), $('brand_color'));
bind($('side_pick'), $('side_color'));
document.querySelectorAll('.preset').forEach(btn => btn.addEventListener('click', () => {
$('brand_color').value = $('brand_pick').value = btn.dataset.brand;
$('side_color').value = $('side_pick').value = btn.dataset.side;
apply();
}));
document.querySelectorAll('[data-preview-text]').forEach(inp => inp.addEventListener('input', () => {
const key = inp.dataset.previewText;
document.querySelectorAll(`[data-preview="${key}"]`).forEach(el => { el.textContent = inp.value || inp.placeholder; });
}));
const file = $('logo-file');
const original = $('logo-preview').src;
const setLogo = (src) => document.querySelectorAll('#logo-preview, [data-preview-logo]').forEach(i => { i.src = src; });
file.addEventListener('change', () => {
const f = file.files[0];
if (f) { setLogo(URL.createObjectURL(f)); if ($('logo-reset')) $('logo-reset').checked = false; }
else setLogo(original);
});
if ($('logo-reset')) $('logo-reset').addEventListener('change', (e) => {
setLogo(e.target.checked ? '/static/brand/logo.png' : original);
if (e.target.checked) file.value = '';
});
})();