"use client"; import { ImageIcon, ImageUpIcon, Loader2Icon } from "lucide-react"; import { useRouter } from "next/navigation"; import { useRef, useState } from "react"; import { toast } from "sonner"; import { DotPainting } from "@/components/hero/dot-painting"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { type Backdrop, BACKDROP_MAX_CELLS } from "@/lib/backdrop"; /** * Turns an image into a brightness grid like the built-in painting: at most BACKDROP_MAX_CELLS on * the long side, transparency on white, contrast-stretched (2nd–98th percentile) to levels 0–f. */ async function toBackdrop(file: File): Promise { let img: ImageBitmap | HTMLCanvasElement = await createImageBitmap(file); const s = Math.min(1, BACKDROP_MAX_CELLS / Math.max(img.width, img.height)); const w = Math.max(8, Math.round(img.width * s)); const h = Math.max(8, Math.round(img.height * s)); // Halve in steps first: one big downscale skips most pixels and looks noisy. while (img.width > w * 2 && img.height > h * 2) { const half = document.createElement("canvas"); half.width = Math.round(img.width / 2); half.height = Math.round(img.height / 2); half.getContext("2d")!.drawImage(img, 0, 0, half.width, half.height); img = half; } const canvas = document.createElement("canvas"); canvas.width = w; canvas.height = h; const g = canvas.getContext("2d", { willReadFrequently: true })!; g.fillStyle = "#fff"; g.fillRect(0, 0, w, h); g.imageSmoothingQuality = "high"; g.drawImage(img, 0, 0, w, h); const px = g.getImageData(0, 0, w, h).data; const lum = new Float32Array(w * h); for (let i = 0; i < lum.length; i++) lum[i] = 0.2126 * px[i * 4] + 0.7152 * px[i * 4 + 1] + 0.0722 * px[i * 4 + 2]; const sorted = Float32Array.from(lum).sort(); const lo = sorted[Math.floor(sorted.length * 0.02)]; const hi = sorted[Math.ceil(sorted.length * 0.98) - 1]; const range = Math.max(1, hi - lo); let dots = ""; for (const l of lum) dots += Math.round(Math.min(1, Math.max(0, (l - lo) / range)) * 15).toString(16); return { w, h, dots, focus: [0.5, 0.5] }; } /** Upload a picture for the dot painting behind the page headers, pick its focus point, or go back to the built-in one. */ export function BackdropSettings({ initial }: { initial: Backdrop | null }) { const router = useRouter(); const [saved, setSaved] = useState(initial); // The picture being edited: the saved one, a new upload, or null (built-in painting). const [draft, setDraft] = useState(initial); const [reading, setReading] = useState(false); const [busy, setBusy] = useState(false); const input = useRef(null); const dirty = draft?.dots !== saved?.dots || draft?.focus[0] !== saved?.focus[0] || draft?.focus[1] !== saved?.focus[1]; async function pick(file: File | undefined) { if (!file) return; setReading(true); try { setDraft(await toBackdrop(file)); } catch { toast.error(`"${file.name}" couldn't be read as an image.`); } finally { setReading(false); if (input.current) input.current.value = ""; } } async function save(next: Backdrop | null) { setBusy(true); try { const res = await fetch("/api/settings/appearance", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ backdrop: next }), }); const body = await res.json().catch(() => ({})); if (!res.ok) return toast.error(body.error?.message ?? `Could not save (${res.status})`); setSaved(body.backdrop); setDraft(body.backdrop); toast.success(body.backdrop ? "Background picture saved" : "Back to The Creation of Adam"); router.refresh(); // the headers get the picture from the layout } finally { setBusy(false); } } return ( Background picture The dot matrix behind every page header. Upload your own picture and it is drawn in dots the same way, in the theme's colors. Only the dots are saved, not the image, and they apply for everyone using this Kaname. {draft ? (

Click the part that should stay in view when a header crops the picture.

) : (

Showing the built-in painting, Michelangelo's The Creation of Adam.

)}
void pick(e.target.files?.[0])} /> {dirty && ( <> )} {saved && !dirty && ( )}
); }