home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xlock / hsbramp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-09  |  2.0 KB  |  115 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)hsbramp.c    23.5 91/05/24 XLOCK";
  3. #endif
  4. /*-
  5.  * hsbramp.c - Create an HSB ramp.
  6.  *
  7.  * Copyright (c) 1991 by Patrick J. Naughton.
  8.  *
  9.  * See xlock.c for copying information.
  10.  *
  11.  * Revision History:
  12.  * 29-Jul-90: renamed hsbramp.c from HSBmap.c
  13.  *          minor optimizations.
  14.  * 01-Sep-88: Written.
  15.  */
  16.  
  17. #include <sys/types.h>
  18. #include <math.h>
  19.  
  20. void
  21. hsb2rgb(H, S, B, r, g, b)
  22.     double      H,
  23.                 S,
  24.                 B;
  25.     u_char     *r,
  26.                *g,
  27.                *b;
  28. {
  29.     int         i;
  30.     double      f;
  31.     double      bb;
  32.     u_char      p;
  33.     u_char      q;
  34.     u_char      t;
  35.  
  36.     H -= floor(H);        /* remove anything over 1 */
  37.     H *= 6.0;
  38.     i = floor(H);        /* 0..5 */
  39.     f = H - (float) i;        /* f = fractional part of H */
  40.     bb = 255.0 * B;
  41.     p = (u_char) (bb * (1.0 - S));
  42.     q = (u_char) (bb * (1.0 - (S * f)));
  43.     t = (u_char) (bb * (1.0 - (S * (1.0 - f))));
  44.     switch (i) {
  45.     case 0:
  46.     *r = (u_char) bb;
  47.     *g = t;
  48.     *b = p;
  49.     break;
  50.     case 1:
  51.     *r = q;
  52.     *g = (u_char) bb;
  53.     *b = p;
  54.     break;
  55.     case 2:
  56.     *r = p;
  57.     *g = (u_char) bb;
  58.     *b = t;
  59.     break;
  60.     case 3:
  61.     *r = p;
  62.     *g = q;
  63.     *b = (u_char) bb;
  64.     break;
  65.     case 4:
  66.     *r = t;
  67.     *g = p;
  68.     *b = (u_char) bb;
  69.     break;
  70.     case 5:
  71.     *r = (u_char) bb;
  72.     *g = p;
  73.     *b = q;
  74.     break;
  75.     }
  76. }
  77.  
  78.  
  79. /*
  80.  * Input is two points in HSB color space and a count
  81.  * of how many discreet rgb space values the caller wants.
  82.  *
  83.  * Output is that many rgb triples which describe a linear
  84.  * interpolate ramp between the two input colors.
  85.  */
  86.  
  87. void
  88. hsbramp(h1, s1, b1, h2, s2, b2, count, red, green, blue)
  89.     double      h1,
  90.                 s1,
  91.                 b1,
  92.                 h2,
  93.                 s2,
  94.                 b2;
  95.     int         count;
  96.  
  97.     u_char     *red,
  98.                *green,
  99.                *blue;
  100. {
  101.     double      dh;
  102.     double      ds;
  103.     double      db;
  104.  
  105.     dh = (h2 - h1) / count;
  106.     ds = (s2 - s1) / count;
  107.     db = (b2 - b1) / count;
  108.     while (count--) {
  109.     hsb2rgb(h1, s1, b1, red++, green++, blue++);
  110.     h1 += dh;
  111.     s1 += ds;
  112.     b1 += db;
  113.     }
  114. }
  115.