From 3cff39f02c54eb0f06835e1e0b6a2f6f02d0b5cb Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 28 Nov 2011 21:15:44 -0500 Subject: Be more consistent about using sRGB in the client. Also, expose the sRGB C and C^-1 functions. --- libdimension/color.c | 80 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 33 deletions(-) (limited to 'libdimension/color.c') diff --git a/libdimension/color.c b/libdimension/color.c index efa4f4a..39f9b3d 100644 --- a/libdimension/color.c +++ b/libdimension/color.c @@ -97,69 +97,83 @@ const dmnsn_color dmnsn_cyan = { .trans = 0.0, }; -/** Inverse function of sRGB's `C' function, for the reverse conversion. */ -static double -dmnsn_sRGB_C_inv(double CsRGB) +/* sRGB's `C' function. */ +static inline double +dmnsn_sRGB_C(double Clinear) { /* - * If C represents R, G, and B, then the Clinear values are now found as - * follows: + * If C represents R, G, and B, then the sRGB values are now found as follows: * - * { Csrgb/12.92, Csrgb <= 0.04045 - * Clinear = { 1/2.4 - * { ((Csrgb + 0.055)/1.055) , Csrgb > 0.04045 + * { 12.92*Clinear, Clinear <= 0.0031308 + * Csrgb = { 1/2.4 + * { (1.055)*Clinear - 0.055, Clinear > 0.0031308 */ - if (CsRGB == 1.0) { + if (Clinear == 1.0) { return 1.0; /* Map 1.0 to 1.0 instead of 0.9999999999999999 */ - } else if (CsRGB <= 0.040449936) { - return CsRGB/12.92; + } else if (Clinear > 0.0031308) { + return 1.055*pow(Clinear, 1.0/2.4) - 0.055; } else { - return pow((CsRGB + 0.055)/1.055, 2.4); + return 12.92*Clinear; } } -/* Convert from sRGB space */ +/* Export dmnsn_sRGB_C */ +double +dmnsn_sRGB_gamma(double Clinear) +{ + return dmnsn_sRGB_C(Clinear); +} + +/* Convert to sRGB space */ dmnsn_color -dmnsn_color_from_sRGB(dmnsn_color color) +dmnsn_color_to_sRGB(dmnsn_color color) { dmnsn_color ret = { - .R = dmnsn_sRGB_C_inv(color.R), - .G = dmnsn_sRGB_C_inv(color.G), - .B = dmnsn_sRGB_C_inv(color.B), + .R = dmnsn_sRGB_C(color.R), + .G = dmnsn_sRGB_C(color.G), + .B = dmnsn_sRGB_C(color.B), .trans = color.trans, .filter = color.filter, }; return ret; } -/** sRGB's `C' function. */ -static double -dmnsn_sRGB_C(double Clinear) +/* Inverse function of sRGB's `C' function, for the reverse conversion. */ +double +dmnsn_sRGB_C_inv(double CsRGB) { /* - * If C represents R, G, and B, then the sRGB values are now found as follows: + * If C represents R, G, and B, then the Clinear values are now found as + * follows: * - * { 12.92*Clinear, Clinear <= 0.0031308 - * Csrgb = { 1/2.4 - * { (1.055)*Clinear - 0.055, Clinear > 0.0031308 + * { Csrgb/12.92, Csrgb <= 0.04045 + * Clinear = { 1/2.4 + * { ((Csrgb + 0.055)/1.055) , Csrgb > 0.04045 */ - if (Clinear == 1.0) { + if (CsRGB == 1.0) { return 1.0; /* Map 1.0 to 1.0 instead of 0.9999999999999999 */ - } else if (Clinear <= 0.0031308) { - return 12.92*Clinear; + } else if (CsRGB <= 0.040449936) { + return CsRGB/12.92; } else { - return 1.055*pow(Clinear, 1.0/2.4) - 0.055; + return pow((CsRGB + 0.055)/1.055, 2.4); } } -/* Convert to sRGB space */ +/* Export dmnsn_sRGB_C_inv */ +double +dmnsn_sRGB_inverse_gamma(double CsRGB) +{ + return dmnsn_sRGB_C_inv(CsRGB); +} + +/* Convert from sRGB space */ dmnsn_color -dmnsn_color_to_sRGB(dmnsn_color color) +dmnsn_color_from_sRGB(dmnsn_color color) { dmnsn_color ret = { - .R = dmnsn_sRGB_C(color.R), - .G = dmnsn_sRGB_C(color.G), - .B = dmnsn_sRGB_C(color.B), + .R = dmnsn_sRGB_C_inv(color.R), + .G = dmnsn_sRGB_C_inv(color.G), + .B = dmnsn_sRGB_C_inv(color.B), .trans = color.trans, .filter = color.filter, }; -- cgit v1.2.3