aboutsummaryrefslogtreecommitdiffstats
path: root/src/blackbody.c
blob: 92597ae2b5d0994f651cbe7f6b3e3efd04e4c6cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * Copyright © 2016  Mattias Andrée <maandree@member.fsf.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef LIBRED_COMPILING_PARSER
#include "libred.h"
#include "macros.h"
#include <math.h>
#include <errno.h>
#include <fcntl.h>



/**
 * The file descriptor to the colour lookup table.
 */
int libred_fd = -1;



/**
 * This function must be called, once,
 * before calling `libred_get_colour`.
 * 
 * @return  0 on success, -1 on error.
 * 
 * @throws  Any error specified for `open(3)`.
 */
int libred_init_colour(void)
{
  libred_term_colour();
  libred_fd = open(SYSDEPRESDIR "/" PACKAGE "/10deg", O_RDONLY);
  return libred_fd < 0 ? -1 : 0;
}


/**
 * Call this when the process will not
 * longer make calls to `libred_get_colour`.
 */
void libred_term_colour(void)
{
  if (libred_fd >= 0)
    close(libred_fd), libred_fd = -1;
}


#endif
/**
 * Convert from CIE xyY to [0, 1] sRGB.
 * 
 * @param  x  The 'x' component.
 * @param  y  The 'y' component.
 * @param  Y  The 'Y' component.
 * @param  r  Output parameter for the “red” value.
 *            (Seriously, sRGB red is orange, just look at it fullscreen.)
 * @param  g  Output parameter for the green value.
 * @param  b  Output parameter for the blue value.
 */
static void ciexyy_to_srgb(double x, double y, double Y, double *r, double *g, double *b)
{
#define SRGB(C)  (((C) <= 0.0031308) ? (12.92 * (C)) : ((1.0 + 0.055) * pow((C), 1.0 / 2.4) - 0.055))
  double X, Z;
  
  /* Convert CIE xyY to CIE XYZ. */
  X = Y * (y == 0.0 ? 0.0 : (x / y));
  Z = Y * (y == 0.0 ? 0.0 : ((1.0 - x - y) / y));
  
  /* Convert CIE XYZ to [0, 1] linear RGB. (ciexyz_to_linear) */
  *r = ( 3.240450 * X) + (-1.537140 * Y) + (-0.4985320 * Z);
  *g = (-0.969266 * X) + ( 1.876010 * Y) + ( 0.0415561 * Z);
  *b = (0.0556434 * X) + (-0.204026 * Y) + ( 1.0572300 * Z);
  
  /* Convert [0, 1] linear RGB to [0, 1] sRGB. */
  SRGB(*r), SRGB(*g), SRGB(*b);
}
#ifndef LIBRED_COMPILING_PARSER


/**
 * Perform linear interpolation (considered very good)
 * between the CIE xyY values for two colour temperatures
 * and convert the result to sRGB. The two colours should
 * be the closest below the desired colour temperature,
 * and the closest above the desired colour temperature.
 * 
 * @param  x1    The 'x' component for the low colour.
 * @param  y1    The 'y' component for the low colour.
 * @param  x2    The 'x' component for the high colour.
 * @param  y2    The 'y' component for the high colour.
 * @param  temp  The desired colour temperature.
 * @param  r     Output parameter for the “red” value.
 * @param  g     Output parameter for the green value.
 * @param  b     Output parameter for the blue value.
 */
static void interpolate(double x1, double y1, double x2, double y2, double temp, double *r, double *g, double *b)
{
  double weight = fmod(temp, (double)LIBRED_DELTA_TEMPERATURE) / (double)LIBRED_DELTA_TEMPERATURE;
  double x = x1 * (1 - weight) + x2 * weight;
  double y = y1 * (1 - weight) + y2 * weight;
  ciexyy_to_srgb(x, y, 1.0, r, g, b);
}


/**
 * Get the [0, 1] sRGB values of a colour temperature.
 * 
 * @param   temp  The desired colour temperature.
 * @param   r     Output parameter for the “red” value.
 * @param   g     Output parameter for the green value.
 * @param   b     Output parameter for the blue value.
 * @return        0 on succeess, -1 on error.
 * 
 * @throws  0     The file did not have the expected size.
 * @throws  EDOM  The selected temperature is below 1000 K.
 * @throws        Any error specified for pread(3).
 */
int libred_get_colour(long int temp, double *r, double *g, double *b)
{
  double values[10]; /* low:x,y,r,g,b + high:x,y,r,g,b */
  off_t offset;
  double max;
  
  /* We do not have any values for above 40 000 K, but
   * the differences will be unnoticeable, perhaps even
   * unencodeable. */
  if (temp > LIBRED_HIGHEST_TEMPERATURE)  temp = LIBRED_HIGHEST_TEMPERATURE;
  /* Things do not glow below 1000 K. Yes, fire is hot! */
  if (temp < LIBRED_LOWEST_TEMPERATURE)   t ((errno = EDOM));
  
  /* Read table. */
  offset = ((off_t)temp - LIBRED_LOWEST_TEMPERATURE) / LIBRED_DELTA_TEMPERATURE;
  offset *= (off_t)(sizeof(values) / 2);
  errno = 0; xpread(libred_fd, values, sizeof(values), offset);
  
  /* Get colour. */
  if (temp % LIBRED_DELTA_TEMPERATURE)
    interpolate(values[0], values[1], values[6], values[7], (double)temp, r, g, b);
  else
    *r = values[2], *g = values[3], *b = values[4];
  
  /* Adjust colours for use. */
  max = fmax(fmax(fabs(*r), fabs(*g)), fabs(*b));
  if (max != 0)  *r /= max, *g /= max, *b /= max;
  *r = *r > 0.0 ? *r : 0.0;
  *g = *g > 0.0 ? *g : 0.0;
  *b = *b > 0.0 ? *b : 0.0;
  
  return 0;
 fail:
  return -1;
}
#endif