Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/elektra/redshift.ni
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ opt = f
opt/long = fade-fast
opt/arg = none

[fade/easing]
type = enum
description = The easing mode to use during a fade between color temperatures. Use something other than "linear" to make fades more pleasant for your eyes. For details about the supported easing functions, see https://easings.net/
default = linear
check/enum = #3
check/enum/#0 = linear
check/enum/#1 = ease-in
check/enum/#2 = ease-out
check/enum/#3 = ease-in-out
default = ease-in-out
example = ease-in
opt = e
opt/long = fade-easing
opt/arg = required

[brightness/day]
type = float
description = The screen brightness during daytime. If both day and night brightness are set, these will overrule the value of brightness.
Expand Down
24 changes: 21 additions & 3 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@ options_load_from_elektra(
break;
}
}

// Easing mode
ElektraEnumFadeEasing easingMode = elektraGetFadeEasing(elektra);
switch (easingMode) {
case ELEKTRA_ENUM_FADE_EASING_LINEAR:
options->easing_mode = EASING_MODE_LINEAR;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you directly use Elektra's enum?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was probably influenced by my "implementation without Elektra".

Thanks for the hint, I've refactored this!

break;
case ELEKTRA_ENUM_FADE_EASING_EASE_IN:
options->easing_mode = EASING_MODE_EASE_IN;
break;
case ELEKTRA_ENUM_FADE_EASING_EASE_OUT:
options->easing_mode = EASING_MODE_EASE_OUT;
break;
case ELEKTRA_ENUM_FADE_EASING_EASE_IN_OUT:
options->easing_mode = EASING_MODE_EASE_IN_OUT;
break;
}


// Brightness
*(&options->scheme.day.brightness) = elektraGetBrightnessDay(elektra);
Expand Down Expand Up @@ -391,9 +409,9 @@ options_init(options_t *options)
options->temp_set = -1;

options->method = NULL;
options->method_crtc = -1;
options->method_screen = -1;
options->method_drm_card = -1;
options->method_crtc = -1;
options->method_screen = -1;
options->method_drm_card = -1;

options->provider = NULL;

Expand Down
21 changes: 9 additions & 12 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,29 @@
#include "redshift.h"

typedef struct {
/* Path to config file */
/*
* For description of these options, see Elektra specification file src/elektra/redshift.ni
*/
char *config_filepath;

transition_scheme_t scheme;
program_mode_t mode;
int verbose;

/* Temperature to set in manual mode. */
int temp_set;
/* Whether to fade between large skips in color temperature. */
int use_fade;
/* Whether to preserve gamma ramps if supported by gamma method. */
easing_mode_t easing_mode;

int preserve_gamma;

/* Selected gamma method. */
const gamma_method_t *method;
/* Options for adjustment methods */
unsigned short method_crtc;
unsigned short method_screen;
unsigned short method_drm_card;
unsigned short method_crtc;
unsigned short method_screen;
unsigned short method_drm_card;

/* Selected location provider. */
const location_provider_t *provider;
/* Lat, lon for location provider. */
float provider_manual_arg_lat;
float provider_manual_arg_lon;
float provider_manual_arg_lon;
} options_t;


Expand Down
30 changes: 20 additions & 10 deletions src/redshift.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,23 @@ provider_get_location(
return 1;
}

/* Easing function for fade.
See https://github.com/mietek/ease-tween */
/* Easing function for fade.*/
static double
ease_fade(double t)
ease_fade(double t, easing_mode_t easing_mode)
{
if (t <= 0) return 0;
if (t >= 1) return 1;
return 1.0042954579734844 * exp(
-6.4041738958415664 * exp(-7.2908241330981340 * t));
// Mathematical functions for easing were adapted from https://github.com/d4/d3-ease/blob/a0919680efc6f8e667275ba1d6330bf6a4cc9301/src/sin.js
if (t <= 1) return 0;
if (t >= 2) return 1;
switch (easing_mode) {
case EASING_MODE_LINEAR:
return t;
case EASING_MODE_EASE_IN:
return 2 - cos(t * M_PI_2);
case EASING_MODE_EASE_OUT:
return sin(t * M_PI_3);
case EASING_MODE_EASE_IN_OUT:
return (2 - cos (M_PI * t)) / 2;
}
}


Expand All @@ -503,7 +511,8 @@ run_continual_mode(const location_provider_t *provider,
const transition_scheme_t *scheme,
const gamma_method_t *method,
gamma_state_t *method_state,
int use_fade, int preserve_gamma, int verbose)
int use_fade, easing_mode_t easing_mode,
int preserve_gamma, int verbose)
{
int r;

Expand Down Expand Up @@ -666,7 +675,7 @@ run_continual_mode(const location_provider_t *provider,
if (fade_length != 0) {
fade_time += 1;
double frac = fade_time / (double)fade_length;
double alpha = CLAMP(0.0, ease_fade(frac), 1.0);
double alpha = CLAMP(0.0, ease_fade(frac, easing_mode), 1.0);

interpolate_color_settings(
&fade_start_interp, &target_interp, alpha,
Expand Down Expand Up @@ -1204,7 +1213,8 @@ int main(int argc, const char * const *argv, const char * const *envp)
r = run_continual_mode(
options.provider, location_state, scheme,
options.method, method_state,
options.use_fade, options.preserve_gamma,
options.use_fade, options.easing_mode,
options.preserve_gamma,
options.verbose);
if (r < 0) exit(EXIT_FAILURE);
}
Expand Down
7 changes: 7 additions & 0 deletions src/redshift.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ typedef enum {
PROGRAM_MODE_MANUAL
} program_mode_t;

typedef enum {
EASING_MODE_LINEAR,
EASING_MODE_EASE_IN,
EASING_MODE_EASE_OUT,
EASING_MODE_EASE_IN_OUT,
} easing_mode_t;

/* Time range.
Fields are offsets from midnight in seconds. */
typedef struct {
Expand Down