diff options
-rw-r--r-- | libdimension/color.c | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/libdimension/color.c b/libdimension/color.c index 9243a19..25a6826 100644 --- a/libdimension/color.c +++ b/libdimension/color.c @@ -21,33 +21,30 @@ #include "dimension.h" #include <math.h> /* For pow() */ -dmnsn_CIE_XYZ whitepoint = { 0.9505, 1, 1.089 }; +/* sRGB white point (D50) */ +dmnsn_CIE_XYZ whitepoint = { .X = 0.9504060171449392, + .Y = 0.9999085943425312, + .Z = 1.089062231497274 }; dmnsn_color dmnsn_color_from_XYZ(dmnsn_CIE_XYZ XYZ) { - dmnsn_color ret; - ret.X = XYZ.X; - ret.Y = XYZ.Y; - ret.Z = XYZ.Z; - ret.filter = 0.0; - ret.trans = 0.0; + dmnsn_color ret = { .X = XYZ.X, .Y = XYZ.Y, .Z = XYZ.Z, + .filter = 0.0, .trans = 0.0 }; return ret; } dmnsn_color dmnsn_color_from_xyY(dmnsn_CIE_xyY xyY) { - dmnsn_color ret; - ret.X = xyY.Y*xyY.x/xyY.y; - ret.Y = xyY.Y; - ret.Z = xyY.Y*(1.0 - xyY.x - xyY.Y)/xyY.y; - ret.filter = 0.0; - ret.trans = 0.0; + dmnsn_color ret = { .X = xyY.Y*xyY.x/xyY.y, + .Y = xyY.Y, + .Z = xyY.Y*(1.0 - xyY.x - xyY.Y)/xyY.y, + .filter = 0.0, .trans = 0.0 }; return ret; } -static double Lab_finv(double t) { +static double dmnsn_Lab_finv(double t) { if (t > 6.0/29.0) { return t*t*t; } else { @@ -65,9 +62,9 @@ dmnsn_color_from_Lab(dmnsn_CIE_Lab Lab, dmnsn_CIE_XYZ white) fx = fy + Lab.a/500.0; fz = fy - Lab.b/200.0; - ret.X = white.X*Lab_finv(fx); - ret.Y = white.Y*Lab_finv(fy); - ret.Z = white.Z*Lab_finv(fz); + ret.X = white.X*dmnsn_Lab_finv(fx); + ret.Y = white.Y*dmnsn_Lab_finv(fy); + ret.Z = white.Z*dmnsn_Lab_finv(fz); return ret; } @@ -86,14 +83,14 @@ dmnsn_color_from_Luv(dmnsn_CIE_Luv Luv, dmnsn_CIE_XYZ white) vnprime = 9.0*white.Y/(white.X + 15.0*white.Y + 3.0*white.Z); vprime = Luv.v/Luv.L/13.0 + vnprime; - ret.Y = white.Y*Lab_finv(fy); + ret.Y = white.Y*dmnsn_Lab_finv(fy); ret.X = ret.Y*9.0*uprime/vprime/4.0; ret.Z = ret.Y*(12.0 - 3*uprime - 20*vprime)/vprime/4.0; return ret; } -static double sRGB_Cinv(double CsRGB) { +static double dmnsn_sRGB_Cinv(double CsRGB) { /* * If C represents R, G, and B, then the Clinear values are now found as * follows: @@ -115,9 +112,9 @@ dmnsn_color_from_sRGB(dmnsn_sRGB sRGB) double Rlinear, Glinear, Blinear; /* Linear RGB values - no gamma */ dmnsn_color ret; - Rlinear = sRGB_Cinv(sRGB.R); - Glinear = sRGB_Cinv(sRGB.G); - Blinear = sRGB_Cinv(sRGB.B); + Rlinear = dmnsn_sRGB_Cinv(sRGB.R); + Glinear = dmnsn_sRGB_Cinv(sRGB.G); + Blinear = dmnsn_sRGB_Cinv(sRGB.B); /* * Now, the linear conversion. Expressed as matrix multiplication, it looks @@ -142,20 +139,20 @@ dmnsn_color_from_sRGB(dmnsn_sRGB sRGB) dmnsn_CIE_XYZ dmnsn_XYZ_from_color(dmnsn_color color) { - dmnsn_CIE_XYZ ret = { color.X, color.Y, color.Z }; + dmnsn_CIE_XYZ ret = { .X = color.X, .Y = color.Y, .Z = color.Z }; return ret; } dmnsn_CIE_xyY dmnsn_xyY_from_color(dmnsn_color color) { - dmnsn_CIE_xyY ret = { color.X/(color.X + color.Y + color.Z), - color.Y/(color.X + color.Y + color.Z), - color.Y }; + dmnsn_CIE_xyY ret = { .x = color.X/(color.X + color.Y + color.Z), + .y = color.Y/(color.X + color.Y + color.Z), + .Y = color.Y }; return ret; } -static double Lab_f(double t) { +static double dmnsn_Lab_f(double t) { if (t > 216.0/24389.0) { return pow(t, 1.0/3.0); } else { @@ -168,9 +165,9 @@ dmnsn_Lab_from_color(dmnsn_color color, dmnsn_CIE_XYZ white) { dmnsn_CIE_Lab ret; - ret.L = 116.0*Lab_f(color.Y/white.Y) - 16.0; - ret.a = 500.0*(Lab_f(color.X/white.X) - Lab_f(color.Y/white.Y)); - ret.b = 200.0*(Lab_f(color.Y/white.Y) - Lab_f(color.Z/white.Z)); + ret.L = 116.0*dmnsn_Lab_f(color.Y/white.Y) - 16.0; + ret.a = 500.0*(dmnsn_Lab_f(color.X/white.X) - dmnsn_Lab_f(color.Y/white.Y)); + ret.b = 200.0*(dmnsn_Lab_f(color.Y/white.Y) - dmnsn_Lab_f(color.Z/white.Z)); return ret; } @@ -186,14 +183,14 @@ dmnsn_Luv_from_color(dmnsn_color color, dmnsn_CIE_XYZ white) vprime = 9.0*color.Y/(color.X + 15.0*color.Y + 3.0*color.Z); vnprime = 9.0*white.Y/(white.X + 15.0*white.Y + 3.0*white.Z); - ret.L = 116.0*Lab_f(color.Y/white.Y) - 16.0; + ret.L = 116.0*dmnsn_Lab_f(color.Y/white.Y) - 16.0; ret.u = 13.0*ret.L*(uprime - unprime); ret.v = 13.0*ret.L*(vprime - vnprime); return ret; } -static double sRGB_C(double Clinear) { +static double dmnsn_sRGB_C(double Clinear) { /* * If C represents R, G, and B, then the sRGB values are now found as follows: * @@ -226,9 +223,9 @@ dmnsn_sRGB_from_color(dmnsn_color color) Glinear = -0.9692*color.X + 1.8760*color.Y + 0.0416*color.Z; Blinear = 0.0556*color.X - 0.2040*color.Y + 1.0570*color.Z; - ret.R = sRGB_C(Rlinear); - ret.G = sRGB_C(Glinear); - ret.B = sRGB_C(Blinear); + ret.R = dmnsn_sRGB_C(Rlinear); + ret.G = dmnsn_sRGB_C(Glinear); + ret.B = dmnsn_sRGB_C(Blinear); return ret; } @@ -243,8 +240,8 @@ dmnsn_color_add(dmnsn_color color1, dmnsn_color color2) Lab2 = dmnsn_Lab_from_color(color2, whitepoint); Lab.L = Lab1.L + Lab2.L; - Lab.a = (Lab1.L*Lab1.a + Lab2.L*Lab2.a)/Lab.L; - Lab.b = (Lab1.L*Lab1.b + Lab2.L*Lab2.b)/Lab.L; + Lab.a = Lab1.a + Lab2.a; + Lab.b = Lab1.b + Lab2.b; ret = dmnsn_color_from_Lab(Lab, whitepoint); ret.filter = (Lab1.L*color1.filter + Lab2.L*color2.filter)/Lab.L; |