Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Keep a cover's Audible badge from setting the accent color

Barnabasd4a23f97ec16
diff --git a/lib/screens/app_shell.dart b/lib/screens/app_shell.dart
index 3a888835..1d42f65c 100644
--- a/lib/screens/app_shell.dart
+++ b/lib/screens/app_shell.dart
@@ -12,6 +12,7 @@ import '../services/sleep_timer_service.dart';
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:palette_generator/palette_generator.dart';
+import '../utils/cover_accent.dart';
import '../main.dart'
show snappyTransitionsNotifier, coverSchemeNotifier, rootNavigatorKey;
import '../l10n/app_localizations.dart';
@@ -390,9 +391,7 @@ class _AppShellState extends State<AppShell> with WidgetsBindingObserver, Ticker
final brightness = Theme.of(context).brightness;
PaletteGenerator.fromImageProvider(provider, maximumColorCount: 16)
.then((palette) {
- final seedColor = palette.vibrantColor?.color
- ?? palette.dominantColor?.color
- ?? palette.colors.firstOrNull;
+ final seedColor = accentFromCoverPalette(palette);
if (seedColor == null) {
_lastCoverItemId = null;
return;
diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart
index c9fcfa28..5474cfb0 100644
--- a/lib/screens/home_screen.dart
+++ b/lib/screens/home_screen.dart
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:palette_generator/palette_generator.dart';
+import '../utils/cover_accent.dart';
import '../widgets/absorbing_shared.dart';
import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
@@ -748,11 +749,7 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {
}
PaletteGenerator.fromImageProvider(provider, maximumColorCount: 16)
.then((palette) {
- final picked = palette.vibrantColor?.color
- ?? palette.lightVibrantColor?.color
- ?? palette.darkVibrantColor?.color
- ?? palette.dominantColor?.color
- ?? palette.colors.firstOrNull;
+ final picked = accentFromCoverPalette(palette);
if (picked == null) return;
_accentCache[coverUrl] = picked;
if (!mounted) return;
diff --git a/lib/utils/cover_accent.dart b/lib/utils/cover_accent.dart
new file mode 100644
index 00000000..28f5ae7f
--- /dev/null
+++ b/lib/utils/cover_accent.dart
@@ -0,0 +1,45 @@
+import 'package:flutter/material.dart';
+import 'package:palette_generator/palette_generator.dart';
+
+/// Picks an accent color from a cover's [palette].
+///
+/// Guards against a small but highly-saturated corner badge (e.g. the yellow
+/// "Only from Audible" banner) hijacking the result. PaletteGenerator's
+/// "vibrant" target is scored mostly on saturation, so a tiny vivid badge can
+/// outrank the dominant artwork color - turning a mostly-blue cover's accent
+/// yellow. When the vibrant swatch covers only a small slice of the cover
+/// relative to the dominant color, we treat it as a badge and fall back to the
+/// most prominent swatch that still reads as a real color. Genuinely vibrant
+/// covers (where the vivid color IS a large share of the image) are unaffected.
+Color? accentFromCoverPalette(PaletteGenerator palette) {
+ final vibrant = palette.vibrantColor ??
+ palette.lightVibrantColor ??
+ palette.darkVibrantColor;
+ final dominant = palette.dominantColor;
+
+ if (vibrant != null &&
+ dominant != null &&
+ vibrant.population < dominant.population * 0.4) {
+ final colorful = _mostProminentColorful(palette);
+ if (colorful != null) return colorful;
+ return dominant.color;
+ }
+
+ return vibrant?.color ??
+ dominant?.color ??
+ (palette.colors.isEmpty ? null : palette.colors.first);
+}
+
+/// Highest-population swatch that still reads as a color - skips grey, near
+/// white and near black so the fallback accent matches the artwork rather than
+/// washing out.
+Color? _mostProminentColorful(PaletteGenerator palette) {
+ PaletteColor? best;
+ for (final pc in palette.paletteColors) {
+ final hsv = HSVColor.fromColor(pc.color);
+ if (hsv.saturation < 0.15) continue; // grey / white / black
+ if (hsv.value < 0.12) continue; // near black
+ if (best == null || pc.population > best.population) best = pc;
+ }
+ return best?.color;
+}
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index 343d1299..56b723a8 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -9,6 +9,7 @@ import 'package:provider/provider.dart';
import 'overlay_toast.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:palette_generator/palette_generator.dart';
+import '../utils/cover_accent.dart';
import 'package:path_provider/path_provider.dart';
import 'package:file_picker/file_picker.dart';
import 'package:http/http.dart' as http;
@@ -247,9 +248,7 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
PaletteGenerator.fromImageProvider(provider, maximumColorCount: 16)
.then((palette) {
debugPrint('[BookDetail] PaletteGenerator ok in ${DateTime.now().difference(t0).inMilliseconds}ms');
- final seedColor = palette.vibrantColor?.color
- ?? palette.dominantColor?.color
- ?? palette.colors.firstOrNull;
+ final seedColor = accentFromCoverPalette(palette);
if (seedColor == null || !mounted) return;
setState(() => _coverScheme = ColorScheme.fromSeed(
seedColor: seedColor, brightness: brightness));