nsfwapp/frontend/src/components/ui/formatters.ts
2026-02-20 18:18:59 +01:00

31 lines
989 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// frontend/src/components/ui/formatters.ts
export function formatResolution(r?: { w: number; h: number } | null): string {
if (!r) return ''
const w = Math.round(Number(r.w))
const h = Math.round(Number(r.h))
if (!Number.isFinite(w) || !Number.isFinite(h) || w <= 0 || h <= 0) return ''
// "p" orientiert sich an der kleineren Seite:
// - Landscape: min = Höhe
// - Portrait: min = Breite (damit 1080×1920 => 1080p)
const p = Math.min(w, h)
// Toleranz, weil Encoder manchmal krumme Werte liefern (z.B. 1072 statt 1080)
const tol = Math.max(12, Math.round(p * 0.02))
const pick = (target: number) => Math.abs(p - target) <= tol
if (pick(4320)) return '8K'
if (pick(2160) || p >= 2000) return '4K'
if (pick(1440)) return '1440p'
if (pick(1080)) return '1080p'
if (pick(720)) return '720p'
if (pick(480)) return '480p'
if (pick(360)) return '360p'
if (pick(240)) return '240p'
// Fallback: z.B. 1600p (Ultrawide) oder 800p
return `${p}p`
}