240 lines
8.2 KiB
Dart
240 lines
8.2 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
import '../theme/app_theme.dart';
|
||
import '../utils/format.dart';
|
||
import 'camera_sheet.dart';
|
||
import 'ui.dart';
|
||
|
||
enum ImageFilterType { none, greyscale, invert, highContrast }
|
||
|
||
/// Live-image adjustments for one camera stream. Saved per camera, because
|
||
/// lighting differs from lane to lane.
|
||
class ImageAdjustments {
|
||
final ImageFilterType filter;
|
||
final double brightness; // -1 … 1
|
||
final double contrast; // 0.5 … 2
|
||
final int quarterTurns; // 0–3, for cameras mounted sideways
|
||
|
||
const ImageAdjustments({
|
||
this.filter = ImageFilterType.none,
|
||
this.brightness = 0,
|
||
this.contrast = 1,
|
||
this.quarterTurns = 0,
|
||
});
|
||
|
||
ImageAdjustments copyWith({
|
||
ImageFilterType? filter,
|
||
double? brightness,
|
||
double? contrast,
|
||
int? quarterTurns,
|
||
}) {
|
||
return ImageAdjustments(
|
||
filter: filter ?? this.filter,
|
||
brightness: brightness ?? this.brightness,
|
||
contrast: contrast ?? this.contrast,
|
||
quarterTurns: quarterTurns ?? this.quarterTurns,
|
||
);
|
||
}
|
||
|
||
/// Keeps rotation, resets the picture itself.
|
||
ImageAdjustments resetPicture() =>
|
||
copyWith(filter: ImageFilterType.none, brightness: 0, contrast: 1);
|
||
|
||
// Older versions stored one global setting; use it until a camera has its own
|
||
static Future<ImageAdjustments> load(String camera) async {
|
||
final p = await SharedPreferences.getInstance();
|
||
final filter =
|
||
p.getInt('filter_cam$camera') ?? p.getInt('selectedFilter') ?? 0;
|
||
return ImageAdjustments(
|
||
filter: ImageFilterType
|
||
.values[filter.clamp(0, ImageFilterType.values.length - 1)],
|
||
brightness: p.getDouble('brightness_cam$camera') ??
|
||
p.getDouble('brightness') ??
|
||
0,
|
||
contrast:
|
||
p.getDouble('contrast_cam$camera') ?? p.getDouble('contrast') ?? 1,
|
||
quarterTurns: p.getInt('rotation_cam$camera') ?? 0,
|
||
);
|
||
}
|
||
|
||
Future<void> save(String camera) async {
|
||
final p = await SharedPreferences.getInstance();
|
||
await p.setInt('filter_cam$camera', filter.index);
|
||
await p.setDouble('brightness_cam$camera', brightness);
|
||
await p.setDouble('contrast_cam$camera', contrast);
|
||
await p.setInt('rotation_cam$camera', quarterTurns);
|
||
}
|
||
|
||
/// Filter first, then contrast around mid-grey and brightness on top.
|
||
ColorFilter get colorFilter {
|
||
const lum = [0.2126, 0.7152, 0.0722];
|
||
final List<double> base = switch (filter) {
|
||
ImageFilterType.greyscale || ImageFilterType.highContrast => [
|
||
...lum, 0, 0, //
|
||
...lum, 0, 0,
|
||
...lum, 0, 0,
|
||
0, 0, 0, 1, 0,
|
||
],
|
||
ImageFilterType.invert => [
|
||
-1, 0, 0, 0, 255, //
|
||
0, -1, 0, 0, 255,
|
||
0, 0, -1, 0, 255,
|
||
0, 0, 0, 1, 0,
|
||
],
|
||
ImageFilterType.none => [
|
||
1, 0, 0, 0, 0, //
|
||
0, 1, 0, 0, 0,
|
||
0, 0, 1, 0, 0,
|
||
0, 0, 0, 1, 0,
|
||
],
|
||
};
|
||
// High contrast makes dark holes stand out on a light target
|
||
final c = contrast * (filter == ImageFilterType.highContrast ? 2.2 : 1);
|
||
final m = [...base];
|
||
for (final row in [0, 5, 10]) {
|
||
for (int i = 0; i < 4; i++) {
|
||
m[row + i] *= c;
|
||
}
|
||
m[row + 4] = m[row + 4] * c + 128 * (1 - c) + brightness * 255;
|
||
}
|
||
return ColorFilter.matrix(m);
|
||
}
|
||
}
|
||
|
||
/// Bottom sheet that leaves the live image visible, so every change is seen immediately.
|
||
Future<void> showImageSettingsSheet(
|
||
BuildContext context, {
|
||
required ImageAdjustments initial,
|
||
required String settingsId,
|
||
required String cameraLabel,
|
||
required String Function(String) t,
|
||
required ValueChanged<ImageAdjustments> onChanged,
|
||
}) {
|
||
var value = initial;
|
||
return showCameraSheet(
|
||
context,
|
||
builder: (sheet) => StatefulBuilder(
|
||
builder: (context, setSheetState) {
|
||
final c = Palette.current;
|
||
void update(ImageAdjustments next) {
|
||
setSheetState(() => value = next);
|
||
onChanged(next);
|
||
next.save(settingsId);
|
||
}
|
||
|
||
return Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [
|
||
CameraSheetHeader(
|
||
title: t('Image Settings'),
|
||
subtitle: '$cameraLabel · ${t('saved for this camera')}',
|
||
trailing: ResetPill(label: t('Reset'), onTap: () => update(value.resetPicture())),
|
||
),
|
||
Row(
|
||
children: [
|
||
for (final (f, icon, label) in [
|
||
(ImageFilterType.none, Icons.image_outlined, 'None'),
|
||
(ImageFilterType.greyscale, Icons.contrast_rounded, 'Greyscale'),
|
||
(ImageFilterType.highContrast, Icons.wb_sunny_outlined, 'High contrast'),
|
||
(ImageFilterType.invert, Icons.visibility_outlined, 'Invert'),
|
||
]) ...[
|
||
if (f != ImageFilterType.none) const SizedBox(width: 8),
|
||
Expanded(
|
||
child: _FilterTile(
|
||
icon: icon,
|
||
label: t(label),
|
||
selected: value.filter == f,
|
||
onTap: () => update(value.copyWith(filter: f)),
|
||
),
|
||
),
|
||
],
|
||
],
|
||
),
|
||
const SizedBox(height: 16),
|
||
SheetSlider(
|
||
icon: Icons.light_mode_outlined,
|
||
label: t('Brightness'),
|
||
valueText: '${value.brightness >= 0 ? '+' : '−'}${(value.brightness.abs() * 100).round()}',
|
||
value: value.brightness,
|
||
min: -1,
|
||
max: 1,
|
||
onChanged: (v) => update(value.copyWith(brightness: v)),
|
||
),
|
||
SheetSlider(
|
||
icon: Icons.contrast_rounded,
|
||
label: t('Contrast'),
|
||
valueText: dec(value.contrast, 1),
|
||
value: value.contrast,
|
||
min: 0.5,
|
||
max: 2,
|
||
onChanged: (v) => update(value.copyWith(contrast: v)),
|
||
),
|
||
const SizedBox(height: 8),
|
||
AppCard(
|
||
color: c.isDark ? c.card2 : c.card,
|
||
padding: const EdgeInsets.fromLTRB(16, 8, 8, 8),
|
||
onTap: () => update(value.copyWith(quarterTurns: (value.quarterTurns + 1) % 4)),
|
||
child: Row(
|
||
children: [
|
||
Icon(Icons.rotate_right_rounded, size: 20, color: c.muted),
|
||
const SizedBox(width: 12),
|
||
Expanded(
|
||
child: Text('${t('Rotate')} · ${value.quarterTurns * 90}°',
|
||
style: TextStyle(fontSize: 14.5, fontWeight: FontWeight.w500, color: c.ink)),
|
||
),
|
||
CircleButton(
|
||
icon: Icons.rotate_right_rounded,
|
||
size: 36,
|
||
onTap: () => update(value.copyWith(quarterTurns: (value.quarterTurns + 1) % 4)),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
);
|
||
},
|
||
),
|
||
);
|
||
}
|
||
|
||
class _FilterTile extends StatelessWidget {
|
||
final IconData icon;
|
||
final String label;
|
||
final bool selected;
|
||
final VoidCallback onTap;
|
||
|
||
const _FilterTile({required this.icon, required this.label, required this.selected, required this.onTap});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final c = Palette.current;
|
||
return Material(
|
||
color: selected ? c.accentSoft : (c.isDark ? c.card2 : c.tile),
|
||
shape: RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.circular(14),
|
||
side: BorderSide(color: selected ? c.accent : Colors.transparent, width: 1.5),
|
||
),
|
||
clipBehavior: Clip.antiAlias,
|
||
child: InkWell(
|
||
onTap: onTap,
|
||
child: Padding(
|
||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 2),
|
||
child: Column(
|
||
children: [
|
||
Icon(icon, color: selected ? c.accent : c.muted, size: 22),
|
||
const SizedBox(height: 6),
|
||
Text(
|
||
label,
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: TextStyle(fontSize: 10.5, fontWeight: FontWeight.w600, color: selected ? c.ink : c.muted),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|