]> Dogcows Code - chaz/rasterize/blobdiff - color.h
make animate script work for modern luas
[chaz/rasterize] / color.h
diff --git a/color.h b/color.h
index 26a6390d2adf70c598652bc33183e5b15a421c23..32765e0901ac2e9902041d431fc55eded1de4aee 100644 (file)
--- a/color.h
+++ b/color.h
@@ -80,16 +80,16 @@ void color_print(color_t c)
 
 
 /*
- * Add two colors together.  Color may need to be clamped afterward.
+ * 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);
+    return color_new(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a);
 }
 
 /*
- * Add three colors together.  Color may need to be clamped afterward.
+ * Add three colors together.
  */
 INLINE_MAYBE
 color_t color_add2(color_t c1, color_t c2, color_t c3)
@@ -97,6 +97,48 @@ 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;
+    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;
+    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);
+}
+
+INLINE_MAYBE
+color_t color_blend(color_t d, color_t s)
+{
+    d.r = (S(1.0) - s.a) * d.r + s.a * s.r;
+    d.g = (S(1.0) - s.a) * d.g + s.a * s.g;
+    d.b = (S(1.0) - s.a) * d.b + s.a * s.b;
+    return d;
+}
+
 /*
  * Clamp a color's channels to the normal range of 0.0 to 1.0.
  */
@@ -119,8 +161,7 @@ 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);
+                     c1.b * (S(1.0) - a) + c2.b * a, c1.a);
 }
 
 /*
@@ -131,8 +172,7 @@ 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));
+                     c1.b * b[0] + c2.b * b[1] + c3.b * b[2], c1.a);
 }
 
 
This page took 0.018244 seconds and 4 git commands to generate.