m3_lightmeter/lib/models/photography_value.dart

52 lines
1.5 KiB
Dart
Raw Normal View History

import 'dart:math';
import 'package:lightmeter/utils/log_2.dart';
2022-10-30 18:06:51 +00:00
enum StopType { full, half, third }
2022-10-25 19:53:39 +00:00
abstract class PhotographyValue<T extends num> {
2022-10-25 19:53:39 +00:00
final T rawValue;
const PhotographyValue(this.rawValue);
T get value => rawValue;
/// EV difference between `this` and `other`
double evDifference(PhotographyValue other) => log2(max(1, other.value) / max(1, value));
String toStringDifference(PhotographyValue other) {
final ev = log2(max(1, other.value) / max(1, value));
final buffer = StringBuffer();
if (ev > 0) {
buffer.write('+');
}
buffer.write(ev.toStringAsFixed(1));
return buffer.toString();
}
}
abstract class PhotographyStopValue<T extends num> extends PhotographyValue<T> {
final StopType stopType;
2022-10-25 19:53:39 +00:00
const PhotographyStopValue(super.rawValue, this.stopType);
2022-10-25 19:53:39 +00:00
}
extension PhotographyStopValues<T extends PhotographyStopValue> on List<T> {
2022-10-30 18:06:51 +00:00
List<T> whereStopType(StopType stopType) {
2022-10-26 19:49:21 +00:00
switch (stopType) {
2022-10-30 18:06:51 +00:00
case StopType.full:
return where((e) => e.stopType == StopType.full).toList();
case StopType.half:
return where((e) => e.stopType == StopType.full || e.stopType == StopType.half).toList();
case StopType.third:
return where((e) => e.stopType == StopType.full || e.stopType == StopType.third).toList();
2022-10-26 19:49:21 +00:00
}
}
2022-10-25 19:53:39 +00:00
2022-10-30 18:06:51 +00:00
List<T> fullStops() => whereStopType(StopType.full);
2022-10-25 19:53:39 +00:00
2022-10-30 18:06:51 +00:00
List<T> halfStops() => whereStopType(StopType.half);
2022-10-26 19:49:21 +00:00
2022-10-30 18:06:51 +00:00
List<T> thirdStops() => whereStopType(StopType.third);
2022-10-25 19:53:39 +00:00
}