aboutsummaryrefslogtreecommitdiffstats
path: root/blackbody.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-10-06 14:54:42 +0200
committerMattias Andrée <maandree@kth.se>2019-10-06 14:54:42 +0200
commitf5b396dd73ea28506286e0be09d5e76e802222a2 (patch)
treecfc285a83639e78858d954cf82815cefe10a55ce /blackbody.c
parentClean up (diff)
downloadlibred-f5b396dd73ea28506286e0be09d5e76e802222a2.tar.gz
libred-f5b396dd73ea28506286e0be09d5e76e802222a2.tar.bz2
libred-f5b396dd73ea28506286e0be09d5e76e802222a2.tar.xz
Simplify
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--blackbody.c186
1 files changed, 78 insertions, 108 deletions
diff --git a/blackbody.c b/blackbody.c
index 104e7da..9dfe1a8 100644
--- a/blackbody.c
+++ b/blackbody.c
@@ -4,173 +4,143 @@
# pragma GCC diagnostic ignored "-Wunsuffixed-float-constants"
#endif
-
#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;
-
+#include <math.h>
+#include <stddef.h>
-/**
- * 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;
-}
+struct xy {double x, y;};
+struct rgb {double r, g, b;};
+static struct xy xy_table[] = {
+#include "10deg-xy.i"
+};
-/**
- * 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;
-}
+static struct rgb rgb_table[] = {
+#include "10deg-rgb.i"
+};
#endif
/**
- * Convert from CIE xyY to [0, 1] sRGB.
+ * 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.
+ * @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;
+ double X, Z, max;
#if __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
- /* Convert CIE xyY to CIE XYZ. */
+ /* 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));
#if __GNUC__
# pragma GCC diagnostic pop
#endif
- /* Convert CIE XYZ to [0, 1] linear RGB. (ciexyz_to_linear) */
+ /* 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. */
+ /* Convert [0, 1] linear RGB to [0, 1] sRGB */
SRGB(*r), SRGB(*g), SRGB(*b);
+
+ /* Adjust colours for use */
+ max = fmax(fmax(fabs(*r), fabs(*g)), fabs(*b));
+#if __GNUC__
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wfloat-equal"
+#endif
+ if (max != 0.0) *r /= max, *g /= max, *b /= max;
+#if __GNUC__
+# pragma GCC diagnostic pop
+#endif
+ *r = *r > 0.0 ? *r : 0.0;
+ *g = *g > 0.0 ? *g : 0.0;
+ *b = *b > 0.0 ? *b : 0.0;
}
#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.
+ * 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.
+ * @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 weight = fmod(temp - (LIBRED_LOWEST_TEMPERATURE % LIBRED_DELTA_TEMPERATURE),
+ (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.
+ * Get the [0, 1] sRGB values of a colour temperature
*
- * @throws EOVERFLOW The file did not have the expected size.
- * @throws EDOM The selected temperature is below 1000 K.
- * @throws Any error specified for pread(3).
+ * @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 EDOM The selected temperature is below 1000 K
*/
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 = EOVERFLOW; 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 __GNUC__
-# pragma GCC diagnostic push
-# pragma GCC diagnostic ignored "-Wfloat-equal"
-#endif
- if (max != 0.0) *r /= max, *g /= max, *b /= max;
-#if __GNUC__
-# pragma GCC diagnostic pop
-#endif
- *r = *r > 0.0 ? *r : 0.0;
- *g = *g > 0.0 ? *g : 0.0;
- *b = *b > 0.0 ? *b : 0.0;
+ double x1, y1, x2, y2;
+ size_t i;
+
+ if (temp > LIBRED_HIGHEST_TEMPERATURE)
+ temp = LIBRED_HIGHEST_TEMPERATURE;
+
+ if (temp < LIBRED_LOWEST_TEMPERATURE) {
+ errno = EDOM;
+ return -1;
+ }
+
+ if (temp % LIBRED_DELTA_TEMPERATURE) { /* TODO make optional */
+ i = (temp - LIBRED_LOWEST_TEMPERATURE) / LIBRED_DELTA_TEMPERATURE;
+ x1 = xy_table[i].x;
+ y1 = xy_table[i].y;
+ x2 = xy_table[i + 1].x;
+ y2 = xy_table[i + 1].y;
+ interpolate(x1, y1, x2, y2, (double)temp, r, g, b);
+ } else {
+ i = (temp - LIBRED_LOWEST_TEMPERATURE) / LIBRED_DELTA_TEMPERATURE;
+ *r = rgb_table[i].r;
+ *g = rgb_table[i].g;
+ *b = rgb_table[i].b;
+ }
return 0;
-fail:
- return -1;
}
#endif