home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / rayce27s / color.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-02  |  2.5 KB  |  154 lines

  1. /*
  2.  * color.c -- colors.
  3.  * 
  4.  * (c) 1993, 1994 Han-Wen Nienhuys <hanwen@stack.urc.tue.nl>
  5.  * 
  6.  * This program is free software; you can redistribute it and/or modify it
  7.  * under the terms of the GNU General Public License as published by the
  8.  * Free Software Foundation;
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include "ray.h"
  21. #include "proto.h"
  22. #include "extern.h"
  23.  
  24. /*
  25.  * simply add colors
  26.  */
  27. PUBLIC color
  28. color_add(color a, color b)
  29. {
  30.     color           c;
  31.  
  32.     c.r = a.r + b.r;
  33.     c.g = a.g + b.g;
  34.     c.b = a.b + b.b;
  35.  
  36.     return c;
  37. }
  38.  
  39. /*
  40.  * multiply colors, by components
  41.  */
  42. PUBLIC color
  43. color_mul(color a, color b)
  44. {
  45.     color           c;
  46.  
  47.     c.r = a.r * b.r;
  48.     c.g = a.g * b.g;
  49.     c.b = a.b * b.b;
  50.  
  51.     return c;
  52. }
  53.  
  54. /*
  55.  * scalar color product
  56.  */
  57. PUBLIC color
  58. scolorproduct(double fact, color c)
  59. {
  60.     color           result;
  61.  
  62.     result.r = c.r * fact;
  63.     result.g = c.g * fact;
  64.     result.b = c.b * fact;
  65.  
  66.     return result;
  67. }
  68.  
  69. /* print out a color please */
  70. PUBLIC void
  71. print_c(char *s, color c)
  72. {
  73. #ifdef DEBUG
  74.     printf("%s: r %lf g %lf b %lf\n", s, c.r, c.g, c.b);
  75. #endif
  76. }
  77.  
  78. /* scale every component of color */
  79. PUBLIC void
  80. scale_color(color *c, double fact)
  81. {
  82.     c->r *= fact;
  83.     c->g *= fact;
  84.     c->b *= fact;
  85. }
  86.  
  87. /* copy it */
  88. PUBLIC void
  89. copy_color(color *dst, color *src)
  90. {
  91.     assert(src != NULL || dst != NULL);
  92.  
  93.     *dst = *src;
  94.     return;
  95. }
  96.  
  97. /* alloc and init */
  98. PUBLIC color   *
  99. get_new_color(void)
  100. {
  101.     color          *p;
  102.  
  103.     p = ALLOC(color);
  104.  
  105.     CHECK_MEM(p, "color");
  106.     setcolor(*p, 0.0, 0.0, 0.0);
  107.  
  108.     return p;
  109. }
  110.  
  111. /* ZAPPP! */
  112. PUBLIC void
  113. free_color(color *c)
  114. {
  115.     free((void *) c);
  116. }
  117.  
  118. /*
  119.  * a norm on the color triples. The formula used is .299 r + .587 g + .114
  120.  * b (from the PBM utils)
  121.  */
  122. PUBLIC double
  123. color_length(color c)
  124. {
  125.     return 0.299 * ABS(c.r) + 0.587 * ABS(c.g) + 0.114 * ABS(c.b);
  126. }
  127.  
  128. /*
  129.  * normalize a color: make it fit in [0,1]
  130.  */
  131. PUBLIC color
  132. normcol(color col)
  133. {
  134.     double          m;
  135.  
  136.     m = MAX(col.r, col.g);
  137.     m = MAX(m, col.b);
  138.     if (m > 1) {
  139.     col.r /= m;
  140.     col.g /= m;
  141.     col.b /= m;
  142.     }
  143.     if (col.r < 0)
  144.     col.r = 0;
  145.  
  146.     if (col.g < 0)
  147.     col.g = 0;
  148.  
  149.     if (col.b < 0)
  150.     col.b = 0;
  151.  
  152.     return col;
  153. }
  154.