diff options
-rw-r--r-- | Makefile | 20 | ||||
-rw-r--r-- | blackbody.c | 252 | ||||
-rw-r--r-- | generate-table.c | 2 | ||||
-rw-r--r-- | libred.7 | 3 | ||||
-rw-r--r-- | libred.h | 81 | ||||
-rw-r--r-- | libred.h.0 | 21 | ||||
-rw-r--r-- | libred_get_colour.3 | 45 | ||||
l--------- | libred_get_colour_xy.3 | 1 | ||||
-rw-r--r-- | libred_get_temperature.3 | 89 | ||||
l--------- | libred_get_temperature_xy.3 | 1 | ||||
-rw-r--r-- | libred_solar_elevation.3 | 11 | ||||
-rw-r--r-- | solar.c | 70 |
12 files changed, 510 insertions, 86 deletions
@@ -11,7 +11,7 @@ include mk/$(OS).mk LIB_MAJOR = 1 -LIB_MINOR = 0 +LIB_MINOR = 1 LIB_VERSION = $(LIB_MAJOR).$(LIB_MINOR) @@ -22,9 +22,15 @@ OBJ =\ LOBJ = $(OBJ:.o=.lo) MAN0 = libred.h.0 -MAN3 = libred_get_colour.3 libred_solar_elevation.3 MAN7 = libred.7 +MAN3 =\ + libred_get_colour.3\ + libred_get_colour_xy.3\ + libred_get_temperature.3\ + libred_get_temperature_xy.3\ + libred_solar_elevation.3 + all: libred.a libred.$(LIBEXT) solar.o: libred.h @@ -32,7 +38,9 @@ blackbody.o: 10deg-xy.i 10deg-rgb.i libred.h generate-table.o: blackbody.c 10deg-xy.i libred.h 10deg-xy.i: 10deg - sed -e 's/^/{/' -e 's/ /, /' -e 's/$$/},/' < 10deg | sed '$$s/,$$//' > $@ + sed '56s/^.*$$/0.312727 0.329023/' < 10deg |\ + sed -e 's/^/{/' -e 's/ /, /' -e 's/$$/},/' | \ + sed '$$s/,$$//' > $@ 10deg-rgb.i: generate-table 10deg ./generate-table > $@ @@ -66,9 +74,9 @@ install: libred.a libred.$(LIBEXT) ln -sf -- libred.$(LIBMINOREXT) "$(DESTDIR)$(PREFIX)/lib/libred.$(LIBMAJOREXT)" ln -sf -- libred.$(LIBMAJOREXT) "$(DESTDIR)$(PREFIX)/lib/libred.$(LIBEXT)" cp -- libred.h "$(DESTDIR)$(PREFIX)/include" - cp -- $(MAN0) "$(DESTDIR)$(MANPREFIX)/man0" - cp -- $(MAN3) "$(DESTDIR)$(MANPREFIX)/man3" - cp -- $(MAN7) "$(DESTDIR)$(MANPREFIX)/man7" + cp -P -- $(MAN0) "$(DESTDIR)$(MANPREFIX)/man0" + cp -P -- $(MAN3) "$(DESTDIR)$(MANPREFIX)/man3" + cp -P -- $(MAN7) "$(DESTDIR)$(MANPREFIX)/man7" uninstall: -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libred.a" diff --git a/blackbody.c b/blackbody.c index 6edc85a..1abf133 100644 --- a/blackbody.c +++ b/blackbody.c @@ -11,6 +11,7 @@ # include <stddef.h> + /** * Colour temperatures in CIE xy (xyY without Y) */ @@ -26,6 +27,7 @@ static struct rgb {double r, g, b;} rgb_table[] = { }; + #endif /** * Convert from CIE xyY to [0, 1] sRGB @@ -41,9 +43,16 @@ static struct rgb {double r, g, b;} rgb_table[] = { 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)) +#define LINEAR_TO_SRGB(C)\ + do {\ + double sign = (C) < 0 ? -1 : 1;\ + (C) *= sign;\ + (C) = (((C) <= 0.0031306684425217108) ? (12.92 * (C)) : (1.055 * pow((C), 1.0 / 2.4) - 0.055));\ + (C) *= sign;\ + } while (0) + double X, Z, max; - + #if __GNUC__ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wfloat-equal" @@ -55,13 +64,16 @@ ciexyy_to_srgb(double x, double y, double Y, double *r, double *g, double *b) # pragma GCC diagnostic pop #endif - /* 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); + /* Convert CIE XYZ to [0, 1] linear RGB */ + *r = ( 3.24044625464773750067593027779366821050643920898438 * X) + + (-1.53713476182008057513428411766653880476951599121094 * Y) + + (-0.49853019302272871815517873983480967581272125244141 * Z); + *g = (-0.96926660624467975146956177923129871487617492675781 * X) + + ( 1.87601195978837020916785149893257766962051391601562 * Y) + + ( 0.04155604221443006535130493261931405868381261825562 * Z); + *b = ( 0.05564350356435283223577314970498264301568269729614 * X) + + (-0.20402617973596023914772956686647376045584678649902 * Y) + + ( 1.05722656772270329206264705135254189372062683105469 * Z); /* Adjust colours for use */ max = fmax(fmax(fabs(*r), fabs(*g)), fabs(*b)); @@ -76,70 +88,124 @@ ciexyy_to_srgb(double x, double y, double Y, double *r, double *g, double *b) *r = *r > 0.0 ? *r : 0.0; *g = *g > 0.0 ? *g : 0.0; *b = *b > 0.0 ? *b : 0.0; + + /* Convert [0, 1] linear RGB to [0, 1] sRGB */ + LINEAR_TO_SRGB(*r); + LINEAR_TO_SRGB(*g); + LINEAR_TO_SRGB(*b); } #ifndef LIBRED_COMPILING_PARSER + +/** + * Convert from [0, 1] sRGB to CIE xyY + * + * @param r The “red” value + * (Seriously, sRGB red is orange, just look at it fullscreen) + * @param g The green value + * @param b The blue value + * @param x Output parameter for The 'x' component + * @param y Output parameter for The 'y' component + * @param Y Output parameter for The 'Y' component + */ +static void +srgb_to_ciexyy(double r, double g, double b, double *x, double *y, double *Y) +{ +#define SRGB_TO_LINEAR(C)\ + do {\ + double sign = (C) < 0 ? -1 : 1;\ + (C) *= sign;\ + (C) = (((C) <= 0.0031306684425217108 * 12.92) ? ((C) / 12.92) : pow(((C) + 0.055) / 1.055, 2.4));\ + (C) *= sign;\ + } while (0) + + /* Convert [0, 1] sRGB to [0, 1] linear RGB */ + double s, z; + + SRGB_TO_LINEAR(r); + SRGB_TO_LINEAR(g); + SRGB_TO_LINEAR(b); + + /* Convert [0, 1] linear RGB to CIE XYZ */ + *x = 0.41245744558236757670854899515688885003328323364258 * r + + 0.35757586524551587814357844763435423374176025390625 * g + + 0.18043724782639966597308500695362454280257225036621 * b; + *y = 0.21267337037840827740353688568575307726860046386719 * r + + 0.71515173049103175628715689526870846748352050781250 * g + + 0.07217489913055986916479156434434116818010807037354 * b; + z = 0.01933394276167346020889326041469757910817861557007 * r + + 0.11919195508183859366635459764438564889132976531982 * g + + 0.95030283855237174250873977143783122301101684570312 * b; + *Y = z; + + /* Convert CIE XYZ to CIE xyY */ + *Y = *y; + s = *x + *y + z; + *x /= s; + *y /= s; + if (!isfinite(*x) || !isfinite(*y)) + *x = *y = 0; +} + + /** * 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 + * between the CIE xy values for two colour temperatures. + * 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 + * @param x Output parameter for the CIE x value + * @param y Output parameter for the CIE y value */ static void -interpolate(double x1, double y1, double x2, double y2, double temp, double *r, double *g, double *b) +interpolate(double x1, double y1, double x2, double y2, double temp, double *x, double *y) { 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); + *x = x1 * (1 - weight) + x2 * weight; + *y = y1 * (1 - weight) + y2 * weight; } -/** - * Get the [0, 1] sRGB values of a colour temperature - * - * libred has a table of colour temperature values, this - * function interpolates values that are missing. If you - * don't want any interpolation the `temp` parameter can - * be specified in one of the following ways: - * - * - floor: - * (temp - LIBRED_LOWEST_TEMPERATURE) / - * LIBRED_DELTA_TEMPERATURE * - * LIBRED_DELTA_TEMPERATURE + - * LIBRED_LOWEST_TEMPERATURE - * - * - ceiling: - * (temp - LIBRED_LOWEST_TEMPERATURE + - * LIBRED_DELTA_TEMPERATURE - 1) / - * LIBRED_DELTA_TEMPERATURE * - * LIBRED_DELTA_TEMPERATURE + - * LIBRED_LOWEST_TEMPERATURE - * - * - round to nearest: - * (temp - LIBRED_LOWEST_TEMPERATURE + - * LIBRED_DELTA_TEMPERATURE / 2) / - * LIBRED_DELTA_TEMPERATURE * - * LIBRED_DELTA_TEMPERATURE + - * LIBRED_LOWEST_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 EDOM The selected temperature is below 1000K - */ + +int +libred_get_colour_xy(long int temp, double *x, double *y) +{ + double x1, y1, x2, y2; + size_t i; + long int tmp; + + if (temp > LIBRED_HIGHEST_TEMPERATURE) + temp = LIBRED_HIGHEST_TEMPERATURE; + + if (temp < LIBRED_LOWEST_TEMPERATURE) { + errno = EDOM; + return -1; + } + + tmp = temp - LIBRED_LOWEST_TEMPERATURE; + + i = (size_t)(tmp / LIBRED_DELTA_TEMPERATURE); + if (tmp % 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, x, y); + } else { + *x = xy_table[i].x; + *y = xy_table[i].y; + } + + return 0; +} + + int libred_get_colour(long int temp, double *r, double *g, double *b) { @@ -156,14 +222,16 @@ libred_get_colour(long int temp, double *r, double *g, double *b) } tmp = temp - LIBRED_LOWEST_TEMPERATURE; - + i = (size_t)(tmp / LIBRED_DELTA_TEMPERATURE); if (tmp % LIBRED_DELTA_TEMPERATURE) { + double x, y; 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); + interpolate(x1, y1, x2, y2, (double)temp, &x, &y); + ciexyy_to_srgb(x, y, 1, r, g, b); } else { *r = rgb_table[i].r; *g = rgb_table[i].g; @@ -172,6 +240,76 @@ libred_get_colour(long int temp, double *r, double *g, double *b) return 0; } + + +double +libred_get_temperature_xy(double x, double y, double *x_error, double *y_error) +{ + size_t i = 0, j; + double x1, y1, x2, y2, dx, dy, xd, yd, t, d2; + double best_temp = -1, best_d2 = INFINITY; + + if (!x_error) + x_error = &dx; + if (!y_error) + y_error = &dy; + + x1 = xy_table[0].x; + y1 = xy_table[0].y; + + for (j = 1; j < sizeof(xy_table) / sizeof(*xy_table); j++) { + x2 = xy_table[j].x; + y2 = xy_table[j].y; + dx = x2 - x1; + dy = y2 - y1; + + t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy); + if (!isfinite(t)) + continue; + t = t < 0 ? 0 : t; + t = t > 1 ? 1 : t; + + xd = dx * t + x1 - x; + yd = dy * t + y1 - y; + d2 = xd * xd + yd * yd; + + if (d2 < best_d2) { + *x_error = xd; + *y_error = yd; + best_d2 = d2; + t *= (double)(j - i); + best_temp = (double)i + t; + } + + i = j; + } + + return best_temp * LIBRED_DELTA_TEMPERATURE + LIBRED_LOWEST_TEMPERATURE; +} + + +double +libred_get_temperature(double r, double g, double b, double *y, + double *r_error, double *g_error, double *b_error) +{ + double tx, ty, luma, x_error, y_error, ret; + + srgb_to_ciexyy(r, g, b, &tx, &ty, &luma); + ret = libred_get_temperature_xy(tx, ty, &x_error, &y_error); + ciexyy_to_srgb(x_error, y_error, 1, &r, &g, &b); + + if (y) + *y = luma; + if (r_error) + *r_error = r; + if (g_error) + *g_error = g; + if (b_error) + *b_error = b; + + return ret; +} + #endif diff --git a/generate-table.c b/generate-table.c index c5c0ca3..a8cde38 100644 --- a/generate-table.c +++ b/generate-table.c @@ -18,7 +18,7 @@ static struct xy {double x, y;} xy_table[] = { #include "10deg-xy.i" }; -/* define ciexyy_to_srgb() and adjust_luma() */ +/* define ciexyy_to_srgb() */ #define LIBRED_COMPILING_PARSER #include "blackbody.c" @@ -7,6 +7,9 @@ is a C library for calculating the Sun's apparent elevation and colours temperatures. .SH SEE ALSO .BR libred.h (0), +.BR libred_get_colour (3), +.BR libred_get_temperature (3), +.BR libred_solar_elevation (3), .BR solar-python (7), .BR radharc (1), .BR redshift (1), @@ -3,6 +3,9 @@ #define LIBRED_H +/* bug fix: */ +#define libred_solar_elevation libred_solar_elevation__int + /** * Approximate apparent size of the Sun in degrees @@ -143,7 +146,7 @@ * @return 0 on success, -1 on failure * @throws Any error specified for clock_gettime(3) on error */ -double libred_solar_elevation(double, double, double *); +int libred_solar_elevation(double, double, double *); /** @@ -212,6 +215,82 @@ int libred_check_timetravel(void); */ int libred_get_colour(long int, double *, double *, double *); +/** + * Get the CIE xy values of a colour temperature + * + * libred has a table of colour temperature values, this + * function interpolates values that are missing. If you + * don't want any interpolation the `temp` parameter can + * be specified in one of the following ways: + * + * - floor: + * (temp - LIBRED_LOWEST_TEMPERATURE) / + * LIBRED_DELTA_TEMPERATURE * + * LIBRED_DELTA_TEMPERATURE + + * LIBRED_LOWEST_TEMPERATURE + * + * - ceiling: + * (temp - LIBRED_LOWEST_TEMPERATURE + + * LIBRED_DELTA_TEMPERATURE - 1) / + * LIBRED_DELTA_TEMPERATURE * + * LIBRED_DELTA_TEMPERATURE + + * LIBRED_LOWEST_TEMPERATURE + * + * - round to nearest: + * (temp - LIBRED_LOWEST_TEMPERATURE + + * LIBRED_DELTA_TEMPERATURE / 2) / + * LIBRED_DELTA_TEMPERATURE * + * LIBRED_DELTA_TEMPERATURE + + * LIBRED_LOWEST_TEMPERATURE + * + * @param temp The desired colour temperature + * @param x Output parameter for the x value + * @param y Output parameter for the y value + * @return 0 on succeess, -1 on error + * + * @throws EDOM The selected temperature is below 1000K + */ +int libred_get_colour_xy(long int, double *, double *); + +/** + * Calculate the colour temperature from its [0, 1] sRGB values + * + * @param r The sRGB “red” value for the colour temperature + * @param g The sRGB green value for the colour temperature + * @param b The sRGB blue value for the colour temperature + * @param y Output parameter for the CIE Y value for the + * provided sRGB colour, the sRGB error values must + * be multiplied by this value to correct them to + * match the brightness of the input sRGB colour; + * may be `NULL` + * @param r_error Output parameter for the absolute error in the + * “red” value of the returned colour temperature; + * may be `NULL` + * @param g_error Output parameter for the absolute error in the + * green value of the returned colour temperature; + * may be `NULL` + * @param b_error Output parameter for the absolute error in the + * blue value of the returned colour temperature; + * may be `NULL` + * @return The closest matching colour temperature + */ +double libred_get_temperature(double, double, double, double *, double *, double *, double *); + +/** + * Calculate the colour temperature from its CIE xy values + * + * @param x The CIE x value for the colour temperature + * @param y The CIE y value for the colour temperature + * @param x_error Output parameter for the absolute error in + * the CIE x value of the returned colour + * temperature; may be `NULL` + * @param y_error Output parameter for the absolute error in + * the CIE y value of the returned colour + * temperature; may be `NULL` + * @return The closest matching colour temperature + */ +double libred_get_temperature_xy(double, double, double *, double *); + #endif @@ -8,7 +8,8 @@ libred.h \- Solar elevation and blackbody colour calculation .fi .PP Link with -.IR -lred . +.I -lred +.IR -lm . .SH DESCRIPTION The .I <libred.h> @@ -116,11 +117,21 @@ It is night only if the Sun's apparent elevation is negative. This header defines the following functions: .TP * -.BR libred_solar_elevation (3) +.BR libred_get_colour (3) .TP * -.BR libred_get_colour (3) +.BR libred_get_colour_xy (3) +.TP +* +.BR libred_get_temperature (3) +.TP +* +.BR libred_get_temperature_xy (3) +.TP +* +.BR libred_solar_elevation (3) .SH SEE ALSO .BR libred (7), -.BR libred_solar_elevation (3), -.BR libred_get_colour (3) +.BR libred_get_colour (3), +.BR libred_get_temperature (3), +.BR libred_solar_elevation (3) diff --git a/libred_get_colour.3 b/libred_get_colour.3 index afd0a7f..d6649d2 100644 --- a/libred_get_colour.3 +++ b/libred_get_colour.3 @@ -1,20 +1,23 @@ .TH LIBRED_GET_COLOUR 3 LIBRED .SH NAME -libred_get_colour \- Calculate a colour temperature +libred_get_colour \- Calculate the colour of a temperature .SH SYNOPSIS .nf #include <libred.h> int \fBlibred_get_colour\fP(long int \fItemp\fP, double *\fIr\fP, double *\fIg\fP, double *\fIb\fP); +int \fBlibred_get_colour_xy\fP(long int \fItemp\fP, double *\fIx\fP, double *\fIy\fP); .fi .PP Link with -.IR -lred . +.I -lred +.IR -lm . .SH DESCRIPTION .BR libred_get_colour () gets or interpolates the colour temperature for .I temp -kelvins, and returns the colour temperature in sRGB. The values, +kelvins, and returns the colour temperature in sRGB, with the +sRGB transfer function applied. The values, between 0.0 and 1.0, for the \(dqred\(dq, green, and blue channels are stored in .IR *r , @@ -49,14 +52,45 @@ ceiling .TP round to nearest .I (temp - LIBRED_LOWEST_TEMPERATURE + LIBRED_DELTA_TEMPERATURE / 2) / LIBRED_DELTA_TEMPERATURE * LIBRED_DELTA_TEMPERATURE + LIBRED_LOWEST_TEMPERATURE +.PP +The +.BR libred_get_colour_xy () +function is the same as the +.BR libred_get_colour () +except it uses the CIE xy colour space instead of sRGB +(of course without any transfer function). This means +that the chroma values x and y are stored in +.I *x +and +.IR *y , +and the CIE Y value shall be assumed to be 1 for +equivalence with the +.BR libred_get_colour () +function. +.PP +Neither +.IR r , +.IR g , +.IR b , +.IR x , +nor +.I y +may be +.IR NULL . .SH RETURN VALUE Upon successful completion, the .BR libred_get_colour () +and +.BR libred_get_colour_xy () function returns 0. On failure, the function returns -1 and sets .I errno to indicate the error. .SH ERRORS -The function may fail if: +The +.BR libred_get_colour () +and +.BR libred_get_colour_xy () +functions may fail if: .TP .B EDOM If @@ -67,4 +101,5 @@ is less than .SH SEE ALSO .BR libred.h (0), .BR libred (7), -.BR libred_get_solar_elevation (3) +.BR libred_get_temperature (3), +.BR libred_solar_elevation (3) diff --git a/libred_get_colour_xy.3 b/libred_get_colour_xy.3 new file mode 120000 index 0000000..1d7ee17 --- /dev/null +++ b/libred_get_colour_xy.3 @@ -0,0 +1 @@ +libred_get_colour.3
\ No newline at end of file diff --git a/libred_get_temperature.3 b/libred_get_temperature.3 new file mode 100644 index 0000000..f5e8883 --- /dev/null +++ b/libred_get_temperature.3 @@ -0,0 +1,89 @@ +.TH LIBRED_GET_TEMPERATURE 3 LIBRED +.SH NAME +libred_get_temperature \- Calculate the temperature of a colour +.SH SYNOPSIS +.nf +#include <libred.h> + +double \fBlibred_get_temperature\fP(double \fIr\fP, double \fIg\fP, double \fIb\fP, double *\fIY\fP, + double *\fIr_error\fP, double *\fIg_error\fP, double *\fIb_error\fP); +double \fBlibred_get_temperature_xy\fP(double \fIx\fP, double \fIy\fP, double *\fIx_error\fP, double *\fIy_error\fP); +.fi +.PP +Link with +.I -lred +.IR -lm . +.SH DESCRIPTION +The +.BR libred_get_temperature () +function parses +.RI ( r , +.IR g , +.IR b ) +as a colour encoded in sRGB with values between 0 and 1 +and with the sRGB transfer function applied. From that +it calculates the CIE Y (luminosity) of the colour and +stores it in +.I *Y +and then normalises the the colour to have Y = 1 and +calculates the closest matching colour temperature, +which is returned. +.PP +The calculated temperature may not line perfectly with +the colour so the difference between the colour of the +returned temperature and the input colour (after +normalisation to Y = 1) is returned in sRGB as +.RI ( *r_error ", " *g_error ", " *b_error ), +interpreted the same way as +.IR r , +.IR g , +and +.IR b . +.PP +The +.BR libred_get_temperature_xy () +function is a variant of the +.BR libred_get_temperature () +function that uses the CIE xy colour space +(CIE xyY with Y = 1). Unlike sRGB, CIE xy +does not have a transfer function, so the values +are linear, and the values also do not have any +limits and does have a natural interpretation. +.PP +Any of +.IR Y , +.IR r_error , +.IR g_error , +.IR b_error , +.IR x_error , +and +.IR y_error +that is +.I NULL +is ignored. +.SH RETURN VALUE +The +.BR libred_get_temperature () +and +.BR libred_get_temperature_xy () +functions return the colour temperature in Kelvins. +.SH ERRORS +None. +.SH NOTES +The sRGB transfer function is applied to +.IR *r_error , +.IR *g_error , +and +.IR *b_error . +One cannoy simply subtract them from +.IR r , +.IR g , +and +.IR b , +without first undoing the transfer function, +to get the colour or the calculated temperature. +.SH SEE ALSO +.BR libred.h (0), +.BR libred (7), +.BR libred_get_temperature (3), +.BR libred_solar_elevation (3) diff --git a/libred_get_temperature_xy.3 b/libred_get_temperature_xy.3 new file mode 120000 index 0000000..b7a4244 --- /dev/null +++ b/libred_get_temperature_xy.3 @@ -0,0 +1 @@ +libred_get_temperature.3
\ No newline at end of file diff --git a/libred_solar_elevation.3 b/libred_solar_elevation.3 index 9bb4b0a..fa2df7e 100644 --- a/libred_solar_elevation.3 +++ b/libred_solar_elevation.3 @@ -5,11 +5,12 @@ libred_solar_elevation \- Calculate the Sun's apparent elevation .nf #include <libred.h> -int \fBlibred_solar_elevation\fP(double \fIlatitude\fP, double \fIlongitude\fP, double *\felevation\fP); +int \fBlibred_solar_elevation\fP(double \fIlatitude\fP, double \fIlongitude\fP, double *\fIelevation\fP); .fi .PP Link with -.IR -lred . +.I -lred +.IR -lm . .SH DESCRIPTION .BR libred_solar_elevation () calculates the Sun's elevation, and stores it, measured in degrees, in @@ -27,11 +28,13 @@ Other values may or may not work, no error is thrown if used. .SH RETURN VALUE Upon successful completion, the .BR libred_solar_elevation () -return 0, on failure it returns -1 and sets +function returns 0, on failure it returns -1 and sets .I errno to indicate the error. .SH ERRORS -The function may fail for any reason specified for +The +.BR libred_solar_elevation () +function may fail for any reason specified for .BR clock_gettime (3). .SH SEE ALSO .BR libred.h (0), @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libred.h" +#undef libred_solar_elevation #include <math.h> #include <time.h> #include <errno.h> @@ -21,6 +22,50 @@ #endif +/* old erroneous function declarations: */ +double libred_solar_elevation(double latitude, double longitude, double *elevation); + + +#if !defined(_POSIX_TIMERS) +# ifndef CLOCK_REALTIME_COARSE +# define CLOCK_REALTIME_COARSE 0 +# endif +# ifndef CLOCK_REALTIME +# define CLOCK_REALTIME 0 +# endif +# if defined(_WIN32) +# include <windows.h> +int +clock_gettime(int clockid, struct timespec *ts) +{ + /* https://learn.microsoft.com/en-us/windows/win32/sysinfo/file-times */ + FILETIME ft; + ULARGE_INTEGER u64; + (void) clockid; + GetSystemTimePreciseAsFileTime(&ft); + u64.LowPart = ft.dwLowDateTime; + u64.HighPart = ft.dwHighDateTime; + ts->tv_sec = (time_t)(uli.QuadPart / 100000000ULL - 11644473600ULL); + ts->tv_nsec = (long)(uli.QuadPart % 100000000ULL * 10ULL); + return 0; +} +# else +# include <sys/time.h> +int +clock_gettime(int clockid, struct timespec *ts) +{ + struct timeval tv; + (void) clockid; + if (gettimeofday(&tv, NULL)) + return -1; + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000; + return 0; +} +# endif +#endif + + /** * Get current Julian Centuries time (100 Julian Days since J2000) * and Julian Day time @@ -163,7 +208,7 @@ static double sun_equation_of_centre(double t) { double a = sun_geometric_mean_anomaly(t), r; - r = sin(1.0 * a) * fma(fma(-0.000014, t, 0.004817), t, 1.914602); + r = sin(1.0 * a) * fma(fma(-0.000014, t, -0.004817), t, 1.914602); r = fma(sin(2.0 * a), fma(-0.000101, t, 0.019993), r); r = fma(sin(3.0 * a), 0.000289, r); return radians(r); @@ -241,7 +286,7 @@ solar_declination(double t) * between apparent and mean solar time * * @param t The time in Julian Centuries - * @return The equation of time, in degrees + * @return The equation of time, in minutes of time */ static double equation_of_time(double t) @@ -249,13 +294,14 @@ equation_of_time(double t) double l = sun_geometric_mean_longitude(t); double e = earth_orbit_eccentricity(t); double m = sun_geometric_mean_anomaly(t); - double y = pow(tan(corrected_mean_ecliptic_obliquity(t) / 2.0), 2.0); + double y = tan(corrected_mean_ecliptic_obliquity(t) / 2.0); double r, c, s; + y *= y; s = y * sin(2.0 * l); c = y * cos(2.0 * l); r = fma(fma(4.0, c, -2.0), e * sin(m), s); - r = fma(pow(0.50 * y, 2.0), sin(-4.0 * l), r); - r = fma(pow(1.25 * e, 2.0), sin(-2.0 * m), r); + r = fma(-0.5 * y*y, sin(4.0 * l), r); + r = fma(-1.25 * e*e, sin(2.0 * m), r); return 4.0 * degrees(r); } @@ -296,8 +342,8 @@ solar_elevation_from_time(double tc, double td, double latitude, double longitud * @return 0 on success, -1 on failure * @throws Any error specified for clock_gettime(3) on error */ -double -libred_solar_elevation(double latitude, double longitude, double *elevation) +int +libred_solar_elevation__int(double latitude, double longitude, double *elevation) { double tc, td; if (julian_time(&tc, &td)) @@ -307,6 +353,16 @@ libred_solar_elevation(double latitude, double longitude, double *elevation) } /** + * Kept for binary compatibility + */ +double +libred_solar_elevation(double latitude, double longitude, double *elevation) +{ + int r = libred_solar_elevation__int(latitude, longitude, elevation); + return (double)r; +} + +/** * This function is obsolete */ int |