Spaces:
Sleeping
Sleeping
File size: 13,168 Bytes
3c56493 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | import { browser } from '$app/environment';
type ColorThiefModule = typeof import('colorthief');
type ColorThiefConstructor = ColorThiefModule extends { default: infer D }
? D extends new (...args: unknown[]) => unknown
? D
: ColorThiefModule extends new (...args: unknown[]) => unknown
? ColorThiefModule
: never
: ColorThiefModule extends new (...args: unknown[]) => unknown
? ColorThiefModule
: never;
type ColorThiefInstance = InstanceType<ColorThiefConstructor>;
export interface RGBColor {
red: number;
green: number;
blue: number;
}
export interface PaletteResult {
dominant: RGBColor;
accent: RGBColor;
palette: RGBColor[];
}
const DEFAULT_COLOR: RGBColor = { red: 15, green: 23, blue: 42 };
const clamp = (value: number, min = 0, max = 255) => Math.min(max, Math.max(min, value));
const mix = (color: RGBColor, target: RGBColor, factor: number): RGBColor => ({
red: clamp(Math.round(color.red + (target.red - color.red) * factor)),
green: clamp(Math.round(color.green + (target.green - color.green) * factor)),
blue: clamp(Math.round(color.blue + (target.blue - color.blue) * factor))
});
const distance = (a: RGBColor, b: RGBColor): number => {
const dr = a.red - b.red;
const dg = a.green - b.green;
const db = a.blue - b.blue;
return Math.sqrt(dr * dr + dg * dg + db * db);
};
const saturation = ({ red, green, blue }: RGBColor): number => {
const r = red / 255;
const g = green / 255;
const b = blue / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
if (max === min) return 0;
const l = (max + min) / 2;
const d = max - min;
return l > 0.5 ? d / (2 - max - min) : d / (max + min);
};
export const rgbToCss = ({ red, green, blue }: RGBColor, alpha?: number): string => {
const r = Math.round(clamp(red));
const g = Math.round(clamp(green));
const b = Math.round(clamp(blue));
if (alpha === undefined) {
return `rgb(${r}, ${g}, ${b})`;
}
const normalizedAlpha = Math.min(Math.max(alpha, 0), 1);
return `rgba(${r}, ${g}, ${b}, ${normalizedAlpha.toFixed(3)})`;
};
export const lighten = (color: RGBColor, amount: number): RGBColor => mix(color, { red: 255, green: 255, blue: 255 }, amount);
export const darken = (color: RGBColor, amount: number): RGBColor => mix(color, { red: 0, green: 0, blue: 0 }, amount);
/**
* Calculate relative luminance of a color (WCAG standard)
* Returns a value between 0 (black) and 1 (white)
*/
const getLuminance = ({ red, green, blue }: RGBColor): number => {
// Convert to sRGB
const rsRGB = red / 255;
const gsRGB = green / 255;
const bsRGB = blue / 255;
// Apply gamma correction
const r = rsRGB <= 0.03928 ? rsRGB / 12.92 : Math.pow((rsRGB + 0.055) / 1.055, 2.4);
const g = gsRGB <= 0.03928 ? gsRGB / 12.92 : Math.pow((gsRGB + 0.055) / 1.055, 2.4);
const b = bsRGB <= 0.03928 ? bsRGB / 12.92 : Math.pow((bsRGB + 0.055) / 1.055, 2.4);
// Calculate luminance
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
};
/**
* Calculate contrast ratio between two colors (WCAG standard)
* Returns a value between 1 and 21
*/
const getContrastRatio = (color1: RGBColor, color2: RGBColor): number => {
const lum1 = getLuminance(color1);
const lum2 = getLuminance(color2);
const lighter = Math.max(lum1, lum2);
const darker = Math.min(lum1, lum2);
return (lighter + 0.05) / (darker + 0.05);
};
/**
* Ensure a color has sufficient contrast against white and grey text
* Darkens the color if necessary to meet minimum contrast requirements
*/
export const ensureTextContrast = (color: RGBColor): RGBColor => {
const WHITE: RGBColor = { red: 255, green: 255, blue: 255 };
const GREY: RGBColor = { red: 156, green: 163, blue: 175 }; // ~gray-400
const MIN_CONTRAST_WHITE = 4.5; // WCAG AA standard for normal text
const MIN_CONTRAST_GREY = 3.0; // Slightly lower for grey text
let adjustedColor = { ...color };
let contrastWhite = getContrastRatio(adjustedColor, WHITE);
let contrastGrey = getContrastRatio(adjustedColor, GREY);
let attempts = 0;
const MAX_ATTEMPTS = 20;
// Darken the color until it meets contrast requirements
while ((contrastWhite < MIN_CONTRAST_WHITE || contrastGrey < MIN_CONTRAST_GREY) && attempts < MAX_ATTEMPTS) {
adjustedColor = darken(adjustedColor, 0.08);
contrastWhite = getContrastRatio(adjustedColor, WHITE);
contrastGrey = getContrastRatio(adjustedColor, GREY);
attempts++;
}
return adjustedColor;
};
const FALLBACK_RESULT: PaletteResult = {
dominant: DEFAULT_COLOR,
accent: { red: 30, green: 64, blue: 175 },
palette: [DEFAULT_COLOR]
};
const toRgbColor = (value: number[] | undefined | null): RGBColor => ({
red: clamp(Math.round(value?.[0] ?? DEFAULT_COLOR.red)),
green: clamp(Math.round(value?.[1] ?? DEFAULT_COLOR.green)),
blue: clamp(Math.round(value?.[2] ?? DEFAULT_COLOR.blue))
});
const selectAccentColor = (dominant: RGBColor, candidates: RGBColor[]): RGBColor => {
if (candidates.length === 0) {
return dominant;
}
const vivid = candidates.filter((color) => saturation(color) > 0.2);
const ranking = (vivid.length > 0 ? vivid : candidates).slice().sort((a, b) => distance(dominant, b) - distance(dominant, a));
return ranking[0] ?? dominant;
};
let colorThiefPromise: Promise<ColorThiefInstance | null> | null = null;
const loadColorThief = async (): Promise<ColorThiefInstance | null> => {
if (!browser) return null;
if (!colorThiefPromise) {
colorThiefPromise = (import('colorthief') as Promise<ColorThiefModule | { default: ColorThiefConstructor }>)
.then((module) => {
const ctor = (module as { default?: ColorThiefConstructor }).default ?? (module as ColorThiefConstructor);
return new ctor();
})
.catch((error) => {
console.warn('Failed to load Color Thief', error);
return null;
}) as Promise<ColorThiefInstance | null>;
}
return colorThiefPromise;
};
export async function extractPaletteFromImage(url: string, precision = 32): Promise<PaletteResult> {
if (!browser) {
return FALLBACK_RESULT;
}
const colorThief = await loadColorThief();
if (!colorThief) {
return FALLBACK_RESULT;
}
const image = new Image();
image.crossOrigin = 'anonymous';
image.decoding = 'async';
const loadPromise = new Promise<HTMLImageElement>((resolve, reject) => {
image.onload = () => resolve(image);
image.onerror = (event) => reject(event);
});
image.src = url;
let loadedImage: HTMLImageElement;
try {
loadedImage = await loadPromise;
} catch (error) {
console.warn('Failed to load image for palette extraction', error);
return FALLBACK_RESULT;
}
let paletteValues: number[][] = [];
let dominantValue: number[] | undefined;
const colorCount = 6;
const quality = Math.max(1, Math.round(precision / 4));
try {
paletteValues = colorThief.getPalette(loadedImage, colorCount, quality) ?? [];
dominantValue = paletteValues[0] ?? colorThief.getColor(loadedImage, quality);
} catch (error) {
console.warn('Color Thief failed to extract palette', error);
return FALLBACK_RESULT;
}
const paletteColors = paletteValues.map((value) => toRgbColor(value));
const dominant = toRgbColor(dominantValue);
const accent = selectAccentColor(dominant, paletteColors.slice(1));
return {
dominant,
accent,
palette: paletteColors.length > 0 ? paletteColors : [dominant]
};
}
/**
* WebGL-specific palette extraction that follows the YouLyPlus specification:
* - Extracts exactly 40 colors from an 8x5 grid
* - Calculates vibrancy (saturation + luminance weighting)
* - Returns the most vibrant color for lyrics highlighting
*/
export async function extractPaletteFromImageWebGL(url: string): Promise<{ palette: RGBColor[], mostVibrant: RGBColor }> {
if (!browser) {
const defaultPalette = Array(40).fill(DEFAULT_COLOR);
return { palette: defaultPalette, mostVibrant: DEFAULT_COLOR };
}
// Constants from specification
const STRETCHED_GRID_WIDTH = 32;
const STRETCHED_GRID_HEIGHT = 18;
const MASTER_PALETTE_TEX_WIDTH = 8;
const MASTER_PALETTE_TEX_HEIGHT = 5;
const MIN_LUMINANCE_THRESHOLD = 0.15;
// Load image
const image = new Image();
image.crossOrigin = 'anonymous';
image.decoding = 'async';
const loadPromise = new Promise<HTMLImageElement>((resolve, reject) => {
image.onload = () => resolve(image);
image.onerror = (event) => reject(event);
});
image.src = url;
let loadedImage: HTMLImageElement;
try {
loadedImage = await loadPromise;
} catch (error) {
console.warn('Failed to load image for WebGL palette extraction', error);
const defaultPalette = Array(40).fill(DEFAULT_COLOR);
return { palette: defaultPalette, mostVibrant: DEFAULT_COLOR };
}
// Create temporary canvas for color extraction
const tempCanvas = document.createElement('canvas');
tempCanvas.width = STRETCHED_GRID_WIDTH;
tempCanvas.height = STRETCHED_GRID_HEIGHT;
const tempCtx = tempCanvas.getContext('2d');
if (!tempCtx) {
const defaultPalette = Array(40).fill(DEFAULT_COLOR);
return { palette: defaultPalette, mostVibrant: DEFAULT_COLOR };
}
// Draw image to canvas
tempCtx.drawImage(loadedImage, 0, 0, STRETCHED_GRID_WIDTH, STRETCHED_GRID_HEIGHT);
// Extract colors from 8x5 grid
const cellW = STRETCHED_GRID_WIDTH / MASTER_PALETTE_TEX_WIDTH;
const cellH = STRETCHED_GRID_HEIGHT / MASTER_PALETTE_TEX_HEIGHT;
const palette: RGBColor[] = [];
for (let j = 0; j < MASTER_PALETTE_TEX_HEIGHT; j++) {
for (let i = 0; i < MASTER_PALETTE_TEX_WIDTH; i++) {
const x = Math.floor(i * cellW);
const y = Math.floor(j * cellH);
let color = getAverageColorWithVibrancy(tempCtx, x, y, Math.ceil(cellW), Math.ceil(cellH));
// Very aggressively tone down bright colors for better contrast
const lum = calculateLuminance(color);
if (lum > 0.3) {
// Reduce by up to 65% for very bright colors
const darkenAmount = (lum - 0.3) * 0.93;
const darkened = darken(color, darkenAmount);
color = { ...darkened, vibrancy: color.vibrancy } as RGBColor & { vibrancy: number };
}
palette.push(color);
}
}
// Find most vibrant color for lyrics (filtering out very dark colors)
const filteredPalette = palette.filter(color => calculateLuminance(color) > MIN_LUMINANCE_THRESHOLD);
let mostVibrant = filteredPalette.length > 0 ? filteredPalette[0] : palette[0];
for (const color of filteredPalette) {
if ((color as any).vibrancy > (mostVibrant as any).vibrancy) {
mostVibrant = color;
}
}
return { palette, mostVibrant };
}
/**
* Calculate saturation using HSV model (as per specification)
*/
const calculateSaturationHSV = (color: RGBColor): number => {
const r_norm = color.red / 255;
const g_norm = color.green / 255;
const b_norm = color.blue / 255;
const max = Math.max(r_norm, g_norm, b_norm);
const min = Math.min(r_norm, g_norm, b_norm);
const delta = max - min;
if (delta < 0.00001 || max < 0.00001) return 0;
return delta / max; // HSV saturation
};
/**
* Calculate relative luminance (ITU-R BT.709)
*/
const calculateLuminance = (color: RGBColor): number => {
const linearize = (v: number) => {
v /= 255;
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
};
const r = linearize(color.red);
const g = linearize(color.green);
const b = linearize(color.blue);
// ITU-R BT.709 weights
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
};
/**
* Calculate vibrancy: combination of saturation and mid-luminance preference
*/
const calculateVibrancy = (color: RGBColor): number => {
const sat = calculateSaturationHSV(color);
const lum = calculateLuminance(color);
// Favor mid-luminance colors (around 0.5)
const lumFactor = 1.0 - Math.abs(lum - 0.5) * 1.8;
// Vibrancy formula from specification
return (sat * 0.5) + (Math.max(0, lumFactor) * 0.5);
};
/**
* Extract average color from a region, with vibrancy-based selection
*/
const getAverageColorWithVibrancy = (ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number): RGBColor & { vibrancy: number } => {
const imageData = ctx.getImageData(x, y, w, h);
const data = imageData.data;
const samples: Array<RGBColor & { saturation: number; luminance: number; vibrancy: number }> = [];
let avgR = 0, avgG = 0, avgB = 0;
let count = 0;
// Sample pixels and calculate average
for (let i = 0; i < data.length; i += 4) {
const color = {
red: data[i],
green: data[i + 1],
blue: data[i + 2]
};
avgR += color.red;
avgG += color.green;
avgB += color.blue;
count++;
// Store some samples for vibrancy comparison
if (i % 16 === 0) { // Sample every 4th pixel
const saturation = calculateSaturationHSV(color);
const luminance = calculateLuminance(color);
const vibrancy = calculateVibrancy(color);
samples.push({ ...color, saturation, luminance, vibrancy });
}
}
const avgColor: RGBColor & { vibrancy: number } = {
red: Math.round(avgR / count),
green: Math.round(avgG / count),
blue: Math.round(avgB / count),
vibrancy: 0
};
avgColor.vibrancy = calculateVibrancy(avgColor);
// Select most vibrant sample if it's significantly better (20% threshold)
let best = avgColor;
for (const sample of samples) {
if (sample.vibrancy > best.vibrancy * 1.2) {
best = { ...sample };
}
}
return best;
};
|