diff --git a/ios/Stemify/Core/Settings.swift b/ios/Stemify/Core/Settings.swift index c88d49d..47c6e0f 100644 --- a/ios/Stemify/Core/Settings.swift +++ b/ios/Stemify/Core/Settings.swift @@ -107,7 +107,7 @@ final class Settings: @unchecked Sendable { private static let keys = [ "servers", "plexUrl", "plexToken", "plexUser", "plexMachineId", "musicSection", "musicSectionTitle", - "stemUrl", "stemToken", "plexAccountToken", "stemRoots", "theme", "accent", "albumColorNowPlaying", "highRefreshRate", + "stemUrl", "stemToken", "plexAccountToken", "stemRoots", "theme", "accent", "albumTheme", "albumColorNowPlaying", "highRefreshRate", "stemQuality", "cacheLimitGb", "downloadsWifiOnly", "downloadSort", "keepStemMix", "masterVolume", "tabLayout", "karaokeVocals", "karaokeSoftDb", "playbackSpeed", "stemsOnly", "noStems", "precacheCount", "hiddenNowPlaying", "presets", "presetOrder", "hiddenPresets", "lockedPreset", "webStations", "lockScreenMix", "swipeUpQueue", "acapella", "crossfadeSec", "sweetFades", "foldedArtLeft", ] @@ -219,6 +219,8 @@ final class Settings: @unchecked Sendable { /// Swipe up on Now Playing opens Playing Next. var swipeUpQueue: Bool { get { read(\.swipeUpQueue, "swipeUpQueue", true) } set { write(\.swipeUpQueue, "swipeUpQueue", newValue, true) } } var lockScreenMix: Bool { get { read(\.lockScreenMix, "lockScreenMix", false) } set { write(\.lockScreenMix, "lockScreenMix", newValue, false) } } + /// The whole app takes its accent and surface tint from the playing track's art. + var albumTheme: Bool { get { read(\.albumTheme, "albumTheme", false) } set { write(\.albumTheme, "albumTheme", newValue, false) } } var albumColorNowPlaying: Bool { get { read(\.albumColorNowPlaying, "albumColorNowPlaying", false) } set { write(\.albumColorNowPlaying, "albumColorNowPlaying", newValue, false) } } /// Folded iPhone Duo: Now Playing's art sits at the left instead of centred. var foldedArtLeft: Bool { get { read(\.foldedArtLeft, "foldedArtLeft", false) } set { write(\.foldedArtLeft, "foldedArtLeft", newValue, false) } } diff --git a/ios/Stemify/UI/RootView.swift b/ios/Stemify/UI/RootView.swift index 44f652b..8cd2d53 100644 --- a/ios/Stemify/UI/RootView.swift +++ b/ios/Stemify/UI/RootView.swift @@ -6,11 +6,13 @@ import UIKit /// (or the stems, queue and lyrics deck) on the other, and the tabs stay in the system's own bar. struct RootView: View { @State private var ui = Ui(overlays: Overlays()) + @State private var art: ArtHue? private var g: AppGraph { .shared } var body: some View { let s = g.settings - let p = Palette.of(s.theme, s.accent) + let base = Palette.of(s.theme, s.accent) + let p = s.albumTheme ? art.map { base.art($0, mode: s.theme) } ?? base : base bar .modifier(GlassMiniPlayerHost()) // Folded on the Duo the app stays upright (the window's trailing inset gives the Duo away). @@ -33,6 +35,11 @@ struct RootView: View { .environment(\.palette, p) .preferredColorScheme(s.theme == .White ? .light : .dark) .tint(p.accent) + .task(id: "\(g.playback.current?.track.thumb ?? "")|\(s.albumTheme)") { + guard s.albumTheme, let thumb = g.playback.current?.track.thumb else { art = nil; return } + let a = await artHue(thumb) + withAnimation(.easeInOut(duration: 0.4)) { art = a } + } } @Environment(\.verticalSizeClass) private var vSize diff --git a/ios/Stemify/UI/Screens/NowPlayingScreen.swift b/ios/Stemify/UI/Screens/NowPlayingScreen.swift index 0d3ce79..87634e5 100644 --- a/ios/Stemify/UI/Screens/NowPlayingScreen.swift +++ b/ios/Stemify/UI/Screens/NowPlayingScreen.swift @@ -33,7 +33,7 @@ private func albumColor(_ thumb: String?, dark: Bool) async -> Color? { } /// Roughly Palette's vibrant swatch: the average hue of the most saturated, mid-bright pixels. -private func vibrant(_ cg: CGImage) -> (CGFloat, CGFloat, CGFloat)? { +func vibrant(_ cg: CGImage) -> (CGFloat, CGFloat, CGFloat)? { let n = 24 var px = [UInt8](repeating: 0, count: n * n * 4) guard let ctx = CGContext(data: &px, width: n, height: n, bitsPerComponent: 8, bytesPerRow: n * 4, diff --git a/ios/Stemify/UI/Screens/SettingsScreens.swift b/ios/Stemify/UI/Screens/SettingsScreens.swift index f705fce..522d25f 100644 --- a/ios/Stemify/UI/Screens/SettingsScreens.swift +++ b/ios/Stemify/UI/Screens/SettingsScreens.swift @@ -1025,6 +1025,11 @@ struct AppearanceScreen: View { .padding(.vertical, 8) .themedRow() } header: { Header(text: "Accent") } + Section { + Toggle(isOn: $s.albumTheme) { Text("Match Album Art").foregroundStyle(c.label) } + .tint(Color(hex: 0x34C759)) + .themedRow() + } Section { Toggle(isOn: $s.albumColorNowPlaying) { Text("Album Colour Background").foregroundStyle(c.label) } .tint(Color(hex: 0x34C759)) diff --git a/ios/Stemify/UI/Theme.swift b/ios/Stemify/UI/Theme.swift index 9623cef..b59f618 100644 --- a/ios/Stemify/UI/Theme.swift +++ b/ios/Stemify/UI/Theme.swift @@ -1,4 +1,5 @@ import SwiftUI +import UIKit extension Color { init(hex: UInt32, alpha: Double = 1) { @@ -84,3 +85,44 @@ private struct ThemedRow: ViewModifier { content.listRowBackground(grouped ? p.cell : p.bg).listRowSeparatorTint(p.separator) } } + +// ---- album art theme ---------------------------------------------------------------------------- + +/// One hue taken from the album art (see vibrant in NowPlayingScreen). +struct ArtHue: Equatable { var h, s, v: CGFloat } + +@MainActor +func artHue(_ thumb: String?) async -> ArtHue? { + guard let url = AppGraph.shared.plex.artUrl(thumb, px: 120) else { return nil } + let img: UIImage? = if let c = ArtLoader.shared.cached(url) { c } else { await ArtLoader.shared.image(url) } + guard let cg = img?.cgImage else { return nil } + return await Task.detached { () -> ArtHue? in + guard let (h, s, v) = vibrant(cg), s >= 0.12 else { return nil } + return ArtHue(h: h, s: s, v: v) + }.value +} + +extension Palette { + /// The theme recoloured around the art's hue: lightly tinted surfaces and an accent in the art's colour. + /// Black keeps its true-black background for OLED. + func art(_ a: ArtHue, mode: ThemeMode) -> Palette { + var c = self + let s = min(a.s, 0.9), t = min(s * 0.5, 0.3) + func hsb(_ sat: CGFloat, _ b: CGFloat) -> Color { Color(hue: a.h, saturation: sat, brightness: b) } + switch mode { + case .White: + c.bg = hsb(t * 0.25, 1); c.groupedBg = hsb(t * 0.45, 0.965); c.cell = hsb(t * 0.2, 1) + c.fill = hsb(t * 0.5, 0.94); c.fillStrong = hsb(t * 0.6, 0.89); c.separator = hsb(t * 0.6, 0.86) + c.accent = hsb(max(s, 0.6), min(max(a.v, 0.5), 0.62)) + case .Dark: + c.bg = hsb(t, 0.11); c.groupedBg = c.bg; c.cell = hsb(t, 0.17) + c.fill = c.cell; c.fillStrong = hsb(t, 0.27); c.separator = hsb(t, 0.22) + c.accent = hsb(max(s, 0.5), max(a.v, 0.85)) + case .Black: + c.cell = hsb(t, 0.11); c.fill = c.cell; c.fillStrong = hsb(t, 0.22); c.separator = hsb(t, 0.17) + c.accent = hsb(max(s, 0.5), max(a.v, 0.85)) + } + c.placeholder = c.fill + return c + } +} diff --git a/web/app.js b/web/app.js index 18004b2..fa9a17d 100644 --- a/web/app.js +++ b/web/app.js @@ -28,8 +28,55 @@ const inkFor = hex => { const n = parseInt(hex.slice(1), 16), l = (0.299 * (n >> 16) + 0.587 * (n >> 8 & 255) + 0.114 * (n & 255)) / 255; return l > 0.6 ? "#141414" : "#ffffff"; }; +// Theme from the playing track's art: one hue taken from the art, laid over the chosen theme's lightness. +const hex = (h, s, l) => { + const f = n => { const k = (n + h * 12) % 12, c = l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1)); return Math.round(c * 255).toString(16).padStart(2, "0"); }; + return "#" + f(0) + f(8) + f(4); +}; +function artPalette([h, s0, v], base) { + const s = Math.min(s0, 0.9), tint = Math.min(s * 0.55, 0.32), clamp = (x, a, b) => Math.max(a, Math.min(b, x)); + if (base.light) return { bg: hex(h, tint, .955), panel: hex(h, tint, .985), raised: hex(h, tint, .9), line: hex(h, tint, .85), + text: hex(h, .2, .1), muted: hex(h, .08, .42), accent: hex(h, Math.max(s, .55), clamp(v * .45, .32, .42)), light: true }; + const black = base.bg === "#000000"; + return { bg: black ? "#000000" : hex(h, tint, .09), panel: black ? "#000000" : hex(h, tint, .06), raised: hex(h, tint, black ? .085 : .145), line: hex(h, tint, black ? .12 : .18), + text: hex(h, .15, .93), muted: hex(h, .1, .66), accent: hex(h, Math.max(s, .55), clamp(v * .7, .58, .7)) }; +} +// Roughly a vibrant swatch: the circular mean hue of the most colourful mid-bright pixels. +function vibrant(bm) { + const n = 24, cv = new OffscreenCanvas(n, n), cx = cv.getContext("2d", { willReadFrequently: true }); + cx.drawImage(bm, 0, 0, n, n); + const d = cx.getImageData(0, 0, n, n).data, px = []; + for (let i = 0; i < d.length; i += 4) { + const r = d[i] / 255, g = d[i + 1] / 255, b = d[i + 2] / 255, mx = Math.max(r, g, b), mn = Math.min(r, g, b), c = mx - mn; + const h = !c ? 0 : mx === r ? ((g - b) / c + 6) % 6 / 6 : mx === g ? ((b - r) / c + 2) / 6 : ((r - g) / c + 4) / 6, s = mx ? c / mx : 0; + px.push([h, s, mx, s * (1 - Math.abs(mx - 0.55) * 1.4)]); + } + const top = px.sort((a, b) => b[3] - a[3]).slice(0, px.length / 6); + let x = 0, y = 0, s = 0, v = 0; + for (const p of top) { x += Math.cos(p[0] * 2 * Math.PI); y += Math.sin(p[0] * 2 * Math.PI); s += p[1]; v += p[2]; } + return s / top.length < 0.12 ? null : [(Math.atan2(y, x) / (2 * Math.PI) + 1) % 1, s / top.length, v / top.length]; +} +const ArtTheme = { + url: "", hsv: null, seq: 0, cache: new Map(), + async set(url) { + if (!S.artTheme) url = ""; + if (url === this.url) return; + this.url = url; + const n = ++this.seq; + let hsv = url ? this.cache.get(url) : null; + if (url && !this.cache.has(url)) { + try { + const r = await fetch(url, { signal: AbortSignal.timeout(8000) }); + hsv = vibrant(await createImageBitmap(await r.blob())); + this.cache.set(url, hsv); + } catch { hsv = null; } + if (n !== this.seq) return; + } + this.hsv = hsv; applyTheme(); + }, +}; function applyTheme() { - const t = THEMES[S.theme] || THEMES.graphite, a = S.accent || t.accent; + const base = THEMES[S.theme] || THEMES.graphite, t = ArtTheme.hsv ? artPalette(ArtTheme.hsv, base) : base, a = ArtTheme.hsv ? t.accent : S.accent || t.accent; $("#theme").textContent = `:root{--bg:${t.bg};--panel:${t.panel};--raised:${t.raised};--line:${t.line};--text:${t.text};--muted:${t.muted};` + `--accent:${a};--accent-ink:${inkFor(a)};--glass:${t.panel}ee;--hover:${t.text}0d;--shadow:${t.light ? "#0003" : "#0009"};` + `--wave:${t.text}44;--wave-ghost:${t.text}14;color-scheme:${t.light ? "light" : "dark"}}`; @@ -174,7 +221,7 @@ const UI = { $("#np-title").textContent = st.name; $("#np-sub").textContent = "Web Radio"; $("#np-art").removeAttribute("src"); - $("#b-art").hidden = true; SideArt.fill(); + $("#b-art").hidden = true; SideArt.fill(); ArtTheme.set(""); document.title = `${st.name} · Stemify`; if ("mediaSession" in navigator) navigator.mediaSession.metadata = new MediaMetadata({ title: st.name, artist: "Web Radio" }); return; @@ -185,7 +232,7 @@ const UI = { $("#np-sub").textContent = t ? [t.artist, t.album].filter(Boolean).join(" · ") : ""; const art = artOf(t, 120); if (art) $("#np-art").src = art; else $("#np-art").removeAttribute("src"); - $("#b-art").hidden = !art; SideArt.fill(); + $("#b-art").hidden = !art; SideArt.fill(); ArtTheme.set(artOf(t, 64)); document.title = t ? `${t.title} · Stemify` : "Stemify"; if ("mediaSession" in navigator) { navigator.mediaSession.metadata = t ? new MediaMetadata({ @@ -1177,6 +1224,7 @@ function renderSettings() {
Theme
${Object.entries(THEMES).map(([k, t]) => ``).join("")}
${srow("Accent", `${S.accent ? `` : ""}`)} + ${stog("Match album art", sw(`id="arttheme"`, S.artTheme))} ${stog("Call the vocals-only preset “Acapella”", sw(`id="acapella"`, S.acapella))}

Player bar

${BAR.map(([k, l]) => stog(l, sw(`data-bar="${k}"`, S.bar[k]))).join("")}
@@ -1238,6 +1286,7 @@ function renderSettings() { const ahead = v => { S.precache = Math.max(0, Math.min(50, Math.round(+v || 0))); $("#precache").value = $("#precachen").value = S.precache; save(); }; $("#precache").oninput = ev => ahead(ev.target.value); $("#precachen").onchange = ev => ahead(ev.target.value); + $("#arttheme").onchange = ev => { S.artTheme = ev.target.checked; save(); ArtTheme.set(S.artTheme ? artOf(Q.cur(), 64) : ""); }; $("#acapella").onchange = ev => { S.acapella = ev.target.checked; save(); emit("presets"); emit("mix"); }; if ($("#forgetall")) $("#forgetall").onclick = () => { for (const k of Object.keys(songMixes)) forgetSongMix(k); again(); }; $("#kon").onchange = ev => setKaraoke(ev.target.checked); diff --git a/web/store.js b/web/store.js index 9f029b7..9ad4a63 100644 --- a/web/store.js +++ b/web/store.js @@ -24,7 +24,7 @@ export const S = Object.assign({ acapella: false, // spell "A Cappella" as "Acapella" bar: { speed: true, mixer: true, lyrics: true, queue: true }, // player bar buttons stations: [], // web radio: [{id, name, url}] - theme: "graphite", accent: "", css: "", sideArt: false, + theme: "graphite", accent: "", css: "", sideArt: false, artTheme: false, }, load(KEY) || {}); if (!S.clientId) S.clientId = uuid(); diff --git a/web/style.css b/web/style.css index ad18a5a..47ba761 100644 --- a/web/style.css +++ b/web/style.css @@ -8,7 +8,7 @@ --accent: #3a81ed; --accent-ink: #ffffff; --radius: 6px; - --player-h: 90px; + --player-h: 100px; --nav-w: 240px; --glass: #101010ee; --hover: #e7e7e70d; @@ -330,6 +330,7 @@ input[type=color]::-moz-color-swatch { border: 0; border-radius: 50%; } .q-item .x { color: var(--muted); font-size: 18px; padding: 0 4px; opacity: 0; } .q-item:hover .x { opacity: 1; } +body, #nav, #player, .sg { transition: background-color .5s; } #player { position: fixed; left: 0; right: 0; bottom: 0; height: var(--player-h); background: var(--bg); border-top: 1px solid rgba(50, 50, 50, .7); display: grid; grid-template-columns: minmax(180px, 1fr) minmax(320px, 1.6fr) minmax(180px, 1fr); align-items: center; gap: 20px; padding: 0 20px; } #player.empty .center, #player.empty .right, #player.empty #b-art { opacity: .35; pointer-events: none; } @@ -347,7 +348,7 @@ input[type=color]::-moz-color-swatch { border: 0; border-radius: 50%; } .np-text { min-width: 0; } #np-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #np-sub { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } -.center { display: grid; gap: 4px; } +.center { display: grid; gap: 10px; } .transport { display: flex; justify-content: center; align-items: center; gap: 20px; font-size: 18px; } .transport button { color: #c5c5c5; } .transport button:hover { color: #fff; } @@ -374,7 +375,7 @@ input[type=color]::-moz-color-swatch { border: 0; border-radius: 50%; } input[type=range] { accent-color: var(--accent); } @media (max-width: 860px) { - :root { --player-h: 150px; } + :root { --player-h: 162px; } #app { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); } #nav { flex-direction: row; overflow-x: auto; padding: 10px 12px; border-right: 0; border-bottom: 1px solid var(--line); align-items: center; } .brand { padding: 0 8px 0 4px; }