]> Dogcows Code - chaz/rasterize/blobdiff - color.h
add external supersampling to animate script
[chaz/rasterize] / color.h
diff --git a/color.h b/color.h
index ba333e2c0f6d279fb42d48836e5113f8b7103c90..2c5bf8040cc2ee0ff7cbd9ecf7665902a930fdb0 100644 (file)
--- a/color.h
+++ b/color.h
@@ -5,8 +5,8 @@
  * mcgarvey@eng.utah.edu
  */
 
-#ifndef __COLOR_H__
-#define __COLOR_H__
+#ifndef _COLOR_H_
+#define _COLOR_H_
 
 #include "common.h"
 
@@ -32,7 +32,7 @@ typedef struct color color_t;
 /*
  * Initialize a color.
  */
-__fast__
+INLINE_MAYBE
 void color_init(color_t* c, colorchan_t r, colorchan_t g, colorchan_t b, colorchan_t a)
 {
     c->r = r;
@@ -45,7 +45,7 @@ void color_init(color_t* c, colorchan_t r, colorchan_t g, colorchan_t b, colorch
 /*
  * Create a new color, copied by value.
  */
-__fast__
+INLINE_MAYBE
 color_t color_new(colorchan_t r, colorchan_t g, colorchan_t b, colorchan_t a)
 {
     color_t c;
@@ -64,16 +64,123 @@ color_t color_new(colorchan_t r, colorchan_t g, colorchan_t b, colorchan_t a)
 #define COLOR_WHITE   color_new(S(1.0), S(1.0), S(1.0), S(1.0))
 
 
+/*
+ * Print the color to stdout.
+ */
+INLINE_MAYBE
+void color_print(color_t c)
+{
+#if (SCALAR_SIZE == 8)
+    const char* fmt = "[ %9.5lf %9.5lf %9.5lf %9.5lf ]";
+#else
+    const char* fmt = "[ %9.5f %9.5f %9.5f %9.5f ]";
+#endif
+    printf(fmt, c.r, c.g, c.b, c.a);
+}
+
+
+/*
+ * Add two colors together.
+ */
+INLINE_MAYBE
+color_t color_add(color_t c1, color_t c2)
+{
+    return color_new(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a + c2.a);
+}
+
+/*
+ * Add three colors together.
+ */
+INLINE_MAYBE
+color_t color_add2(color_t c1, color_t c2, color_t c3)
+{
+    return color_add(color_add(c1, c2), c3);
+}
+
+/*
+ * Multiply two colors together.
+ */
+INLINE_MAYBE
+color_t color_mult(color_t c1, color_t c2)
+{
+    c1.r *= c2.r;
+    c1.g *= c2.g;
+    c1.b *= c2.b;
+    c1.a *= c2.a;
+    return c1;
+}
+
+/*
+ * Scale a color by some scalar coefficient.
+ */
+INLINE_MAYBE
+color_t color_scale(color_t c, scal_t k)
+{
+    c.r *= k;
+    c.g *= k;
+    c.b *= k;
+    c.a *= k;
+    return c;
+}
+
+/*
+ * Scale a color by another color and some scalar coefficient.
+ */
+INLINE_MAYBE
+color_t color_scale2(color_t c1, color_t c2, scal_t k)
+{
+    return color_scale(color_mult(c1, c2), k);
+}
+
+/*
+ * Clamp a color's channels to the normal range of 0.0 to 1.0.
+ */
+INLINE_MAYBE
+color_t color_clamp(color_t c)
+{
+    c.r = scal_clamp(c.r, S(0.0), S(1.0));
+    c.g = scal_clamp(c.g, S(0.0), S(1.0));
+    c.b = scal_clamp(c.b, S(0.0), S(1.0));
+    c.a = scal_clamp(c.a, S(0.0), S(1.0));
+    return c;
+}
+
+
+/*
+ * Interpolate smoothly between two colors with an alpha value.
+ */
+INLINE_MAYBE
+color_t color_interp(color_t c1, color_t c2, scal_t a)
+{
+    return color_new(c1.r * (S(1.0) - a) + c2.r * a,
+                     c1.g * (S(1.0) - a) + c2.g * a,
+                     c1.b * (S(1.0) - a) + c2.b * a,
+                     c1.a * (S(1.0) - a) + c2.a * a);
+}
+
+/*
+ * Interpolate smoothly between three colors with barycentric coordinates.
+ */
+INLINE_MAYBE
+color_t color_interp2(color_t c1, color_t c2, color_t c3, scal_t b[3])
+{
+    return color_new(c1.r * b[0] + c2.r * b[1] + c3.r * b[2],
+                     c1.g * b[0] + c2.g * b[1] + c3.g * b[2],
+                     c1.b * b[0] + c2.b * b[1] + c3.b * b[2],
+                     S(1.0));
+}
+
+
 /*
  * Define integer types for a 32-bit RGBA color representation.
  */
-typedef uint32_t    rgba_t;
 typedef uint8_t     rgbachan_t;
+typedef uint32_t    rgba_t;
 
 /*
  * Create a new color from a 32-bit RGBA value.
  */
-__fast__
+INLINE_MAYBE
 color_t color_from_rgba(rgba_t n)
 {
     colorchan_t r = (colorchan_t)UNPACK(n, 3) / S(255.0);
@@ -86,7 +193,7 @@ color_t color_from_rgba(rgba_t n)
 /*
  * Split a color into 8-bit RGBA channels.
  */
-__fast__
+INLINE_MAYBE
 void color_split(color_t c, rgbachan_t* r, rgbachan_t* g, rgbachan_t* b, rgbachan_t* a)
 {
     if (r) *r = (rgbachan_t)(c.r * S(255.0));
@@ -98,7 +205,7 @@ void color_split(color_t c, rgbachan_t* r, rgbachan_t* g, rgbachan_t* b, rgbacha
 /*
  * Convert a color to a 32-bit RGBA value.
  */
-__fast__
+INLINE_MAYBE
 rgba_t rgba_from_color(color_t c)
 {
     rgbachan_t r, g, b, a;
@@ -107,5 +214,5 @@ rgba_t rgba_from_color(color_t c)
 }
 
 
-#endif // __COLOR_H__
+#endif // _COLOR_H_
 
This page took 0.024102 seconds and 4 git commands to generate.