summaryrefslogtreecommitdiffstats
path: root/libdimension/dimension/color
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension/dimension/color')
-rw-r--r--libdimension/dimension/color/color.h198
-rw-r--r--libdimension/dimension/color/tcolor.h116
2 files changed, 314 insertions, 0 deletions
diff --git a/libdimension/dimension/color/color.h b/libdimension/dimension/color/color.h
new file mode 100644
index 0000000..84e66ea
--- /dev/null
+++ b/libdimension/dimension/color/color.h
@@ -0,0 +1,198 @@
+/*************************************************************************
+ * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * This file is part of The Dimension Library. *
+ * *
+ * The Dimension Library is free software; you can redistribute it and/ *
+ * or modify it under the terms of the GNU Lesser General Public License *
+ * as published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Library is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this program. If not, see *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+/**
+ * @file
+ * Colors.
+ */
+
+#ifndef DMNSN_COLOR_H
+#error "Please include <dimension/color.h> instead of this header directly."
+#endif
+
+#include <math.h>
+#include <stdbool.h>
+
+/** A color value. */
+typedef struct {
+ double R; /**< Red component. */
+ double G; /**< Green component. */
+ double B; /**< Blue component. */
+} dmnsn_color;
+
+/** A standard format string for colors. */
+#define DMNSN_COLOR_FORMAT "Color<%g, %g, %g>"
+/** The appropriate arguements to printf() a color. */
+#define DMNSN_COLOR_PRINTF(c) (c).R, (c).G, (c).B
+
+/** Construct a new color. */
+DMNSN_INLINE dmnsn_color
+dmnsn_new_color(double R, double G, double B)
+{
+ dmnsn_color ret = { R, G, B };
+ return ret;
+}
+
+/** Apply sRGB gamma */
+DMNSN_INLINE double
+dmnsn_sRGB_gamma(double Clinear)
+{
+ /*
+ * If C represents R, G, and B, then the sRGB values are now found as follows:
+ *
+ * { 12.92*Clinear, Clinear <= 0.0031308
+ * Csrgb = { 1/2.4
+ * { (1.055)*Clinear - 0.055, Clinear > 0.0031308
+ */
+ if (Clinear == 1.0) {
+ return 1.0; /* Map 1.0 to 1.0 instead of 0.9999999999999999 */
+ } else if (Clinear > 0.0031308) {
+ return 1.055*pow(Clinear, 1.0/2.4) - 0.055;
+ } else {
+ return 12.92*Clinear;
+ }
+}
+
+/** Convert to sRGB space. */
+DMNSN_INLINE dmnsn_color
+dmnsn_color_to_sRGB(dmnsn_color color)
+{
+ return dmnsn_new_color(
+ dmnsn_sRGB_gamma(color.R),
+ dmnsn_sRGB_gamma(color.G),
+ dmnsn_sRGB_gamma(color.B)
+ );
+}
+
+/** Remove sRGB gamma */
+DMNSN_INLINE double
+dmnsn_sRGB_inverse_gamma(double CsRGB)
+{
+ /*
+ * If C represents R, G, and B, then the Clinear values are now found as
+ * follows:
+ *
+ * { Csrgb/12.92, Csrgb <= 0.04045
+ * Clinear = { 2.4
+ * { ((Csrgb + 0.055)/1.055) , Csrgb > 0.04045
+ */
+ if (CsRGB == 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 {
+ return pow((CsRGB + 0.055)/1.055, 2.4);
+ }
+}
+
+/** Convert from sRGB space. */
+DMNSN_INLINE dmnsn_color
+dmnsn_color_from_sRGB(dmnsn_color color)
+{
+ return dmnsn_new_color(
+ dmnsn_sRGB_inverse_gamma(color.R),
+ dmnsn_sRGB_inverse_gamma(color.G),
+ dmnsn_sRGB_inverse_gamma(color.B)
+ );
+}
+
+/** Greyscale color intensity. */
+DMNSN_INLINE double
+dmnsn_color_intensity(dmnsn_color color)
+{
+ return 0.2126*color.R + 0.7152*color.G + 0.0722*color.B;
+}
+
+/** Add two colors together. */
+DMNSN_INLINE dmnsn_color
+dmnsn_color_add(dmnsn_color lhs, dmnsn_color rhs)
+{
+ return dmnsn_new_color(lhs.R + rhs.R, lhs.G + rhs.G, lhs.B + rhs.B);
+}
+
+/** Subtract two colors. */
+DMNSN_INLINE dmnsn_color
+dmnsn_color_sub(dmnsn_color lhs, dmnsn_color rhs)
+{
+ return dmnsn_new_color(lhs.R - rhs.R, lhs.G - rhs.G, lhs.B - rhs.B);
+}
+
+/** Scale a color's intensity. */
+DMNSN_INLINE dmnsn_color
+dmnsn_color_mul(double n, dmnsn_color color)
+{
+ return dmnsn_new_color(n*color.R, n*color.G, n*color.B);
+}
+
+/** Return the color at \p n on a gradient from \p c1 at 0 to \p c2 at 1. */
+DMNSN_INLINE dmnsn_color
+dmnsn_color_gradient(dmnsn_color c1, dmnsn_color c2, double n)
+{
+ return dmnsn_new_color(
+ n*(c2.R - c1.R) + c1.R,
+ n*(c2.G - c1.G) + c1.G,
+ n*(c2.B - c1.B) + c1.B
+ );
+}
+
+/** Illuminate \p color with \p light. */
+DMNSN_INLINE dmnsn_color
+dmnsn_color_illuminate(dmnsn_color light, dmnsn_color color)
+{
+ return dmnsn_new_color(light.R*color.R, light.G*color.G, light.B*color.B);
+}
+
+/** Saturate the color components to [0.0, 1.0]. */
+DMNSN_INLINE dmnsn_color
+dmnsn_color_clamp(dmnsn_color color)
+{
+ color.R = dmnsn_clamp(color.R, 0.0, 1.0);
+ color.G = dmnsn_clamp(color.G, 0.0, 1.0);
+ color.B = dmnsn_clamp(color.B, 0.0, 1.0);
+ return color;
+}
+
+/** Return whether a color contains any NaN components. */
+DMNSN_INLINE bool
+dmnsn_color_isnan(dmnsn_color color)
+{
+ return dmnsn_isnan(color.R) || dmnsn_isnan(color.G) || dmnsn_isnan(color.B);
+}
+
+/* Standard colors */
+
+/** Black. */
+#define dmnsn_black dmnsn_new_color(0.0, 0.0, 0.0)
+/** White. */
+#define dmnsn_white dmnsn_new_color(1.0, 1.0, 1.0)
+/** Red. */
+#define dmnsn_red dmnsn_new_color(1.0, 0.0, 0.0)
+/** Green. */
+#define dmnsn_green dmnsn_new_color(0.0, 1.0, 0.0)
+/** Blue. */
+#define dmnsn_blue dmnsn_new_color(0.0, 0.0, 1.0)
+/** Magenta. */
+#define dmnsn_magenta dmnsn_new_color(1.0, 0.0, 1.0)
+/** Orange. */
+#define dmnsn_orange dmnsn_new_color(1.0, 0.21404114048223255, 0.0)
+/** Yellow. */
+#define dmnsn_yellow dmnsn_new_color(1.0, 1.0, 0.0)
+/** Cyan. */
+#define dmnsn_cyan dmnsn_new_color(0.0, 1.0, 1.0)
diff --git a/libdimension/dimension/color/tcolor.h b/libdimension/dimension/color/tcolor.h
new file mode 100644
index 0000000..b4b4167
--- /dev/null
+++ b/libdimension/dimension/color/tcolor.h
@@ -0,0 +1,116 @@
+/*************************************************************************
+ * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * This file is part of The Dimension Library. *
+ * *
+ * The Dimension Library is free software; you can redistribute it and/ *
+ * or modify it under the terms of the GNU Lesser General Public License *
+ * as published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Library is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this program. If not, see *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+/**
+ * @file
+ * Colors with transparency information.
+ */
+
+#ifndef DMNSN_COLOR_H
+#error "Please include <dimension/color.h> instead of this header directly."
+#endif
+
+/** A transparent color. */
+typedef struct dmnsn_tcolor {
+ dmnsn_color c; /**< Color. */
+ double T; /**< Transparency. */
+ double F; /**< Proportion of filtered transparency. */
+} dmnsn_tcolor;
+
+/** A standard format string for colors. */
+#define DMNSN_TCOLOR_FORMAT "TColor<%g, %g, %g, %g, %g>"
+/** The appropriate arguements to printf() a color. */
+#define DMNSN_TCOLOR_PRINTF(tc) (tc).c.R, (tc).c.G, (tc).c.B, (tc).T, (tc).F
+
+/** Create a tcolor. */
+DMNSN_INLINE dmnsn_tcolor
+dmnsn_new_tcolor(dmnsn_color c, double T, double F)
+{
+ dmnsn_tcolor ret = { c, T, F };
+ return ret;
+}
+
+/** Convert a dmnsn_color into a dmnsn_tcolor. */
+#define DMNSN_TCOLOR(c) dmnsn_new_tcolor(c, 0.0, 0.0)
+
+/** Create a tcolor with individually-specified components. */
+DMNSN_INLINE dmnsn_tcolor
+dmnsn_new_tcolor5(double R, double G, double B, double T, double F)
+{
+ dmnsn_tcolor ret = { dmnsn_new_color(R, G, B), T, F };
+ return ret;
+}
+
+/** Return the color at \p n on a gradient from \p c1 at 0 to \p c2 at 1. */
+DMNSN_INLINE dmnsn_tcolor
+dmnsn_tcolor_gradient(dmnsn_tcolor c1, dmnsn_tcolor c2, double n)
+{
+ return dmnsn_new_tcolor(
+ dmnsn_color_gradient(c1.c, c2.c, n),
+ n*(c2.T - c1.T) + c1.T,
+ n*(c2.F - c1.F) + c1.F
+ );
+}
+
+/** Filter \p light through \p filter. */
+DMNSN_INLINE dmnsn_color
+dmnsn_tcolor_filter(dmnsn_color light, dmnsn_tcolor filter)
+{
+ dmnsn_color filtered =
+ dmnsn_color_mul(filter.T*filter.F, dmnsn_color_illuminate(light, filter.c));
+ dmnsn_color transmitted = dmnsn_color_mul(filter.T*(1.0 - filter.F), light);
+ return dmnsn_color_add(filtered, transmitted);
+}
+
+/** Remove the filtered component of a tcolor. */
+DMNSN_INLINE dmnsn_tcolor
+dmnsn_tcolor_remove_filter(dmnsn_tcolor tcolor)
+{
+ double intensity = dmnsn_color_intensity(tcolor.c);
+ double newtrans = (1.0 - (1.0 - intensity)*tcolor.F)*tcolor.T;
+ if (1.0 - newtrans >= dmnsn_epsilon) {
+ tcolor.c = dmnsn_color_mul((1.0 - tcolor.T)/(1.0 - newtrans), tcolor.c);
+ }
+ tcolor.T = newtrans;
+ tcolor.F = 0.0;
+ return tcolor;
+}
+
+/** Saturate the tcolor components to [0.0, 1.0]. */
+DMNSN_INLINE dmnsn_tcolor
+dmnsn_tcolor_clamp(dmnsn_tcolor tcolor)
+{
+ tcolor.c = dmnsn_color_clamp(tcolor.c);
+ tcolor.T = dmnsn_clamp(tcolor.T, 0.0, 1.0);
+ tcolor.F = dmnsn_clamp(tcolor.F, 0.0, 1.0);
+ return tcolor;
+}
+
+/** Return whether a tcolor contains any NaN components. */
+DMNSN_INLINE bool
+dmnsn_tcolor_isnan(dmnsn_tcolor tcolor)
+{
+ return dmnsn_color_isnan(tcolor.c) || dmnsn_isnan(tcolor.T) || dmnsn_isnan(tcolor.F);
+}
+
+/* Standard tcolors */
+
+/** Clear. */
+#define dmnsn_clear dmnsn_new_tcolor5(0.0, 0.0, 0.0, 1.0, 0.0)