summaryrefslogtreecommitdiffstats
path: root/libdimension/color.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-11-09 14:07:14 -0500
committerTavian Barnes <tavianator@gmail.com>2009-11-09 14:07:14 -0500
commit5eab850f84d30bfe2042b439c65b7e83db581348 (patch)
tree93efdae85a039c3b83aece5405f9fa631a0a5ecb /libdimension/color.c
parenta26ca6375382107037807abcd7ad4cdfd8cde2d6 (diff)
downloaddimension-5eab850f84d30bfe2042b439c65b7e83db581348.tar.xz
Rudimentary light/shadow handling.
Diffstat (limited to 'libdimension/color.c')
-rw-r--r--libdimension/color.c56
1 files changed, 47 insertions, 9 deletions
diff --git a/libdimension/color.c b/libdimension/color.c
index 7fb73ed..374a0a0 100644
--- a/libdimension/color.c
+++ b/libdimension/color.c
@@ -263,17 +263,14 @@ dmnsn_sRGB_from_color(dmnsn_color color)
dmnsn_color
dmnsn_color_add(dmnsn_color color1, dmnsn_color color2)
{
- dmnsn_CIE_Lab Lab, Lab1, Lab2;
- dmnsn_color ret;
-
- Lab1 = dmnsn_Lab_from_color(color1, dmnsn_whitepoint);
- Lab2 = dmnsn_Lab_from_color(color2, dmnsn_whitepoint);
+ dmnsn_CIE_Lab Lab1 = dmnsn_Lab_from_color(color1, dmnsn_whitepoint);
+ dmnsn_CIE_Lab Lab2 = dmnsn_Lab_from_color(color2, dmnsn_whitepoint);
- Lab.L = Lab1.L + Lab2.L;
- Lab.a = Lab1.a + Lab2.a;
- Lab.b = Lab1.b + Lab2.b;
+ dmnsn_CIE_Lab Lab = { .L = Lab1.L + Lab2.L,
+ .a = Lab1.a + Lab2.a,
+ .b = Lab1.b + Lab2.b };
- ret = dmnsn_color_from_Lab(Lab, dmnsn_whitepoint);
+ dmnsn_color ret = dmnsn_color_from_Lab(Lab, dmnsn_whitepoint);
/* Weighted average of transparencies by intensity */
ret.filter = (Lab1.L*color1.filter + Lab2.L*color2.filter)/Lab.L;
ret.trans = (Lab1.L*color1.trans + Lab2.L*color2.trans)/Lab.L;
@@ -281,6 +278,47 @@ dmnsn_color_add(dmnsn_color color1, dmnsn_color color2)
return ret;
}
+/* Multiply a color by a scalar */
+dmnsn_color
+dmnsn_color_mul(double n, dmnsn_color color)
+{
+ dmnsn_CIE_Lab Lab = dmnsn_Lab_from_color(color, dmnsn_whitepoint);
+ double LabL = Lab.L;
+ Lab.L *= n;
+ Lab.a *= n;
+ Lab.b *= n;
+
+ dmnsn_color ret = dmnsn_color_from_Lab(Lab, dmnsn_whitepoint);
+ ret.filter = color.filter;
+ ret.trans = color.trans;
+
+ return ret;
+}
+
+/* Illuminates `color' with `light' */
+dmnsn_color
+dmnsn_color_illuminate(dmnsn_color light, dmnsn_color color)
+{
+ dmnsn_CIE_Lab Lab1 = dmnsn_Lab_from_color(light, dmnsn_whitepoint);
+ dmnsn_CIE_Lab Lab2 = dmnsn_Lab_from_color(color, dmnsn_whitepoint);
+
+ double dot = Lab1.L*Lab2.L + Lab1.a*Lab2.a + Lab1.b*Lab2.b;
+ double norm = sqrt(Lab2.L*Lab2.L + Lab2.a*Lab2.a + Lab2.b*Lab2.b);
+
+ if (norm == 0.0)
+ return dmnsn_black;
+
+ dmnsn_CIE_Lab Lab = { .L = dot*Lab2.L/norm/100.0,
+ .a = dot*Lab2.a/norm,
+ .b = dot*Lab2.b/norm };
+
+ dmnsn_color ret = dmnsn_color_from_Lab(Lab, dmnsn_whitepoint);
+ ret.filter = color.filter;
+ ret.trans = color.trans;
+
+ return ret;
+}
+
/* Find the perceptual difference between two colors, using CIE L*a*b*. */
double
dmnsn_color_difference(dmnsn_color color1, dmnsn_color color2)