home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / xlock / part01 / HSBmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-24  |  2.5 KB  |  123 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)HSBmap.c 22.2 89/09/20";
  3. #endif
  4. /*-
  5.  * HSBmap.c - Create an HSB ramp.
  6.  *
  7.  * Copyright (c) 1989 by Sun Microsystems, Inc.
  8.  *
  9.  * Author: Patrick J. Naughton
  10.  * naughton@sun.com
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose and without fee is hereby granted,
  14.  * provided that the above copyright notice appear in all copies and that
  15.  * both that copyright notice and this permission notice appear in
  16.  * supporting documentation.
  17.  *
  18.  * This file is provided AS IS with no warranties of any kind.  The author
  19.  * shall have no liability with respect to the infringement of copyrights,
  20.  * trade secrets or any patents by this file or any part thereof.  In no
  21.  * event will the author be liable for any lost revenue or profits or
  22.  * other special, indirect and consequential damages.
  23.  *
  24.  */
  25.  
  26. #include <sys/types.h>
  27. #include <math.h>
  28.  
  29. void
  30. hsb2rgb(H, S, B, r, g, b)
  31.     double      H,
  32.                 S,
  33.                 B;
  34.     u_char     *r,
  35.                *g,
  36.                *b;
  37. {
  38.     register int i;
  39.     register double f,
  40.                 bb;
  41.     register u_char p,
  42.                 q,
  43.                 t;
  44.  
  45.     H -= floor(H);        /* remove anything over 1 */
  46.     H *= 6.0;
  47.     i = floor(H);        /* 0..5 */
  48.     f = H - (float) i;        /* f = fractional part of H */
  49.     bb = 255.0 * B;
  50.     p = (u_char) (bb * (1.0 - S));
  51.     q = (u_char) (bb * (1.0 - (S * f)));
  52.     t = (u_char) (bb * (1.0 - (S * (1.0 - f))));
  53.     switch (i) {
  54.     case 0:
  55.     *r = (u_char) bb;
  56.     *g = t;
  57.     *b = p;
  58.     break;
  59.     case 1:
  60.     *r = q;
  61.     *g = (u_char) bb;
  62.     *b = p;
  63.     break;
  64.     case 2:
  65.     *r = p;
  66.     *g = (u_char) bb;
  67.     *b = t;
  68.     break;
  69.     case 3:
  70.     *r = p;
  71.     *g = q;
  72.     *b = (u_char) bb;
  73.     break;
  74.     case 4:
  75.     *r = t;
  76.     *g = p;
  77.     *b = (u_char) bb;
  78.     break;
  79.     case 5:
  80.     *r = (u_char) bb;
  81.     *g = p;
  82.     *b = q;
  83.     break;
  84.     }
  85. }
  86.  
  87.  
  88. void
  89. HSBramp(h1, s1, b1, h2, s2, b2, start, end, red, green, blue)
  90.     double      h1,
  91.                 s1,
  92.                 b1,
  93.                 h2,
  94.                 s2,
  95.                 b2;
  96.     int         start,
  97.                 end;
  98.     u_char     *red,
  99.                *green,
  100.                *blue;
  101. {
  102.     double      dh,
  103.                 ds,
  104.                 db;
  105.     register u_char *r,
  106.                *g,
  107.                *b;
  108.     register int i;
  109.  
  110.     r = red;
  111.     g = green;
  112.     b = blue;
  113.     dh = (h2 - h1) / 255.0;
  114.     ds = (s2 - s1) / 255.0;
  115.     db = (b2 - b1) / 255.0;
  116.     for (i = start; i <= end; i++) {
  117.     hsb2rgb(h1, s1, b1, r++, g++, b++);
  118.     h1 += dh;
  119.     s1 += ds;
  120.     b1 += db;
  121.     }
  122. }
  123.