home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / GRAPHICS / ssaver.lzh / SRC / hsbramp.c < prev    next >
Text File  |  1995-10-31  |  3KB  |  126 lines

  1. /*-
  2.  * hsbramp.c - Create an HSB ramp.
  3.  *
  4.  * Copyright (c) 1991 by Patrick J. Naughton.
  5.  *
  6.  * Revision History:
  7.  * 29-Oct-95: Port to OSK for the MM/1 by Blair Leduc (b.leduc@ieee.org)
  8.  * 29-Jul-90: renamed hsbramp.c from HSBmap.c
  9.  *          minor optimizations.
  10.  * 01-Sep-88: Written.
  11.  */
  12.  
  13. /*
  14.  Permission to use, copy, modify, and distribute this software and its
  15.  documentation for any purpose and without fee is hereby granted,
  16.  provided that the above copyright notice appear in all copies and that
  17.  both that copyright notice and this permission notice appear in
  18.  supporting documentation.
  19.  
  20.  This file is provided AS IS with no warranties of any kind.  The author
  21.  shall have no liability with respect to the infringement of copyrights,
  22.  trade secrets or any patents by this file or any part thereof.  In no
  23.  event will the author be liable for any lost revenue or profits or
  24.  other special, indirect and consequential damages.
  25.  */
  26.  
  27. #include <types.h>
  28. #include <math.h>
  29.  
  30. void
  31. hsb2rgb(H, S, B, r, g, b)
  32.     double      H,
  33.                 S,
  34.                 B;
  35.     unsigned char     *r,
  36.                *g,
  37.                *b;
  38. {
  39.     int         i;
  40.     double      f;
  41.     double      bb;
  42.     unsigned char      p;
  43.     unsigned char      q;
  44.     unsigned char      t;
  45.  
  46.     H -= floor(H);        /* remove anything over 1 */
  47.     H *= 6.0;
  48.     i = floor(H);        /* 0..5 */
  49.     f = H - (float) i;        /* f = fractional part of H */
  50.     bb = 255.0 * B;
  51.     p = (unsigned char) (bb * (1.0 - S));
  52.     q = (unsigned char) (bb * (1.0 - (S * f)));
  53.     t = (unsigned char) (bb * (1.0 - (S * (1.0 - f))));
  54.     switch (i) {
  55.     case 0:
  56.     *r = (unsigned char) bb;
  57.     *g = t;
  58.     *b = p;
  59.     break;
  60.     case 1:
  61.     *r = q;
  62.     *g = (unsigned char) bb;
  63.     *b = p;
  64.     break;
  65.     case 2:
  66.     *r = p;
  67.     *g = (unsigned char) bb;
  68.     *b = t;
  69.     break;
  70.     case 3:
  71.     *r = p;
  72.     *g = q;
  73.     *b = (unsigned char) bb;
  74.     break;
  75.     case 4:
  76.     *r = t;
  77.     *g = p;
  78.     *b = (unsigned char) bb;
  79.     break;
  80.     case 5:
  81.     *r = (unsigned char) bb;
  82.     *g = p;
  83.     *b = q;
  84.     break;
  85.     }
  86. }
  87.  
  88.  
  89. /*
  90.  * Input is two points in HSB color space and a count
  91.  * of how many discreet rgb space values the caller wants.
  92.  *
  93.  * Output is that many rgb triples which describe a linear
  94.  * interpolate ramp between the two input colors.
  95.  */
  96.  
  97. void
  98. hsbramp(h1, s1, b1, h2, s2, b2, count, pal)
  99.     double      h1,
  100.                 s1,
  101.                 b1,
  102.                 h2,
  103.                 s2,
  104.                 b2;
  105.     int         count;
  106.  
  107.     unsigned char     *pal;
  108. {
  109.     double      dh;
  110.     double      ds;
  111.     double      db;
  112.     int max=count;
  113.  
  114.     dh = (h2 - h1) / count;
  115.     ds = (s2 - s1) / count;
  116.     db = (b2 - b1) / count;
  117.     while (count) {
  118.         hsb2rgb(h1, s1, b1, &pal[(max-count)*3], &pal[(max-count)*3+1],
  119.             &pal[(max-count)*3+2]);
  120.         count--;
  121.         h1 += dh;
  122.         s1 += ds;
  123.         b1 += db;
  124.     }
  125. }
  126.