35013502/**3503* @param {sbsRGBA} color3504* @returns {string} - строка rgba() для css3505*/3506function t_animationSBS__stringifyColor(color) {3507const [r, g, b] = color.slice(0, 3).map(channel => Math.round(channel));3508return `rgba(${r},${g},${b},${parseFloat(color[3].toFixed(3))})`;3509}35103511/**3512* Смешивает цвета с premultiplied alpha, как это делает браузер в css-анимации.
* Каналы умножаются на прозрачность до смешивания и делятся на итоговую прозрачность после. * Без этого переход от transparent (rgba(0, 0, 0, 0)) к цвету шел бы через темные оттенки * * @param {sbsRGBA} from * @param {sbsRGBA} to * @param {number} percentage - доля смешивания от 0 до 1 * @returns {sbsRGBA} */function t_animationSBS__mixColors(from, to, percentage) { const alpha = from[3] + percentage * (to[3] - from[3]); const mixed = [0, 1, 2].map(i => { // у полностью прозрачного цвета каналы не важны, берем каналы конечного цвета if (!alpha) return to[i]; return (from[i] * from[3] + percentage * (to[i] * to[3] - from[i] * from[3])) / alpha; }); return [...mixed, alpha];}/** * Парсит цвет шага для cl и bg: сплошной цвет (см. t_animationSBS__parseColor) или градиент из пикера редактора * * @param {string | undefined} value * @returns {sbsRGBA | sbsGradient | null} - null, если цвета нет или его не удалось распарсить */function t_animationSBS__parsePaint(value) { return t_animationSBS__parseColor(value) || t_animationSBS__parseGradient(value);}/** * Проверяет, что цвет шага - градиент, а не сплошной цвет: сплошной цвет хранится массивом каналов * * @param {sbsRGBA | sbsGradient | null | undefined} paint * @returns {boolean} */function t_animationSBS__isGradientPaint(paint) { return Boolean(paint) && !Array.isArray(paint);}/** * Парсит один linear-, radial- или conic-gradient. Пикер отдает их в виде * 'linear-gradient(0.183turn,rgba(220,243,240,1) 0%,rgba(255,255,255,1) 100%)', * 'radial-gradient(ellipse 40% 60% at right 10px bottom 20%,rgba(0,0,0,1) 0%,rgba(226,0,0,0.77) 49%)', * 'conic-gradient(from 0.25turn at 30% 40%,rgba(0,0,0,1) 0%,rgba(226,0,0,1) 100%)'. * Несколько градиентов через запятую, направления в угол ('to top right') и стопы без цвета не поддерживаются * * @param {string | undefined} value * @returns {sbsGradient | null} */function t_animationSBS__parseGradient(value) { if (typeof value !== 'string') return null; const gradientStr = value.trim().toLowerCase(); if (t_animationSBS__splitBackgroundLayers(gradientStr).length !== 1) return null; const match = gradientStr.match(/^(linear|radial|conic)-gradient\((.*)\)$/); if (!match) return null; const args = t_animationSBS__splitBackgroundLayers(match[2]); // первый аргумент без цвета - направление, форма или центр градиента const settings = args.length && !t_animationSBS__parseGradientStop(args[0]) ? args.shift() : ''; const gradient = t_animationSBS__parseGradientSettings(match[1], settings); const stops = args.map(t_animationSBS__parseGradientStop); if (!gradient || stops.length < 2 || stops.some(stop => !stop)) return null; gradient.stops = t_animationSBS__fillStopPositions(stops); return gradient;}/** * Парсит первый аргумент градиента и определяет вид слоя (kind): linear, conic или radial-{форма}-{размер}, * где размер - ключевое слово или 'length', если он задан длиной. Внутри одного вида слой интерполируется, * между видами - кроссфейд, см. t_animationSBS__createPaintMeta * * @param {'linear' | 'radial' | 'conic'} type * @param {string} settings - направление linear, 'from <угол> at <позиция>' conic или '<форма> <размер> at <позиция>' radial * @returns {sbsGradient | null} - градиент без стопов или null, если аргумент не удалось распарсить */function t_animationSBS__parseGradientSettings(type, settings) { const center = { x: { p: 50, x: 0 }, y: { p: 50, x: 0 } }; if (type === 'linear') { const angle = settings ? t_animationSBS__parseGradientAngle(settings) : 180; if (angle === null || Number.isNaN(angle)) return null; return { type, kind: type, angle }; } const [shapeSettings, positionSettings] = settings.split(/\s*\bat\b\s*/); const position = positionSettings ? t_animationSBS__parseGradientPosition(positionSettings) : center; if (!position) return null; if (type === 'conic') { const from = shapeSettings.match(/^from\s+(\S+)$/);