Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop the progress numbers washing out on light covers

Barnabas53b907ecf088
diff --git a/lib/utils/cover_accent.dart b/lib/utils/cover_accent.dart
index 28f5ae7f..0591bc53 100644
--- a/lib/utils/cover_accent.dart
+++ b/lib/utils/cover_accent.dart
@@ -1,3 +1,5 @@
+import 'dart:ui' as ui;
+
import 'package:flutter/material.dart';
import 'package:palette_generator/palette_generator.dart';
@@ -30,6 +32,67 @@ Color? accentFromCoverPalette(PaletteGenerator palette) {
(palette.colors.isEmpty ? null : palette.colors.first);
}
+/// Mean brightness of the top strip of a blurred cover - the strip the
+/// player's progress row sits on. Read from the small blurred image the cards
+/// already keep, and only every fourth pixel, so this is a few thousand
+/// samples once per cover.
+///
+/// Deliberately averages the gamma-encoded bytes rather than linearising them:
+/// the threshold this feeds is about what the eye reads as light or dark, and
+/// mid-grey should land near the middle.
+Future<double?> topStripLuminance(ui.Image image) async {
+ try {
+ final rows = (image.height * 0.28).round().clamp(1, image.height);
+ final data = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
+ if (data == null) return null;
+ final bytes = data.buffer.asUint8List();
+ var total = 0.0;
+ var count = 0;
+ for (var y = 0; y < rows; y++) {
+ for (var x = 0; x < image.width; x += 4) {
+ final i = (y * image.width + x) * 4;
+ if (i + 2 >= bytes.length) break;
+ total +=
+ (0.2126 * bytes[i] + 0.7152 * bytes[i + 1] + 0.0722 * bytes[i + 2]) /
+ 255.0;
+ count++;
+ }
+ }
+ return count == 0 ? null : total / count;
+ } catch (_) {
+ return null;
+ }
+}
+
+/// What the eye actually sees when a [scrim] at [scrimAlpha] is laid over
+/// artwork of [artworkLuminance].
+double scrimmedLuminance(
+ double artworkLuminance,
+ Color scrim,
+ double scrimAlpha,
+) =>
+ artworkLuminance * (1 - scrimAlpha) +
+ scrim.computeLuminance() * scrimAlpha;
+
+/// Ink and its shadow for text drawn on a backdrop of [luminance].
+///
+/// The player's elapsed/percent/remaining row used to take its color from the
+/// app theme, which says nothing about the cover behind it: a pale cover in
+/// dark mode left white text on a near-white backdrop, which is what a user
+/// reported as unreadable. Choosing by what is actually behind the text fixes
+/// it in both directions, and needs no setting.
+({Color ink, Color shadow}) inkForLuminance(double luminance) {
+ final useLightInk = luminance < 0.45;
+ return (
+ ink: useLightInk
+ ? Colors.white.withValues(alpha: 0.82)
+ : Colors.black.withValues(alpha: 0.72),
+ shadow: useLightInk
+ ? Colors.black.withValues(alpha: 0.6)
+ : Colors.white.withValues(alpha: 0.6),
+ );
+}
+
/// 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.
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index 1a0c94db..ff6d6191 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../l10n/app_localizations.dart';
+import '../utils/cover_accent.dart';
import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
import '../screens/app_shell.dart';
@@ -99,6 +100,10 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
ui.Image? _blurredCover; // Precached blurred background
String? _blurredCoverIdentity;
String? _pendingBlurIdentity;
+ // Mean brightness of the blurred cover's top strip, which is what the
+ // progress row is drawn on. Null until the blur is ready, or when the card
+ // isn't using the blurred background at all.
+ double? _coverTopLuminance;
List<String> _buttonOrder = PlayerSettings.defaultButtonOrder;
int _buttonVisibleCount = PlayerSettings.defaultButtonVisibleCount;
bool _iconsOnly = false;
@@ -536,6 +541,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
_blurredCover = null;
_blurredCoverIdentity = null;
_pendingBlurIdentity = null;
+ _coverTopLuminance = null;
_fetchedChapters = null;
_fetchedEbookFile = null;
_lastChapterIdx = -1;
@@ -671,11 +677,14 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
final blurred = await picture.toImage(targetWidth, targetHeight);
picture.dispose();
+ final topLuminance = await topStripLuminance(blurred);
+
if (mounted && _currentCoverIdentity() == coverIdentity) {
final previous = _blurredCover;
setState(() {
_blurredCover = blurred;
_blurredCoverIdentity = coverIdentity;
+ _coverTopLuminance = topLuminance;
});
previous?.dispose();
} else {
@@ -690,6 +699,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
}
}
+
@override
Widget build(BuildContext context) {
super.build(context); // required for AutomaticKeepAliveClientMixin
@@ -906,11 +916,27 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
final liveBookProgress = dur > 0 ? (pos / dur).clamp(0.0, 1.0) : bookProgress;
final elapsed = pos / speed;
final remaining = (dur - pos) / speed;
+ // This row sits on the cover, not on the theme surface.
+ // With a blurred background the scrim is at its thinnest
+ // right here (0.3 / 0.4), so a pale cover in dark mode
+ // used to leave white text on a near-white backdrop.
+ // Pick the ink from what is actually behind it; the other
+ // background modes follow the theme as before.
+ final coverLuminance =
+ _cardBackground == 'blurred' ? _coverTopLuminance : null;
+ final ink = coverLuminance == null
+ ? null
+ : inkForLuminance(scrimmedLuminance(
+ coverLuminance,
+ isDark ? Colors.black : Colors.white,
+ isDark ? 0.3 : 0.4,
+ ));
final timeStyle = tt.labelSmall?.copyWith(
- color: isDark ? Colors.white.withValues(alpha: 0.55) : cs.onSurface,
+ color: ink?.ink ??
+ (isDark ? Colors.white.withValues(alpha: 0.55) : cs.onSurface),
fontWeight: FontWeight.w500, fontSize: (compact ? 10 : 11) * _progressTextScale,
fontFeatures: const [FontFeature.tabularFigures()],
- shadows: [Shadow(color: isDark ? Colors.black.withValues(alpha: 0.6) : Colors.white.withValues(alpha: 0.6), blurRadius: 4)],
+ shadows: [Shadow(color: ink?.shadow ?? (isDark ? Colors.black.withValues(alpha: 0.6) : Colors.white.withValues(alpha: 0.6)), blurRadius: 4)],
);
return Stack(
alignment: Alignment.center,
diff --git a/lib/widgets/expanded_card.dart b/lib/widgets/expanded_card.dart
index 1d55871f..32f989ad 100644
--- a/lib/widgets/expanded_card.dart
+++ b/lib/widgets/expanded_card.dart
@@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../l10n/app_localizations.dart';
+import '../utils/cover_accent.dart';
import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
import '../services/audio_player_service.dart';
@@ -89,6 +90,8 @@ class _ExpandedCardState extends State<ExpandedCard> {
}
ImageProvider? _coverProvider;
ui.Image? _blurredCover;
+ // Brightness of the blurred cover's top strip, where the percentage sits.
+ double? _coverTopLuminance;
List<dynamic>? _fetchedChapters;
// The item passed to the full-screen player is often a lean/synthetic map
// without media.ebookFile, so the "Read" action fell back to "no ebook".
@@ -399,6 +402,7 @@ class _ExpandedCardState extends State<ExpandedCard> {
// Dispose old blur and regenerate
_blurredCover?.dispose();
_blurredCover = null;
+ _coverTopLuminance = null;
_generateBlur();
_fetchChaptersIfNeeded();
_startChapterTracking();
@@ -584,8 +588,13 @@ class _ExpandedCardState extends State<ExpandedCard> {
final blurred = await picture.toImage(targetWidth, targetHeight);
picture.dispose();
+ final topLuminance = await topStripLuminance(blurred);
+
if (mounted) {
- setState(() => _blurredCover = blurred);
+ setState(() {
+ _blurredCover = blurred;
+ _coverTopLuminance = topLuminance;
+ });
} else {
blurred.dispose();
}
@@ -700,15 +709,28 @@ class _ExpandedCardState extends State<ExpandedCard> {
: outerConstraints.maxWidth >
outerConstraints.maxHeight;
+ // Same story as the small card: the scrim is thinnest at
+ // the top, so on a blurred background take the ink from
+ // the cover rather than from the theme.
+ final coverLuminance =
+ _cardBackground == 'blurred' ? _coverTopLuminance : null;
+ final ink = coverLuminance == null
+ ? null
+ : inkForLuminance(scrimmedLuminance(
+ coverLuminance,
+ isDark ? Colors.black : Colors.white,
+ isDark ? 0.3 : 0.4,
+ ));
final statsRow = Padding(
padding: EdgeInsets.fromLTRB(24, compact ? 4 : 12, 24, 0),
child: Center(
child: Text('${(bookProgress * 100).clamp(0, 100).toStringAsFixed(1)}%',
style: tt.labelSmall?.copyWith(
- color: isDark ? Colors.white.withValues(alpha: 0.55) : Colors.black.withValues(alpha: 0.45),
+ color: ink?.ink ??
+ (isDark ? Colors.white.withValues(alpha: 0.55) : Colors.black.withValues(alpha: 0.45)),
fontWeight: FontWeight.w500, fontSize: (compact ? 10 : 11) * _progressTextScale,
fontFeatures: const [ui.FontFeature.tabularFigures()],
- shadows: [Shadow(color: isDark ? Colors.black.withValues(alpha: 0.6) : Colors.white.withValues(alpha: 0.6), blurRadius: 4)],
+ shadows: [Shadow(color: ink?.shadow ?? (isDark ? Colors.black.withValues(alpha: 0.6) : Colors.white.withValues(alpha: 0.6)), blurRadius: 4)],
)),
),
);