summaryrefslogtreecommitdiffstats
path: root/color.h
blob: 7c7ab8b0aa79cd2e25446917a5b189eadee0ce80 (plain)
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
#ifndef COLOR_H
#define COLOR_H

#include <stddef.h>
#include <stdbool.h>
#include <math.h>
#include <inttypes.h>

/*
 * CIE xy is a chromatic-only colorspace without intensity
 */
struct cie_xy {
	double x;		/* x chromacity */
	double y;		/* y chromacity */
};

/*
 * CIE XYZ is an intensity-weighted colorspace
 */
struct cie_XYZ {
	double X;		/* X weighted chromacity */
	double Y;		/* Intensity */
	double Z;		/* Z weighted chromacity */
};

/*
 * CIE L*u*v* is close-to-linear-in-hue color space suitable for gradients;
 * it is NOT additive for different intensities (Y coordinates)
 */
struct cie_Luv {
	double L;		/* L*, relative intensity, 0 ≤ L* ≤ 100 */
	double u;		/* u*, -100 ≤ u* ≤ 100 */
	double v;		/* v*, -100 ≤ v* ≤ 100 */
};

/*
 * RGB as a floating-point vector
 */
struct RGB {
	double R, G, B;
};

#ifdef __GNUC__
# define const_func __attribute__((const))
# define pure_func  __attribute__((pure))
#else
# define const_func
# define pure_func
#endif

struct cie_xy const_func XYZ_to_xy(struct cie_XYZ XYZ);
struct cie_XYZ const_func xyY_to_XYZ(struct cie_xy xy, double Y);
struct cie_Luv const_func XYZ_to_Luv(struct cie_XYZ XYZ, struct cie_XYZ white);
struct cie_XYZ const_func Luv_to_XYZ(struct cie_Luv Luv, struct cie_XYZ white);
struct cie_xy pure_func spectral(double nm);
struct cie_XYZ pure_func planckian(double T);
struct RGB pure_func XYZ_to_RGB(struct cie_XYZ XYZ);
struct cie_XYZ pure_func RGB_to_XYZ(struct RGB RGB);
void RGBXYZ_init(void);
struct RGB const_func XYZ_to_sRGB(struct cie_XYZ XYZ);
struct cie_XYZ const_func sRGB_to_XYZ(struct RGB sRGB);
/* y = A x */
static inline void color_mat_multiply(double y[3], const double A[3][3],
				      const double x[3])
{
	y[0] = A[0][0] * x[0] + A[0][1] * x[1] + A[0][2] * x[2];
	y[1] = A[1][0] * x[0] + A[1][1] * x[1] + A[1][2] * x[2];
	y[2] = A[2][0] * x[0] + A[2][1] * x[1] + A[2][2] * x[2];
}
void color_mat_inverse(double X[3][3], const double A[3][3]); /* X = A^-1 */

#endif /* COLOR_H */