Skip to content

Commit 485c19e

Browse files
committed
Allow individual adjustment of each gamma channel.
1 parent e63ea01 commit 485c19e

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

colortemp.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ colortemp_check_extension()
153153
}
154154

155155
int
156-
colortemp_set_temperature(int temp, float gamma)
156+
colortemp_set_temperature(int temp, float gamma[3])
157157
{
158158
xcb_generic_error_t *error;
159159

@@ -226,11 +226,11 @@ colortemp_set_temperature(int temp, float gamma)
226226
uint16_t *gamma_b = &gamma_ramps[2*gamma_ramp_size];
227227

228228
for (int i = 0; i < gamma_ramp_size; i++) {
229-
gamma_r[i] = pow((float)i/gamma_ramp_size, 1.0/gamma) *
229+
gamma_r[i] = pow((float)i/gamma_ramp_size, 1.0/gamma[0]) *
230230
UINT16_MAX * white_point[0];
231-
gamma_g[i] = pow((float)i/gamma_ramp_size, 1.0/gamma) *
231+
gamma_g[i] = pow((float)i/gamma_ramp_size, 1.0/gamma[1]) *
232232
UINT16_MAX * white_point[1];
233-
gamma_b[i] = pow((float)i/gamma_ramp_size, 1.0/gamma) *
233+
gamma_b[i] = pow((float)i/gamma_ramp_size, 1.0/gamma[2]) *
234234
UINT16_MAX * white_point[2];
235235
}
236236

colortemp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
#define _COLORTEMP_H
55

66
int colortemp_check_extension();
7-
int colortemp_set_temperature(int temp, float gamma);
7+
int colortemp_set_temperature(int temp, float gamma[3]);
88

99
#endif /* ! _COLORTEMP_H */

redshift.c

+28-5
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
#define HELP \
4242
USAGE \
4343
" Set color temperature of display according to time of day.\n" \
44-
" -g GAMMA\tAdditional gamma correction to apply\n" \
44+
" -g R:G:B\tAdditional gamma correction to apply\n" \
4545
" -h\t\tDisplay this help message\n" \
4646
" -l LAT:LON\tYour current location\n" \
47-
" -t DAY:NIGHT\tColor temperature to set at night/day\n" \
47+
" -t DAY:NIGHT\tColor temperature to set at daytime/night\n" \
4848
" -v\t\tVerbose output\n"
4949

5050
/* DEGREE SIGN is Unicode U+00b0 */
@@ -74,15 +74,34 @@ main(int argc, char *argv[])
7474
float lon = NAN;
7575
int temp_day = DEFAULT_DAY_TEMP;
7676
int temp_night = DEFAULT_NIGHT_TEMP;
77-
float gamma = DEFAULT_GAMMA;
77+
float gamma[3] = { DEFAULT_GAMMA, DEFAULT_GAMMA, DEFAULT_GAMMA };
7878
int verbose = 0;
7979
char *s;
8080

8181
int opt;
8282
while ((opt = getopt(argc, argv, "g:hl:t:v")) != -1) {
8383
switch (opt) {
8484
case 'g':
85-
gamma = atof(optarg);
85+
s = strchr(optarg, ':');
86+
if (s == NULL) {
87+
/* Use value for all channels */
88+
float g = atof(optarg);
89+
gamma[0] = gamma[1] = gamma[2] = g;
90+
} else {
91+
/* Parse separate value for each channel */
92+
*(s++) = '\0';
93+
gamma[0] = atof(optarg); /* Red */
94+
char *g_s = s;
95+
s = strchr(s, ':');
96+
if (s == NULL) {
97+
fprintf(stderr, USAGE, argv[0]);
98+
exit(EXIT_FAILURE);
99+
}
100+
101+
*(s++) = '\0';
102+
gamma[1] = atof(g_s); /* Blue */
103+
gamma[2] = atof(s); /* Green */
104+
}
86105
break;
87106
case 'h':
88107
printf(HELP, argv[0]);
@@ -160,7 +179,9 @@ main(int argc, char *argv[])
160179
}
161180

162181
/* Gamma */
163-
if (gamma < MIN_GAMMA || gamma > MAX_GAMMA) {
182+
if (gamma[0] < MIN_GAMMA || gamma[0] > MAX_GAMMA ||
183+
gamma[1] < MIN_GAMMA || gamma[1] > MAX_GAMMA ||
184+
gamma[2] < MIN_GAMMA || gamma[2] > MAX_GAMMA) {
164185
fprintf(stderr, "Gamma value must be between %.1f and %.1f.\n",
165186
MIN_GAMMA, MAX_GAMMA);
166187
exit(EXIT_FAILURE);
@@ -194,6 +215,8 @@ main(int argc, char *argv[])
194215

195216
if (verbose) {
196217
printf("Color temperature: %uK\n", temp);
218+
printf("Gamma: %.3f, %.3f, %.3f\n",
219+
gamma[0], gamma[1], gamma[2]);
197220
}
198221

199222
/* Set color temperature */

0 commit comments

Comments
 (0)