diff --git a/GammaTest/AppDelegate.m b/GammaTest/AppDelegate.m index f32012b..1de77a2 100644 --- a/GammaTest/AppDelegate.m +++ b/GammaTest/AppDelegate.m @@ -25,6 +25,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"enabled": @NO, @"maxOrange": @0.7, + @"dim": @0, @"colorChangingEnabled": @YES, @"lastAutoChangeDate": [NSDate distantPast], @"autoStartHour": @19, diff --git a/GammaTest/GammaController.h b/GammaTest/GammaController.h index af749ee..dfe764d 100644 --- a/GammaTest/GammaController.h +++ b/GammaTest/GammaController.h @@ -12,6 +12,7 @@ + (void)setGammaWithRed:(float)red green:(float)green blue:(float)blue; + (void)setGammaWithOrangeness:(float)percentOrange; ++ (void)setGammaWithOrangenessAndDim:(float)percentOrange percentDim:(float)percentDim; + (void)enableOrangeness; + (void)disableOrangeness; diff --git a/GammaTest/GammaController.m b/GammaTest/GammaController.m index f74a154..fd0d43f 100644 --- a/GammaTest/GammaController.m +++ b/GammaTest/GammaController.m @@ -28,6 +28,9 @@ @implementation GammaController +float dimCap = 0.85; //only allows you to dim to this percent to prevent completetely blacking out the screen + + //This function is largely the same as the one in iomfsetgamma.c from Saurik's UIKitTools package. The license is pasted below. /* UIKit Tools - command-line utilities for UIKit @@ -162,10 +165,39 @@ + (void)setGammaWithOrangeness:(float)percentOrange { [self setGammaWithRed:red green:green blue:blue]; } ++ (void)setGammaWithOrangenessAndDim:(float)percentOrange percentDim:(float)percentDim { + if (percentOrange > 1) + percentOrange = 1; + else if (percentOrange < 0) + percentOrange = 0; + + if (percentDim > 1) + percentDim = 1; + else if (percentDim < 0) + percentDim = 0; + percentDim = 1-percentDim * dimCap; + +// float red = 0.3; +// float blue = 0.3; +// float green = 0.3; + + float red = (1.0); + float blue = (1 - percentOrange); + float green = (red + blue)/2.0*percentDim; + red *= percentDim; + blue *= percentDim; + + if (percentOrange == 0) { + red = blue = green = 0.99*percentDim; + } + + [self setGammaWithRed:red green:green blue:blue]; +} + + (void)enableOrangeness { NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; [self wakeUpScreenIfNeeded]; - [GammaController setGammaWithOrangeness:[defaults floatForKey:@"maxOrange"]]; + [GammaController setGammaWithOrangenessAndDim:[defaults floatForKey:@"maxOrange"] percentDim:[defaults floatForKey:@"dim"]]; [defaults setObject:[NSDate date] forKey:@"lastOnDate"]; [defaults setBool:YES forKey:@"enabled"]; } diff --git a/GammaTest/MainViewController.m b/GammaTest/MainViewController.m index 6d70802..3eabb46 100644 --- a/GammaTest/MainViewController.m +++ b/GammaTest/MainViewController.m @@ -15,6 +15,7 @@ @interface MainViewController () @property (weak, nonatomic) IBOutlet UISwitch *colorChangingEnabledSwitch; @property (weak, nonatomic) IBOutlet UITextField *startTimeTextField; @property (weak, nonatomic) IBOutlet UITextField *endTimeTextField; +@property (weak, nonatomic) IBOutlet UISlider *dimSlider; @end @@ -22,10 +23,14 @@ @implementation MainViewController @synthesize enabledSwitch; @synthesize orangeSlider; +@synthesize dimSlider; @synthesize colorChangingEnabledSwitch; @synthesize startTimeTextField; @synthesize endTimeTextField; +float orange; +float dim; + - (void)viewDidLoad { [super viewDidLoad]; @@ -38,6 +43,9 @@ - (void)viewDidLoad { self.tableView.alwaysBounceVertical = NO; enabledSwitch.on = [defaults boolForKey:@"enabled"]; orangeSlider.value = [defaults floatForKey:@"maxOrange"]; + dimSlider.value = [defaults floatForKey:@"dim"]; + orange = [defaults floatForKey:@"maxOrange"]; + dim = [defaults floatForKey:@"dim"]; colorChangingEnabledSwitch.on = [defaults boolForKey:@"colorChangingEnabled"]; NSDate *date = [self dateForHour:[defaults integerForKey:@"autoStartHour"] andMinute:[defaults integerForKey:@"autoStartMinute"]]; @@ -63,18 +71,27 @@ - (void)viewDidLoad { - (IBAction)enabledSwitchChanged:(UISwitch *)sender { if (sender.on) - [GammaController setGammaWithOrangeness:[[NSUserDefaults standardUserDefaults] floatForKey:@"maxOrange"]]; + [GammaController setGammaWithOrangenessAndDim:[[NSUserDefaults standardUserDefaults] floatForKey:@"maxOrange"] percentDim:[[NSUserDefaults standardUserDefaults] floatForKey:@"dim"]]; else [GammaController setGammaWithOrangeness:0]; [[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:@"enabled"]; } + - (IBAction)maxOrangeSliderChanged:(UISlider *)sender { [[NSUserDefaults standardUserDefaults] setFloat:sender.value forKey:@"maxOrange"]; + orange = sender.value; + + if (enabledSwitch.on) + [GammaController setGammaWithOrangenessAndDim:orange percentDim:dim]; +} +- (IBAction)dimSliderChanged:(UISlider *)sender { + [[NSUserDefaults standardUserDefaults] setFloat:sender.value forKey:@"dim"]; + dim = sender.value; if (enabledSwitch.on) - [GammaController setGammaWithOrangeness:sender.value]; + [GammaController setGammaWithOrangenessAndDim:orange percentDim:dim]; } - (IBAction)colorChangingEnabledSwitchChanged:(UISwitch *)sender { diff --git a/GammaTest/Storyboard.storyboard b/GammaTest/Storyboard.storyboard index ec97b7d..a7ba55e 100644 --- a/GammaTest/Storyboard.storyboard +++ b/GammaTest/Storyboard.storyboard @@ -1,8 +1,8 @@ - + - + @@ -39,7 +39,7 @@ - + @@ -75,10 +75,10 @@ - + - + @@ -100,14 +100,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + Automatic color changing uses the background fetch feature to turn on or off orangeness. It doesn't happen immediately when the start or end times are passed, and won't work if background app refresh is disabled or iOS 9 low power mode is on. If your device's screen turns on randomly, that's the app making sure the screen is on before changing the color. - + - + @@ -139,13 +164,13 @@ - + - + - - + - + -