home *** CD-ROM | disk | FTP | other *** search
- /*
- * color.c -- colors.
- *
- * (c) 1993, 1994 Han-Wen Nienhuys <hanwen@stack.urc.tue.nl>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation;
- *
- * This program 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
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include "ray.h"
- #include "proto.h"
- #include "extern.h"
-
- /*
- * simply add colors
- */
- PUBLIC color
- color_add(color a, color b)
- {
- color c;
-
- c.r = a.r + b.r;
- c.g = a.g + b.g;
- c.b = a.b + b.b;
-
- return c;
- }
-
- /*
- * multiply colors, by components
- */
- PUBLIC color
- color_mul(color a, color b)
- {
- color c;
-
- c.r = a.r * b.r;
- c.g = a.g * b.g;
- c.b = a.b * b.b;
-
- return c;
- }
-
- /*
- * scalar color product
- */
- PUBLIC color
- scolorproduct(double fact, color c)
- {
- color result;
-
- result.r = c.r * fact;
- result.g = c.g * fact;
- result.b = c.b * fact;
-
- return result;
- }
-
- /* print out a color please */
- PUBLIC void
- print_c(char *s, color c)
- {
- #ifdef DEBUG
- printf("%s: r %lf g %lf b %lf\n", s, c.r, c.g, c.b);
- #endif
- }
-
- /* scale every component of color */
- PUBLIC void
- scale_color(color *c, double fact)
- {
- c->r *= fact;
- c->g *= fact;
- c->b *= fact;
- }
-
- /* copy it */
- PUBLIC void
- copy_color(color *dst, color *src)
- {
- assert(src != NULL || dst != NULL);
-
- *dst = *src;
- return;
- }
-
- /* alloc and init */
- PUBLIC color *
- get_new_color(void)
- {
- color *p;
-
- p = ALLOC(color);
-
- CHECK_MEM(p, "color");
- setcolor(*p, 0.0, 0.0, 0.0);
-
- return p;
- }
-
- /* ZAPPP! */
- PUBLIC void
- free_color(color *c)
- {
- free((void *) c);
- }
-
- /*
- * a norm on the color triples. The formula used is .299 r + .587 g + .114
- * b (from the PBM utils)
- */
- PUBLIC double
- color_length(color c)
- {
- return 0.299 * ABS(c.r) + 0.587 * ABS(c.g) + 0.114 * ABS(c.b);
- }
-
- /*
- * normalize a color: make it fit in [0,1]
- */
- PUBLIC color
- normcol(color col)
- {
- double m;
-
- m = MAX(col.r, col.g);
- m = MAX(m, col.b);
- if (m > 1) {
- col.r /= m;
- col.g /= m;
- col.b /= m;
- }
- if (col.r < 0)
- col.r = 0;
-
- if (col.g < 0)
- col.g = 0;
-
- if (col.b < 0)
- col.b = 0;
-
- return col;
- }
-