From 7d74a6a48c35bbabba96ad25cfda4ffd60de0512 Mon Sep 17 00:00:00 2001
From: Mattias Andrée <maandree@operamail.com>
Date: Sun, 4 May 2014 16:00:32 +0200
Subject: Add support for custom transition start and end elevation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Implemented through config options transition-high and transition-low (there
is no command line switch at this point). Determines at what solar elevation
Redshift will change from night to transition period to day.

Signed-off-by: Mattias Andrée <maandree@operamail.com>
---
 src/redshift.c | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/src/redshift.c b/src/redshift.c
index c2f2e9e..6fc2895 100644
--- a/src/redshift.c
+++ b/src/redshift.c
@@ -262,16 +262,20 @@ static int disable = 0;
 
 #endif /* ! HAVE_SIGNAL_H || __WIN32__ */
 
+/* Transition settings. */
+static float transition_low = TRANSITION_LOW;
+static float transition_high = TRANSITION_HIGH;
+
 
 /* Print which period (night, day or transition) we're currently in. */
 static void
 print_period(double elevation)
 {
-	if (elevation < TRANSITION_LOW) {
+	if (elevation < transition_low) {
 		printf(_("Period: Night\n"));
-	} else if (elevation < TRANSITION_HIGH) {
-		float a = (TRANSITION_LOW - elevation) /
-			(TRANSITION_LOW - TRANSITION_HIGH);
+	} else if (elevation < transition_high) {
+		float a = (transition_low - elevation) /
+			(transition_low - transition_high);
 		printf(_("Period: Transition (%.2f%% day)\n"), a*100);
 	} else {
 		printf(_("Period: Daytime\n"));
@@ -283,12 +287,12 @@ static float
 calculate_interpolated_value(double elevation, float day, float night)
 {
 	float result;
-	if (elevation < TRANSITION_LOW) {
+	if (elevation < transition_low) {
 		result = night;
-	} else if (elevation < TRANSITION_HIGH) {
+	} else if (elevation < transition_high) {
 		/* Transition period: interpolate */
-		float a = (TRANSITION_LOW - elevation) /
-			(TRANSITION_LOW - TRANSITION_HIGH);
+		float a = (transition_low - elevation) /
+			(transition_low - transition_high);
 		result = (1.0-a)*night + a*day;
 	} else {
 		result = day;
@@ -875,6 +879,12 @@ main(int argc, char *argv[])
 				if (isnan(brightness_night)) {
 					brightness_night = atof(setting->value);
 				}
+			} else if (strcasecmp(setting->name,
+					      "elevation-high") == 0) {
+				transition_high = atof(setting->value);
+			} else if (strcasecmp(setting->name,
+					      "elevation-low") == 0) {
+				transition_low = atof(setting->value);
 			} else if (strcasecmp(setting->name, "gamma") == 0) {
 				if (isnan(gamma[0])) {
 					r = parse_gamma_string(setting->value,
@@ -994,8 +1004,11 @@ main(int argc, char *argv[])
 		        printf(_("Location: %f, %f\n"), lat, lon);
 			printf(_("Temperatures: %dK at day, %dK at night\n"),
 			       temp_day, temp_night);
+		        /* TRANSLATORS: Append degree symbols if possible. */
+			printf(_("Solar elevations: day above %.1f, night below %.1f\n"),
+			       transition_high, transition_low);
 		}
-	
+
 		/* Latitude */
 		if (lat < MIN_LAT || lat > MAX_LAT) {
 		        /* TRANSLATORS: Append degree symbols if possible. */
@@ -1029,6 +1042,14 @@ main(int argc, char *argv[])
 				MIN_TEMP, MAX_TEMP);
 			exit(EXIT_FAILURE);
 		}
+
+		/* Solar elevations */
+		if (transition_high < transition_low) {
+		        fprintf(stderr,
+		                _("High transition elevation cannot be lower than"
+				  " the low transition elevation.\n"));
+		        exit(EXIT_FAILURE);
+		}
 	}
 
 	if (mode == PROGRAM_MODE_MANUAL) {
-- 
cgit v1.2.3-70-g09d2