259 lines
8.3 KiB
Dart
259 lines
8.3 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
import 'camera_sheet.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 Color accent,
|
||
required String Function(String) t,
|
||
required ValueChanged<ImageAdjustments> onChanged,
|
||
}) {
|
||
var value = initial;
|
||
return showCameraSheet(
|
||
context,
|
||
builder: (sheet) => StatefulBuilder(
|
||
builder: (context, setSheetState) {
|
||
void update(ImageAdjustments next) {
|
||
setSheetState(() => value = next);
|
||
onChanged(next);
|
||
next.save(settingsId);
|
||
}
|
||
|
||
return Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
CameraSheetHeader(
|
||
title: t('Image Settings'),
|
||
subtitle: '$cameraLabel · ${t('Saved for this camera')}',
|
||
trailing: TextButton.icon(
|
||
style: TextButton.styleFrom(foregroundColor: Colors.white70),
|
||
icon: const Icon(Icons.restart_alt_rounded, size: 18),
|
||
label: Text(t('Reset')),
|
||
onPressed: () => update(value.resetPicture()),
|
||
),
|
||
),
|
||
Row(
|
||
children: [
|
||
for (final (f, icon, label) in [
|
||
(ImageFilterType.none, Icons.image_outlined, 'None'),
|
||
(
|
||
ImageFilterType.greyscale,
|
||
Icons.filter_b_and_w_rounded,
|
||
'Greyscale'
|
||
),
|
||
(
|
||
ImageFilterType.highContrast,
|
||
Icons.contrast_rounded,
|
||
'High contrast'
|
||
),
|
||
(
|
||
ImageFilterType.invert,
|
||
Icons.invert_colors_rounded,
|
||
'Invert'
|
||
),
|
||
]) ...[
|
||
if (f != ImageFilterType.none) const SizedBox(width: 8),
|
||
Expanded(
|
||
child: _FilterTile(
|
||
icon: icon,
|
||
label: t(label),
|
||
selected: value.filter == f,
|
||
accent: accent,
|
||
onTap: () => update(value.copyWith(filter: f)),
|
||
),
|
||
),
|
||
],
|
||
],
|
||
),
|
||
const SizedBox(height: 14),
|
||
SheetSlider(
|
||
icon: Icons.brightness_6_rounded,
|
||
label: t('Brightness'),
|
||
valueText:
|
||
'${value.brightness >= 0 ? '+' : ''}${(value.brightness * 100).round()}',
|
||
value: value.brightness,
|
||
min: -1,
|
||
max: 1,
|
||
accent: accent,
|
||
onChanged: (v) => update(value.copyWith(brightness: v)),
|
||
),
|
||
SheetSlider(
|
||
icon: Icons.tonality_rounded,
|
||
label: t('Contrast'),
|
||
valueText: '${(value.contrast * 100).round()} %',
|
||
value: value.contrast,
|
||
min: 0.5,
|
||
max: 2,
|
||
accent: accent,
|
||
onChanged: (v) => update(value.copyWith(contrast: v)),
|
||
),
|
||
const SizedBox(height: 8),
|
||
SheetToggle(
|
||
icon: Icons.rotate_90_degrees_cw_rounded,
|
||
label: '${t('Rotate')} · ${value.quarterTurns * 90}°',
|
||
active: value.quarterTurns != 0,
|
||
accent: accent,
|
||
onTap: () => update(
|
||
value.copyWith(quarterTurns: (value.quarterTurns + 1) % 4)),
|
||
),
|
||
],
|
||
);
|
||
},
|
||
),
|
||
);
|
||
}
|
||
|
||
class _FilterTile extends StatelessWidget {
|
||
final IconData icon;
|
||
final String label;
|
||
final bool selected;
|
||
final Color accent;
|
||
final VoidCallback onTap;
|
||
|
||
const _FilterTile({
|
||
required this.icon,
|
||
required this.label,
|
||
required this.selected,
|
||
required this.accent,
|
||
required this.onTap,
|
||
});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Material(
|
||
color: selected
|
||
? accent.withValues(alpha: 0.18)
|
||
: Colors.white.withValues(alpha: 0.06),
|
||
shape: RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.circular(14),
|
||
side:
|
||
BorderSide(color: selected ? accent : Colors.transparent, width: 2),
|
||
),
|
||
child: InkWell(
|
||
borderRadius: BorderRadius.circular(14),
|
||
onTap: onTap,
|
||
child: Padding(
|
||
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 4),
|
||
child: Column(
|
||
children: [
|
||
Icon(icon, color: selected ? accent : Colors.white70, size: 26),
|
||
const SizedBox(height: 4),
|
||
Text(
|
||
label,
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: TextStyle(
|
||
fontSize: 11,
|
||
fontWeight: FontWeight.w600,
|
||
color: selected ? Colors.white : Colors.white70,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|