Skip to content

Instantly share code, notes, and snippets.

@willblaschko
Created December 12, 2020 17:06
Show Gist options
  • Save willblaschko/230a5b654f6ad3f078eb1a30a6d08025 to your computer and use it in GitHub Desktop.
Save willblaschko/230a5b654f6ad3f078eb1a30a6d08025 to your computer and use it in GitHub Desktop.
Photography EV/F-Stop/ISO/Shutter Speed Calculations - Dart
import 'dart:math';
import 'package:lightmeterv2/models/shutter.dart';
class EV
{
static double luxToFC = 10.763910417;
static double calc(double f, int iso, Shutter sec)
{
double EV = (((log(f * f) / log(2.0)) - (log(sec.getValue()) / log(2.0))) - (log(iso / 100) / log(2.0)));
return EV;
}
static double EVtoLux(double ev)
{
return (2.5 * pow(2, ev));
}
static double EVtoFC(double ev)
{
return EVtoLux(ev) / luxToFC;
}
static double luxToEV(double lux)
{
return (log(lux ~/ 2.5) ~/ log(2)) as double;
}
static double adjustEV(double ev, double hand, double ND)
{
double adjust = (hand - ND);
return ev + adjust;
}
}
import 'dart:math';
import 'package:lightmeterv2/models/iso.dart';
import 'package:lightmeterv2/models/shutter.dart';
import 'package:lightmeterv2/util/value_list_manager.dart';
class FStop
{
static double calc(double ev, double iso, Shutter sec)
{
double shutter = (log(sec.getValue()) / log(2));
double f = sqrt(pow(2, (ev + shutter) + ISO.EVValue(iso)));
return ValueListManager.getClosestBoundedDouble(f, ValueListManager.getFStopListBounded());
}
static double calcUnbounded(double ev, double iso, Shutter sec)
{
double shutter = (log(sec.getValue()) / log(2));
double f = sqrt(pow(2, (ev + shutter) + ISO.EVValue(iso)));
return ValueListManager.getClosestBoundedDouble(f, ValueListManager.getFStopList());
}
}
import 'dart:core';
import 'dart:math';
import 'package:lightmeterv2/models/shutter.dart';
import 'package:lightmeterv2/util/value_list_manager.dart';
class ISO {
static double calc(double ev, double f, Shutter sec){
double iso=1.000* log(f*f)/ log(2)- log(sec.getValue())/ log(2)-ev;
iso= pow(2, (iso))*100;
return ValueListManager.getClosestBoundedDouble(iso, ValueListManager.getISOListBounded());
}
static double calcUnbounded(double ev, double f, Shutter sec){
double iso=1.000* log(f*f)/ log(2)- log(sec.getValue())/ log(2)-ev;
iso= pow(2, (iso))*100;
return ValueListManager.getClosestBoundedDouble(iso, ValueListManager.getISOList());
}
static double EVValue(double iso){
return log(iso/100.00)/ log(2);
}
}
import 'dart:core';
import 'dart:math';
import 'package:lightmeterv2/util/value_list_manager.dart';
import 'iso.dart';
class Shutter implements Comparable<Shutter> {
String humanReadable;
double value;
String original;
Shutter(String s){
if(s == null){
s = "0";
}
this.original=s;
this.value=val(s);
this.humanReadable=getDisplayShutter(value);
}
Shutter.fromDouble(double s){
this.original = s.toString();
this.value=s;
this.humanReadable=this.original;
}
Shutter.fromJson(Map<String, dynamic> json) : humanReadable = json['humanReadable'],
value = json['value'], original = json['original'];
Map<String, dynamic> toJson() =>
{
'humanReadable': humanReadable,
'value': value,
'original': original
};
static Shutter calc(double ev, double iso, double f){
double tempF=(log(f*f)/ log(2));
double t= pow(2,(tempF-ev-ISO.EVValue(iso)));
return ValueListManager.getClosestBoundedShutter(t);
}
static Shutter calcUnbounded(double ev, double iso, double f){
double tempF=(log(f*f)/ log(2));
double t= pow(2,(tempF-ev-ISO.EVValue(iso)));
if(t > 15){
return new Shutter(t.round().toString()+"");
}
return ValueListManager.getClosest(t);
}
String getHumanReadable() {
return humanReadable;
}
@override
String toString(){
return getHumanReadable();
}
double getValue() {
return value;
}
String getOriginal() { return original; }
String getDisplayShutter(double val){
if(val < 60){
return this.original;
}
double intVal = val;
String myText=intVal.toString();
if(intVal>60 && intVal < 3600)
{
myText=((intVal~/60).toStringAsFixed(0)+"m "+(intVal%60).toStringAsFixed(0)+"s");
}
if(intVal>=3600){
myText=(intVal~/3600).toStringAsFixed(0)+"h "+((intVal%3600)~/60).toStringAsFixed(0)+"m";
}
if(intVal>=86400){
myText=(intVal~/86400).toStringAsFixed(0)+"d "+((intVal%86400)~/3600).toStringAsFixed(0)+"h";
}
return myText;
}
static double val(String s){
double shutter;
try {
if (s.contains("/")) {
List<String> parts = s.split("/");
shutter = double.parse(parts[0]) / double.parse(parts[1]);
} else {
shutter = double.parse(s);
}
}catch (e){
print(e);
return 1/200;
}
return shutter;
}
static bool isValidShutter(String s){
double shutter;
try {
if (s.contains("/")) {
List<String> parts = s.split("/");
shutter = double.parse(parts[0]) / double.parse(parts[1]);
} else {
shutter = double.parse(s);
}
}catch (e){
return false;
}
return true;
}
@override
int compare(Shutter shutter, Shutter shutter2) {
if(shutter == null){
return 1;
}
if(shutter2 == null){
return -1;
}
return (shutter.getValue()).compareTo(shutter2.getValue());
}
@override
int compareTo(Shutter shutter) {
if(shutter == null){
return 1;
}
return (getValue()).compareTo(shutter.getValue());
}
@override
bool equals(Object o){
if(!(o is Shutter)){ return false; }
Shutter s= o as Shutter;
return (s.getValue() == getValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment