// SPDX-License-Identifier: AGPL-3.0-only // Ported from Folia (https://github.com/chthollyphile/folia-major, v0.8.2), src/utils/colorMix.ts and // resolveWordColor from src/components/visualizer/wordColoring.ts. import type { Theme } from './types'; const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value)); const mix = (from: number, to: number, amount: number) => from + (to - from) * amount; const FALLBACK_RGB = { r: 255, g: 255, b: 255 }; export type Channels = { r: number; g: number; b: number }; type ParsedColor = Channels | 'fallback' | 'passthrough'; const readColor = (normalizedColor: string): ParsedColor => { if (!normalizedColor) return 'fallback'; if (normalizedColor.startsWith('#')) { const hex = normalizedColor.slice(1); const parse = (value: string) => Number.parseInt(value, 16); if (/^[0-9a-fA-F]{3}$/.test(hex)) { return { r: parse(hex[0] + hex[0]), g: parse(hex[1] + hex[1]), b: parse(hex[2] + hex[2]) }; } if (/^[0-9a-fA-F]{6}$/.test(hex)) { return { r: parse(hex.slice(0, 2)), g: parse(hex.slice(2, 4)), b: parse(hex.slice(4, 6)) }; } return 'fallback'; } const rgbMatch = normalizedColor.match(/^rgba?\(([^)]+)\)$/); if (rgbMatch) { // Both the legacy `rgb(1, 2, 3)` and the space-separated `rgb(1 2 3 / .5)` the browser may hand back. const [r, g, b] = rgbMatch[1].split(/[\s,/]+/).filter(Boolean).slice(0, 3).map(part => Number.parseFloat(part)); if ([r, g, b].every(Number.isFinite)) return { r, g, b }; return 'fallback'; } return 'passthrough'; }; // Styles ask for the same few theme colours every frame, often once per glyph, so each string is read once. const parsedColors = new Map(); const PARSED_COLOR_LIMIT = 512; const parseColor = (color: string): { normalized: string; parsed: ParsedColor } => { const normalized = typeof color === 'string' ? color.trim() : ''; let parsed = parsedColors.get(normalized); if (parsed === undefined) { if (parsedColors.size >= PARSED_COLOR_LIMIT) parsedColors.clear(); parsed = readColor(normalized); parsedColors.set(normalized, parsed); } return { normalized, parsed }; }; const formatRgba = (channels: Channels, alpha: number) => ( `rgba(${Math.round(clamp(channels.r, 0, 255))}, ${Math.round(clamp(channels.g, 0, 255))}, ${Math.round(clamp(channels.b, 0, 255))}, ${alpha})` ); export const colorWithAlpha = (color: string, alpha: number) => { const normalizedAlpha = clamp(alpha, 0, 1); const { normalized, parsed } = parseColor(color); if (parsed === 'passthrough') return normalized; return formatRgba(parsed === 'fallback' ? FALLBACK_RGB : parsed, normalizedAlpha); }; /** The colour's channels, or null when it is not a hex or rgb() colour. */ export const parseColorChannels = (color: string): Channels | null => { const { parsed } = parseColor(color); return typeof parsed === 'string' ? null : { ...parsed }; }; export const mixColors = (from: string, to: string, amount: number, alpha = 1) => { const normalizedAmount = clamp(amount, 0, 1); const fromParsed = parseColor(from).parsed; const toParsed = parseColor(to).parsed; if (typeof fromParsed === 'string' || typeof toParsed === 'string') { return colorWithAlpha(normalizedAmount >= 0.5 ? to : from, alpha); } return `rgba(${Math.round(mix(fromParsed.r, toParsed.r, normalizedAmount))}, ${Math.round(mix(fromParsed.g, toParsed.g, normalizedAmount))}, ${Math.round(mix(fromParsed.b, toParsed.b, normalizedAmount))}, ${clamp(alpha, 0, 1)})`; }; const CJK = /[一-龥぀-ヿ가-힯]/; const normalizeWordColorToken = (text: string) => text.toLowerCase().replace(/[^\w]/g, ''); /** A word's highlight: the theme's colour for that keyword when it names one, else the fallback (the accent). */ export const resolveWordColor = (wordText: string, wordColors: Theme['wordColors'], fallbackColor: string): string => { if (!wordColors || wordColors.length === 0) return fallbackColor; const cleanCurrent = wordText.trim(); if (!cleanCurrent) return fallbackColor; const matched = wordColors.find(entry => { const target = typeof entry?.word === 'string' ? entry.word.trim() : ''; if (!target) return false; if (CJK.test(cleanCurrent)) return target.includes(cleanCurrent); const targetWords = target.split(/\s+/).map(normalizeWordColorToken).filter(Boolean); const normalizedCurrent = normalizeWordColorToken(cleanCurrent); return Boolean(normalizedCurrent) && targetWords.includes(normalizedCurrent); }); return (matched && typeof matched.color === 'string' && matched.color) || fallbackColor; };