fix(mobile): restore main artwork color algorithm

This commit is contained in:
joshuafhiggins committed 2026-11-09 17:39:50 -05:00 Verified
1 parent 6766711d58
commit af7d5cc51d
5 files changed
+277 -155

No files matched your search

+2 -2
View File
@@ -117,11 +117,11 @@ export default function PlayerScreen() {
currentSong && config
? albumCoverUrl(config.serverUrl, currentSong.album.id)
: ""
const { palette, artworkColor } = useCoverPalette(
const { palette } = useCoverPalette(
coverUri,
currentSong ? currentSong.album.cover != null : undefined
)
const colors = nowPlayingColors(palette, isTablet ? artworkColor : undefined)
const colors = nowPlayingColors(palette)
const background = useSharedValue(colors.background)
useEffect(() => {
+1 -27
View File
@@ -18,7 +18,6 @@ import { GestureDetector, Gesture } from "react-native-gesture-handler"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { useRouter } from "expo-router"
import { StatusBar } from "expo-status-bar"
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg"
import { AudioOutputButton } from "@/components/audio-output-button"
import { CoverImage } from "@/components/cover-image"
@@ -146,36 +145,11 @@ export function TabletNowPlaying({
colors={colors}
viewportHeight={queueHeight}
currentHeader={null}
fadeColor={`#${colors.background
.slice(1)
.match(/../g)!
.map((channel) =>
Math.round(parseInt(channel, 16) * 0.62)
.toString(16)
.padStart(2, "0")
)
.join("")}`}
/>
)
return (
<Animated.View style={[styles.root, backgroundStyle, dismissStyle]}>
<StatusBar style="light" />
<Svg
key={`${width}:${height}`}
viewBox={`0 0 ${width} ${height}`}
pointerEvents="none"
style={StyleSheet.absoluteFill}
width={width}
height={height}
>
<Defs>
<LinearGradient id="player-shade" x1="0" y1="0" x2="0" y2="1">
<Stop offset="0.25" stopColor="#000" stopOpacity={0} />
<Stop offset="1" stopColor="#000" stopOpacity={0.45} />
</LinearGradient>
</Defs>
<Rect width={width} height={height} fill="url(#player-shade)" />
</Svg>
<StatusBar style={colors.isDark ? "light" : "dark"} />
<GestureDetector gesture={dismissGesture()}>
<Pressable
onPress={() => router.back()}
+29 -14
View File
@@ -33,14 +33,31 @@ function present(colors: (string | undefined)[]) {
function toSwatches(result: ImageColorsResult): CoverSwatches | null {
if (result.platform === "ios") {
const text = present([result.primary, result.secondary, result.detail])
return result.background.toLowerCase() === MISSING
? null
: { background: result.background }
: { background: result.background, text, accent: text }
}
const background = present([result.dominant, result.muted])[0]
if (!background) return null
return { background }
return {
background,
text: present([
result.lightVibrant,
result.lightMuted,
result.darkVibrant,
result.darkMuted,
result.vibrant,
result.muted,
]),
accent: present([
result.vibrant,
result.lightVibrant,
result.darkVibrant,
result.muted,
]),
}
}
function sameSwatches(
@@ -52,7 +69,11 @@ function sameSwatches(
function cachedSwatches(key: string): CoverSwatches | null | undefined {
if (swatchesByKey.has(key)) return swatchesByKey.get(key)
return readCache<CoverSwatches>(cacheKey(key))
const cached = readCache<CoverSwatches>(cacheKey(key))
// Discard the background-only entries written by the tablet palette experiment.
return cached && (!Array.isArray(cached.text) || !Array.isArray(cached.accent))
? undefined
: cached
}
function extractSwatches(key: string) {
@@ -96,7 +117,7 @@ export function prefetchCoverPalette(uri: string) {
export function useCoverPalette(
uri: string,
hasCover: boolean | undefined
): { palette: CoverPalette; settled: boolean; artworkColor?: string } {
): { palette: CoverPalette; settled: boolean } {
const theme = useThemeColors()
const isDark = useIsDark()
const key = uri ? withCoverImageSize(uri, 64) : ""
@@ -144,20 +165,14 @@ export function useCoverPalette(
const palette = useMemo(() => {
const fromArt =
hasCover !== false && swatches ? deriveCoverPalette(swatches) : null
if (fromArt) return fromArt
const fallback = themeCoverPalette(theme, isDark)
return {
...fallback,
accent: fallback.foreground,
accentForeground: fallback.background,
accentText: fallback.foreground,
}
hasCover !== false && swatches
? deriveCoverPalette(swatches, isDark)
: null
return fromArt ?? themeCoverPalette(theme, isDark)
}, [hasCover, isDark, swatches, theme])
return {
palette,
artworkColor: swatches?.background,
settled:
!key || hasCover === false || swatches !== undefined || waitedKey === key,
}
+240 -90
View File
@@ -17,16 +17,33 @@ export type CoverPalette = {
isDark: boolean
}
/** Dominant artwork color, cached before deriving the screen palette. */
export type CoverSwatches = { background: string }
/**
* Raw colors sampled from the art. `background` is the color the art bleeds
* into; `text` is ordered by preference and `accent` holds candidates for the
* Play button.
*/
export type CoverSwatches = {
background: string
text: string[]
accent: string[]
}
// Best effort: `legibleBackground` only guarantees that white or black reaches
// 7:1, so on some backgrounds primary text settles for pure white or black.
const PRIMARY_MIN_CONTRAST = 10
const SECONDARY_MIN_CONTRAST = 4.5
const SECONDARY_TO_PRIMARY_CONTRAST = 0.62
const ACCENT_MIN_CONTRAST = 3
const ACCENT_MIN_CHROMA = 0.05
const ACCENT_MAX_LIGHTNESS_SHIFT = 0.25
// Backgrounds between these luminances can't reach 7:1 with white or black.
const DARK_BACKGROUND_MAX_LUMINANCE = 0.095
const LIGHT_BACKGROUND_MIN_LUMINANCE = 0.31
type Rgb = [number, number, number]
type Hsl = { h: number; s: number; l: number }
type Oklch = { l: number; c: number; h: number }
export function themeCoverPalette(
theme: ThemeColors,
isDark: boolean
): CoverPalette {
export function themeCoverPalette(theme: ThemeColors, isDark: boolean): CoverPalette {
return {
background: theme.background,
foreground: theme.foreground,
@@ -40,107 +57,240 @@ export function themeCoverPalette(
}
}
/**
* Artwork pages use a dark, hue-matched canvas with neutral primary controls.
* Saturated covers stay deep; muted covers retain the lighter, subtly tinted
* canvas in the references: yellow #3E2B00, muted green #2A3028, and
* deep magenta #370C18.
*/
export function deriveCoverPalette(
swatches: CoverSwatches
swatches: CoverSwatches,
schemeIsDark: boolean
): CoverPalette | null {
const source = parseHex(swatches.background)
if (!source) return null
const sampled = toHsl(source)
// Yellow loses its green cast when shaded to the warm amber in the reference.
const muted = Math.max(0, 1 - sampled.s / 0.35)
const warmHue =
sampled.h >= 45 && sampled.h <= 75 ? 42 + (sampled.h - 53) * 0.5 : sampled.h
// Deep magenta shadows need a burgundy canvas rather than near-black.
// Blend by hue and shadow depth so bright covers keep their own treatment.
const berry =
Math.max(0, 1 - Math.abs(sampled.h - 320) / 40) *
Math.min(1, Math.max(0, (0.12 - sampled.l) / 0.08))
const h = warmHue - 16 * muted + 23.25 * berry
const s = Math.min(1, sampled.s * (1.2 + 2.7 * muted)) * (1 - 0.36 * berry)
const shadedLightness = Math.min(sampled.l, (31 + 14 * muted) / 255)
const l = shadedLightness * (1 - berry) + (33.5 / 255) * berry
const background = fromHsl({ h, s, l })
const secondary = fromHsl({
h: h - 2.5 + 8 * muted + 3 * berry,
s: s * (0.34 + 0.06 * muted + 0.024 * berry),
l: (174.5 - 2.5 * muted - 13.5 * berry) / 255,
})
const background = legibleBackground(source, schemeIsDark)
const lightText = prefersLightText(background)
const parsedText = swatches.text
.map(parseHex)
.filter((color): color is Rgb => color !== null)
const backgroundLuminance = luminance(background)
const sameSideText = parsedText.filter((color) =>
lightText
? luminance(color) > backgroundLuminance
: luminance(color) < backgroundLuminance
)
const textCandidates = sameSideText.length > 0 ? sameSideText : parsedText
const fallbackText: Rgb = lightText ? [255, 255, 255] : [0, 0, 0]
const foreground = withMinContrast(
textCandidates[0] ?? fallbackText,
background,
PRIMARY_MIN_CONTRAST
)
const primaryContrast = contrastRatio(foreground, background)
const secondaryTarget = Math.max(
SECONDARY_MIN_CONTRAST,
primaryContrast * SECONDARY_TO_PRIMARY_CONTRAST
)
const secondaryForeground = softenTo(
withMinContrast(
textCandidates[1] ?? foreground,
background,
SECONDARY_MIN_CONTRAST
),
background,
secondaryTarget
)
const accent = pickAccent(swatches.accent, background) ?? foreground
const accentForeground: Rgb = prefersLightText(accent)
? [255, 255, 255]
: [0, 0, 0]
return {
background: toHex(background),
foreground: "#FFFFFF",
secondaryForeground: toHex(secondary),
accent: "#FFFFFF",
accentForeground: toHex(background),
accentText: "#FFFFFF",
surface: toHex(
fromHsl({
h: h - 0.9 + 3 * berry,
s: s * (0.6 + 0.1 * muted),
l: l + (19 - muted) / 255,
})
),
divider: toHex(
fromHsl({
h,
s: s * (0.48 + 0.1 * muted + 0.025 * berry),
l: l + 33 / 255,
})
),
isDark: true,
foreground: toHex(foreground),
secondaryForeground: toHex(secondaryForeground),
accent: toHex(accent),
accentForeground: toHex(accentForeground),
accentText: toHex(withMinContrast(accent, background, SECONDARY_MIN_CONTRAST)),
surface: toHex(mix(foreground, background, lightText ? 0.16 : 0.1)),
divider: toHex(mix(foreground, background, lightText ? 0.2 : 0.14)),
isDark: lightText,
}
}
function toHsl([red, green, blue]: Rgb): Hsl {
const [r, g, b] = [red, green, blue].map((channel) => channel / 255)
const max = Math.max(r, g, b)
const min = Math.min(r, g, b)
const delta = max - min
const l = (max + min) / 2
if (delta === 0) return { h: 0, s: 0, l }
const sector =
max === r
? (g - b) / delta
: max === g
? (b - r) / delta + 2
: (r - g) / delta + 4
return {
h: (sector * 60 + 360) % 360,
s: delta / (1 - Math.abs(2 * l - 1)),
l,
function legibleBackground(color: Rgb, schemeIsDark: boolean): Rgb {
const lch = toOklch(color)
// Keep the art's hue but avoid a glaring page in dark mode or a murky one in
// light mode.
const clamped = schemeIsDark
? withLightness(color, Math.min(lch.l, 0.86))
: withLightness(color, Math.max(lch.l, 0.2))
const y = luminance(clamped)
if (y <= DARK_BACKGROUND_MAX_LUMINANCE || y >= LIGHT_BACKGROUND_MIN_LUMINANCE) {
return clamped
}
const pivot = schemeIsDark ? 0.28 : 0.14
return y < pivot
? withLuminance(clamped, DARK_BACKGROUND_MAX_LUMINANCE)
: withLuminance(clamped, LIGHT_BACKGROUND_MIN_LUMINANCE)
}
function fromHsl({ h, s, l }: Hsl): Rgb {
const hue = (((h % 360) + 360) % 360) / 60
const chroma = (1 - Math.abs(2 * l - 1)) * s
const x = chroma * (1 - Math.abs((hue % 2) - 1))
const sectors = [
[chroma, x, 0],
[x, chroma, 0],
[0, chroma, x],
[0, x, chroma],
[x, 0, chroma],
[chroma, 0, x],
]
const offset = l - chroma / 2
return sectors[Math.floor(hue)].map((channel) =>
Math.round((channel + offset) * 255)
function pickAccent(candidates: string[], background: Rgb): Rgb | null {
let best: Rgb | null = null
let bestChroma = ACCENT_MIN_CHROMA
for (const hex of candidates) {
const color = parseHex(hex)
if (!color) continue
const adjusted = withMinContrast(color, background, ACCENT_MIN_CONTRAST)
const before = toOklch(color)
const after = toOklch(adjusted)
// A color pushed far from its own lightness (yellow turned olive) no
// longer reads as coming from the art.
if (Math.abs(after.l - before.l) > ACCENT_MAX_LIGHTNESS_SHIFT) continue
if (after.c > bestChroma) {
best = adjusted
bestChroma = after.c
}
}
return best
}
function prefersLightText(background: Rgb) {
return contrastRatio([255, 255, 255], background) >= contrastRatio([0, 0, 0], background)
}
/** Moves `color`'s lightness away from `background` until it reaches `min`. */
function withMinContrast(color: Rgb, background: Rgb, min: number): Rgb {
if (contrastRatio(color, background) >= min) return color
const lighten = prefersLightText(background)
const start = toOklch(color).l
const extreme = lighten ? 1 : 0
if (contrastRatio(withLightness(color, extreme), background) < min) {
return lighten ? [255, 255, 255] : [0, 0, 0]
}
let near = start
let far = extreme
for (let i = 0; i < 16; i++) {
const mid = (near + far) / 2
if (contrastRatio(withLightness(color, mid), background) >= min) {
far = mid
} else {
near = mid
}
}
return withLightness(color, far)
}
/** Blends `color` toward `background` until its contrast drops to `max`. */
function softenTo(color: Rgb, background: Rgb, max: number): Rgb {
if (contrastRatio(color, background) <= max) return color
let keep = 0
let blend = 1
for (let i = 0; i < 16; i++) {
const mid = (keep + blend) / 2
if (contrastRatio(mix(color, background, mid), background) >= max) {
blend = mid
} else {
keep = mid
}
}
return mix(color, background, blend)
}
function withLuminance(color: Rgb, target: number): Rgb {
let low = 0
let high = 1
for (let i = 0; i < 16; i++) {
const mid = (low + high) / 2
if (luminance(withLightness(color, mid)) < target) {
low = mid
} else {
high = mid
}
}
return withLightness(color, target <= DARK_BACKGROUND_MAX_LUMINANCE ? low : high)
}
/** `amount` of `color` over `base`. */
function mix(color: Rgb, base: Rgb, amount: number): Rgb {
return [0, 1, 2].map((i) =>
Math.round(base[i] + (color[i] - base[i]) * amount)
) as Rgb
}
function parseHex(hex: string): Rgb | null {
if (!/^#[0-9a-f]{6}$/i.test(hex)) return null
const value = parseInt(hex.slice(1), 16)
function contrastRatio(a: Rgb, b: Rgb) {
const la = luminance(a)
const lb = luminance(b)
return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05)
}
function luminance([r, g, b]: Rgb) {
return 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b)
}
function toLinear(channel: number) {
const c = channel / 255
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
}
function fromLinear(value: number) {
const c = value <= 0.0031308 ? value * 12.92 : 1.055 * value ** (1 / 2.4) - 0.055
return c * 255
}
function toOklch(color: Rgb): Oklch {
const [r, g, b] = color.map(toLinear)
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)
const L = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s
const A = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s
const B = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s
return { l: L, c: Math.hypot(A, B), h: Math.atan2(B, A) }
}
function oklchToLinear({ l: L, c, h }: Oklch): Rgb {
const A = c * Math.cos(h)
const B = c * Math.sin(h)
const l = (L + 0.3963377774 * A + 0.2158037573 * B) ** 3
const m = (L - 0.1055613458 * A - 0.0638541728 * B) ** 3
const s = (L - 0.0894841775 * A - 1.291485548 * B) ** 3
return [
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
]
}
/** Sets OKLCH lightness, reducing chroma as needed to stay within sRGB. */
function withLightness(color: Rgb, lightness: number): Rgb {
const lch = toOklch(color)
const l = Math.min(1, Math.max(0, lightness))
let c = lch.c
let linear = oklchToLinear({ l, c, h: lch.h })
for (let i = 0; i < 12 && !inGamut(linear); i++) {
c *= 0.8
linear = oklchToLinear({ l, c, h: lch.h })
}
return linear.map((value) =>
Math.round(Math.min(255, Math.max(0, fromLinear(value))))
) as Rgb
}
function inGamut(linear: Rgb) {
return linear.every((value) => value >= -0.0005 && value <= 1.0005)
}
function parseHex(hex: string | undefined): Rgb | null {
if (!hex) return null
const match = /^#?([0-9a-f]{6})/i.exec(hex.trim())
if (!match) return null
const value = parseInt(match[1], 16)
return [(value >> 16) & 255, (value >> 8) & 255, value & 255]
}
function toHex(color: Rgb) {
return `#${color.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`
return `#${color.map((c) => c.toString(16).padStart(2, "0")).join("")}`
}
+5 -22
View File
@@ -15,35 +15,18 @@ export type NowPlayingColors = {
isDark: boolean
}
export function nowPlayingColors(
palette: CoverPalette,
artworkColor?: string
): NowPlayingColors {
const foreground = artworkColor ? "#FFFFFF" : palette.foreground
const background =
artworkColor && /^#[0-9a-f]{6}$/i.test(artworkColor)
? `#${artworkColor
.slice(1)
.match(/../g)!
.map((channel) =>
Math.round(parseInt(channel, 16) * 0.4 + 32)
.toString(16)
.padStart(2, "0")
)
.join("")}`
: palette.background
export function nowPlayingColors(palette: CoverPalette): NowPlayingColors {
const { foreground } = palette
return {
background,
background: palette.background,
foreground,
secondaryForeground: artworkColor
? "rgba(255,255,255,0.6)"
: palette.secondaryForeground,
secondaryForeground: palette.secondaryForeground,
sliderTrack: withAlpha(foreground, 0.2),
sliderFill: withAlpha(foreground, 0.6),
control: withAlpha(foreground, 0.14),
controlPressed: withAlpha(foreground, 0.1),
selected: withAlpha(foreground, 0.8),
selectedForeground: background,
selectedForeground: palette.background,
highlight: withAlpha(foreground, 0.1),
isDark: palette.isDark,
}