aboutsummaryrefslogtreecommitdiffstats
path: root/src/blackbody.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@member.fsf.org>2016-01-04 21:13:38 +0100
committerMattias Andrée <maandree@member.fsf.org>2016-01-04 21:13:38 +0100
commitdb99d62a8f3ac1c09f4105e60f5be32ad008d3a5 (patch)
treeb5e35927e302c830811c4780a8a8584502bd2d95 /src/blackbody.c
parentsplit out from radharc (diff)
downloadlibred-db99d62a8f3ac1c09f4105e60f5be32ad008d3a5.tar.gz
libred-db99d62a8f3ac1c09f4105e60f5be32ad008d3a5.tar.bz2
libred-db99d62a8f3ac1c09f4105e60f5be32ad008d3a5.tar.xz
misc
Signed-off-by: Mattias Andrée <maandree@member.fsf.org>
Diffstat (limited to 'src/blackbody.c')
-rw-r--r--src/blackbody.c145
1 files changed, 88 insertions, 57 deletions
diff --git a/src/blackbody.c b/src/blackbody.c
index abe03a9..f45ead4 100644
--- a/src/blackbody.c
+++ b/src/blackbody.c
@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "blackbody.h"
+#include "libred.h"
#include "macros.h"
#include <math.h>
#include <errno.h>
@@ -22,6 +22,40 @@
/**
+ * The file descriptor to the colour lookup table.
+ */
+static int 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();
+ fd = open(SYSDEPRESDIR "/" PACKAGE "/10deg", O_RDONLY);
+ return fd < 0 ? -1 : 0;
+}
+
+
+/**
+ * Call this when the process will not
+ * longer make calls to `libred_get_colour`.
+ */
+void libred_term_colour(void)
+{
+ if (fd >= 0)
+ close(fd), fd = -1;
+}
+
+
+/**
* Convert from CIE xyY to [0, 1] sRGB.
*
* @param x The 'x' component.
@@ -32,23 +66,22 @@
* @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)
+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);
+ 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);
}
@@ -68,20 +101,18 @@ ciexyy_to_srgb(double x, double y, double Y, double *r, double *g, double *b)
* @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)
+static void interpolate(double x1, double y1, double x2, double y2, double temp, double *r, double *g, double *b)
{
- double weight = fmod(temp, (double)DELTA_TEMPERATURE) / (double)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);
+ double weight = fmod(temp, (double)DELTA_TEMPERATURE) / (double)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 fd File descriptor for the colour table.
* @param temp The desired colour temperature.
* @param r Output parameter for the “red” value.
* @param g Output parameter for the green value.
@@ -90,41 +121,41 @@ interpolate(double x1, double y1, double x2, double y2, double temp, double *r,
*
* @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
-get_colour(int fd, long int temp, double *r, double *g, double *b)
+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 > HIGHEST_TEMPERATURE) temp = HIGHEST_TEMPERATURE;
- /* Things do not glow below 1000 K. Yes, fire is hot! */
- if (temp < LOWEST_TEMPERATURE) t ((errno = EDOM));
-
- /* Read table. */
- offset = ((off_t)temp - LOWEST_TEMPERATURE) / DELTA_TEMPERATURE;
- offset *= (off_t)(sizeof(values) / 2);
- errno = 0; xpread(fd, values, sizeof(values), offset);
-
- /* Get colour. */
- if (temp % 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;
+ 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 > HIGHEST_TEMPERATURE) temp = HIGHEST_TEMPERATURE;
+ /* Things do not glow below 1000 K. Yes, fire is hot! */
+ if (temp < LOWEST_TEMPERATURE) t ((errno = EDOM));
+
+ /* Read table. */
+ offset = ((off_t)temp - LOWEST_TEMPERATURE) / DELTA_TEMPERATURE;
+ offset *= (off_t)(sizeof(values) / 2);
+ errno = 0; xpread(fd, values, sizeof(values), offset);
+
+ /* Get colour. */
+ if (temp % 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;
}