// SPDX-License-Identifier: AGPL-3.0-only // Part of the Folia lyric stage port (see ./README.md): the settings of a look as a list a page can draw rows from. // The rows, their ranges, steps and defaults are those of Folia's own settings panels (v0.8.2: // src/components/visualizer/settingsPanels.tsx for 流光 / 云阶 / 倾诉, window/VisPlaygroundSettingsPanel.tsx for // type, opacity and the band at the foot, the theme editor for the four colours and the animation intensity); // the wording is Folia's (src/i18n/locales/zh-CN.ts). // // Changed from Folia: 心象 has no rows of its own in Folia's panel (its four settings arrive only with an imported // look), so the four here are worded and ranged for this port; the weight is a choice of the two weights the // host's typeface has, not a slider from 100 to 900; no rows for what this port does not draw. // // Plain data and pure helpers, no DOM: the page draws the rows on the server and binds them, and // scripts/test_music_look.mjs runs these. const toggle = (path, label, fallback, invert = false) => ({ kind: 'toggle', path, label, fallback, invert }); const range = (path, label, min, max, step, fallback, unit) => ({ kind: 'range', path, label, min, max, step, fallback, unit }); const choice = (path, label, fallback, options) => ({ kind: 'choice', path, label, fallback, options }); const colour = (path, label) => ({ kind: 'colour', path, label }); /** The lyric styles a look may name, by Folia's ids (./names.ts). */ export const LOOK_STYLES = Object.freeze([ 'classic', 'cadenza', 'partita', 'tilt', // Folia's own styles, run as carried (./original/): a look made with one of them puts it on stage; their own // settings are kept by Folia's panels, not in the look. 'fume', 'monet', 'cappella', 'claddagh', 'diorama', 'pendolo', 'sonnet', 'tempera', 'lumiere', 'nebbia', 'metaphor', 'hanabi', 'machi', ]); /** * The rows, in groups. A group with `style` belongs to that lyric style and shows only while it is the one on * stage. `$side` in a path stands for the side on show, `light` or `dark`. A toggle marked `invert` is on when the * field it keeps is false (the look stores "hidden", the row says "shown"). */ export const LOOK_GROUPS = Object.freeze([ { id: 'classic', title: '流光', style: 'classic', controls: [ toggle('tunings.classic.enableWordRotation', '逐字旋转', true), range('tunings.classic.breathingFloatMultiplier', '呼吸浮动范围', 0, 2, 0.05, 1, 'times'), range('tunings.classic.wordSpacing', '单词间距', 0, 2, 0.05, 0.7, 'times'), ] }, { id: 'cadenza', title: '心象', style: 'cadenza', controls: [ range('tunings.cadenza.fontScale', '字号', 0.8, 1.6, 0.02, 1.12, 'times'), range('tunings.cadenza.widthRatio', '行宽', 0.4, 1, 0.02, 0.72, 'percent'), range('tunings.cadenza.motionAmount', '动感', 0, 2, 0.05, 1, 'times'), range('tunings.cadenza.glowIntensity', '发光', 0, 2, 0.05, 1, 'times'), ] }, { id: 'partita', title: '云阶', style: 'partita', controls: [ toggle('tunings.partita.showGuideLines', '引导线', true), toggle('tunings.partita.useSemanticLayout', '语义排列', true), range('tunings.partita.staggerMin', '错位最小值', 0, 180, 5, 20, 'px'), range('tunings.partita.staggerMax', '错位最大值', 0, 180, 5, 100, 'px'), ] }, { id: 'tilt', title: '倾诉', style: 'tilt', controls: [ choice('tunings.tilt.colorScheme', '配色方案', 'default', [ { value: 'default', label: '双色1' }, { value: 'swap', label: '双色2' }, { value: 'accentAll', label: '单色1' }, { value: 'primaryAll', label: '单色2' }, ]), range('tunings.tilt.splitProbability', '分行概率', 0, 1, 0.05, 0.75, 'percent'), range('tunings.tilt.tiltStyleProbability', '斜体强调概率', 0, 1, 0.05, 0.35, 'percent'), ] }, { id: 'type', title: '文字', controls: [ range('fontScale', '字号', 0.85, 1.4, 0.05, 1, 'times'), choice('fontWeight', '字重', undefined, [ { value: undefined, label: '自动' }, { value: 400, label: '常规' }, { value: 500, label: '中等' }, ]), range('opacity', '整体透明度', 0.2, 1, 0.05, 1, 'percent'), ] }, { id: 'colour', title: '配色', controls: [ colour('theme.$side.backgroundColor', '背景'), colour('theme.$side.primaryColor', '主文本'), colour('theme.$side.accentColor', '强调色'), colour('theme.$side.secondaryColor', '辅助色'), choice('theme.$side.animationIntensity', '动画强度', 'normal', [ { value: 'calm', label: '平静' }, { value: 'normal', label: '标准' }, { value: 'chaotic', label: '混乱' }, ]), ] }, { id: 'backdrop', title: '背景', controls: [ toggle('hideShapes', '几何图形', false, true), toggle('hideVignette', '暗角', false, true), ] }, { id: 'band', title: '字幕带', controls: [ toggle('subtitle.hidden', '底部字幕层', false, true), range('subtitle.opacity', '字幕透明度', 0.2, 1, 0.05, 0.6, 'percent'), toggle('subtitle.background', '字幕背景', true), toggle('subtitle.blur', '下一句模糊', true), range('subtitle.scale', '字幕缩放', 0.85, 1.4, 0.05, 1, 'times'), ] }, ]); /** Every row, without its group. */ export const LOOK_CONTROLS = Object.freeze(LOOK_GROUPS.flatMap((group) => group.controls)); const SIDES = ['light', 'dark']; const COLOUR = /^(#[0-9a-fA-F]{3,8}|rgba?\([\d\s.,%/]+\))$/; const keysOf = (path, side) => path.replace('$side', side).split('.'); const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value); /** The value a look keeps at a row's path (`side`: which side `$side` stands for), or undefined. */ export function readLook(look, path, side = 'dark') { let node = look; for (const key of keysOf(path, side)) { if (!isObject(node)) return undefined; node = node[key]; } return node; } /** A copy of the look with the path set to the value; `undefined` takes the field out, and with it any object left empty. */ export function writeLook(look, path, value, side = 'dark') { const keys = keysOf(path, side); const copy = (node, depth) => { const next = isObject(node) ? { ...node } : {}; const key = keys[depth]; const child = depth === keys.length - 1 ? value : copy(next[key], depth + 1); if (child === undefined) delete next[key]; else next[key] = child; return Object.keys(next).length ? next : undefined; }; return copy(look, 0) ?? {}; } /** A value as the row may keep it: of the row's kind and inside its range, else undefined. */ export function cleanLookValue(control, value) { if (control.kind === 'toggle') return typeof value === 'boolean' ? value : undefined; if (control.kind === 'range') return typeof value === 'number' && Number.isFinite(value) ? Math.min(control.max, Math.max(control.min, value)) : undefined; if (control.kind === 'colour') return typeof value === 'string' && COLOUR.test(value.trim()) ? value.trim() : undefined; // A weight from elsewhere (a look made in Folia may ask for 700) settles on the nearer of the two the typeface has. if (control.path === 'fontWeight') return typeof value === 'number' && Number.isFinite(value) ? (value >= 450 ? 500 : 400) : undefined; return control.options.some((option) => option.value === value) ? value : undefined; } /** * A look with nothing in it but what the rows keep, each value checked: for anything read back from storage or * out of a pasted configuration. Also kept, checked the same way, are the three things only an imported look * carries: the lyric style it was made with, extra font families, and words given their own colour. */ export function sanitizeLook(input) { let look = {}; if (!isObject(input)) return look; for (const control of LOOK_CONTROLS) { for (const side of control.path.includes('$side') ? SIDES : SIDES.slice(1)) { const value = cleanLookValue(control, readLook(input, control.path, side)); if (value !== undefined) look = writeLook(look, control.path, value, side); } } if (LOOK_STYLES.includes(input.style)) look.style = input.style; const families = (Array.isArray(input.fontFamilies) ? input.fontFamilies : []) .filter((family) => typeof family === 'string' && family.trim() && family.length <= 80) .slice(0, 6) .map((family) => family.trim()); if (families.length) look.fontFamilies = families; for (const side of SIDES) { const words = readLook(input, 'theme.$side.wordColors', side); const kept = (Array.isArray(words) ? words : []) .filter((entry) => isObject(entry) && typeof entry.word === 'string' && entry.word.trim() && entry.word.length <= 80 && typeof entry.color === 'string' && COLOUR.test(entry.color.trim())) .slice(0, 64) .map((entry) => ({ word: entry.word.trim(), color: entry.color.trim() })); if (kept.length) look = writeLook(look, 'theme.$side.wordColors', kept, side); } return look; } const trim = (value, digits) => value.toFixed(digits).replace(/\.?0+$/, ''); /** A range row's value as the row prints it: 1.2×, 75%, 40px. */ export function formatLook(control, value) { if (control.unit === 'percent') return `${Math.round(value * 100)}%`; if (control.unit === 'px') return `${Math.round(value)}px`; return `${trim(value, 2)}×`; } const pair = (value) => Math.min(255, Math.max(0, Math.round(value))).toString(16).padStart(2, '0'); /** A colour as a colour field takes it, #rrggbb, from a hex colour or rgb()/rgba(); black when it is neither. */ export function toHexColour(value) { const text = String(value ?? '').trim(); const hex = /^#([0-9a-f]{3,8})$/i.exec(text)?.[1]; if (hex && (hex.length === 3 || hex.length === 4)) return `#${[...hex.slice(0, 3)].map((digit) => digit + digit).join('')}`.toLowerCase(); if (hex && (hex.length === 6 || hex.length === 8)) return `#${hex.slice(0, 6)}`.toLowerCase(); const parts = /^rgba?\(([^)]+)\)$/i.exec(text)?.[1].split(/[\s,/]+/).filter(Boolean); if (parts && parts.length >= 3) { const channels = parts.slice(0, 3).map((part) => (part.endsWith('%') ? (Number.parseFloat(part) / 100) * 255 : Number.parseFloat(part))); if (channels.every(Number.isFinite)) return `#${channels.map(pair).join('')}`; } return '#000000'; }