summaryrefslogtreecommitdiffstats
path: root/libdimension/color.c
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension/color.c')
-rw-r--r--libdimension/color.c80
1 files changed, 47 insertions, 33 deletions
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,
};