diff options
-rw-r--r-- | libdimension/color.c | 44 | ||||
-rw-r--r-- | libdimension/dimension/color.h | 2 | ||||
-rw-r--r-- | libdimension/reflective.c | 8 |
3 files changed, 21 insertions, 33 deletions
diff --git a/libdimension/color.c b/libdimension/color.c index 5220dfa..9e1ef4f 100644 --- a/libdimension/color.c +++ b/libdimension/color.c @@ -323,31 +323,6 @@ dmnsn_color_add(dmnsn_color c1, dmnsn_color c2) return ret; } -/* Subtract two colors */ -dmnsn_color -dmnsn_color_sub(dmnsn_color c1, dmnsn_color c2) -{ - dmnsn_sRGB sRGB1 = dmnsn_sRGB_from_color(c1); - dmnsn_sRGB sRGB2 = dmnsn_sRGB_from_color(c2); - - dmnsn_sRGB sRGB = { - .R = sRGB1.R - sRGB2.R, - .G = sRGB1.G - sRGB2.G, - .B = sRGB1.B - sRGB2.B - }; - - dmnsn_color ret = dmnsn_color_from_sRGB(sRGB); - - /* Weighted average of transparencies by intensity */ - dmnsn_CIE_Lab Lab1 = dmnsn_Lab_from_color(ret, dmnsn_whitepoint); - dmnsn_CIE_Lab Lab2 = dmnsn_Lab_from_color(ret, dmnsn_whitepoint); - if (Lab1.L + Lab2.L) { - ret.filter = (Lab1.L*c1.filter - Lab2.L*c2.filter)/(Lab1.L + Lab2.L); - ret.trans = (Lab1.L*c1.trans - Lab2.L*c2.trans )/(Lab1.L + Lab2.L); - } - return ret; -} - /* Multiply a color by a scalar */ dmnsn_color dmnsn_color_mul(double n, dmnsn_color color) @@ -363,6 +338,25 @@ dmnsn_color_mul(double n, dmnsn_color color) return ret; } +/* For n in [0, 1] get the color in a gradient between c1 and c2 */ +dmnsn_color +dmnsn_color_gradient(dmnsn_color c1, dmnsn_color c2, double n) +{ + dmnsn_sRGB sRGB1 = dmnsn_sRGB_from_color(c1); + dmnsn_sRGB sRGB2 = dmnsn_sRGB_from_color(c2); + + dmnsn_sRGB sRGB = { + .R = n*(sRGB2.R - sRGB1.R) + sRGB1.R, + .G = n*(sRGB2.G - sRGB1.G) + sRGB1.G, + .B = n*(sRGB2.B - sRGB1.B) + sRGB1.B + }; + + dmnsn_color ret = dmnsn_color_from_sRGB(sRGB); + ret.filter = n*(c2.filter - c1.filter) + c1.filter; + ret.trans = n*(c2.trans - c1.trans) + c1.trans; + return ret; +} + /* Filters `color' through `filter' */ dmnsn_color dmnsn_color_filter(dmnsn_color color, dmnsn_color filter) diff --git a/libdimension/dimension/color.h b/libdimension/dimension/color.h index 43edbff..9627ce8 100644 --- a/libdimension/dimension/color.h +++ b/libdimension/dimension/color.h @@ -88,8 +88,8 @@ dmnsn_sRGB dmnsn_sRGB_from_color(dmnsn_color color); /* Perceptual color manipulation */ dmnsn_color dmnsn_color_add(dmnsn_color color1, dmnsn_color color2); -dmnsn_color dmnsn_color_sub(dmnsn_color color1, dmnsn_color color2); dmnsn_color dmnsn_color_mul(double n, dmnsn_color color); +dmnsn_color dmnsn_color_gradient(dmnsn_color c1, dmnsn_color c2, double n); dmnsn_color dmnsn_color_filter(dmnsn_color color, dmnsn_color filter); dmnsn_color dmnsn_color_illuminate(dmnsn_color light, dmnsn_color color); double dmnsn_color_difference(dmnsn_color color1, dmnsn_color color2); diff --git a/libdimension/reflective.c b/libdimension/reflective.c index f1c7355..f44e4ff 100644 --- a/libdimension/reflective.c +++ b/libdimension/reflective.c @@ -40,13 +40,7 @@ dmnsn_reflective_finish_fn(const dmnsn_finish *finish, double reflection = pow(dmnsn_vector_dot(ray, normal), params->falloff); return dmnsn_color_illuminate( - dmnsn_color_add( - dmnsn_color_mul( - reflection, - dmnsn_color_sub(params->max, params->min) - ), - params->min - ), + dmnsn_color_gradient(params->min, params->max, reflection), reflect ); } |