dtwm: basic multihead(xinerama only) support
[oweals/cde.git] / cde / programs / dtscreen / hsbramp.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: hsbramp.c /main/3 1995/11/02 16:07:23 rswiston $ */
24 /*
25  */
26 /*                                                                      *
27  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
28  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
29  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
30  * (c) Copyright 1993, 1994 Novell, Inc.                                *
31  */
32 /*-
33  * hsbramp.c - Create an HSB ramp.
34  *
35  * Copyright (c) 1991 by Patrick J. Naughton.
36  *
37  * See dtscreen.c for copying information.
38  *
39  * Revision History:
40  * 29-Jul-90: renamed hsbramp.c from HSBmap.c
41  *            minor optimizations.
42  * 01-Sep-88: Written.
43  */
44
45 #include <sys/types.h>
46 #include <math.h>
47
48 void
49 hsb2rgb(H, S, B, r, g, b)
50     double      H,
51                 S,
52                 B;
53     u_char     *r,
54                *g,
55                *b;
56 {
57     int         i;
58     double      f;
59     double      bb;
60     u_char      p;
61     u_char      q;
62     u_char      t;
63
64     H -= floor(H);              /* remove anything over 1 */
65     H *= 6.0;
66     i = floor(H);               /* 0..5 */
67     f = H - (float) i;          /* f = fractional part of H */
68     bb = 255.0 * B;
69     p = (u_char) (bb * (1.0 - S));
70     q = (u_char) (bb * (1.0 - (S * f)));
71     t = (u_char) (bb * (1.0 - (S * (1.0 - f))));
72     switch (i) {
73     case 0:
74         *r = (u_char) bb;
75         *g = t;
76         *b = p;
77         break;
78     case 1:
79         *r = q;
80         *g = (u_char) bb;
81         *b = p;
82         break;
83     case 2:
84         *r = p;
85         *g = (u_char) bb;
86         *b = t;
87         break;
88     case 3:
89         *r = p;
90         *g = q;
91         *b = (u_char) bb;
92         break;
93     case 4:
94         *r = t;
95         *g = p;
96         *b = (u_char) bb;
97         break;
98     case 5:
99         *r = (u_char) bb;
100         *g = p;
101         *b = q;
102         break;
103     }
104 }
105
106
107 /*
108  * Input is two points in HSB color space and a count
109  * of how many discreet rgb space values the caller wants.
110  *
111  * Output is that many rgb triples which describe a linear
112  * interpolate ramp between the two input colors.
113  */
114
115 void
116 hsbramp(double h1, double s1, double b1,
117         double h2, double s2, double b2,
118         int count,
119         u_char *red, u_char *green, u_char *blue)
120 {
121     double      dh;
122     double      ds;
123     double      db;
124
125     dh = (h2 - h1) / count;
126     ds = (s2 - s1) / count;
127     db = (b2 - b1) / count;
128     while (count--) {
129         hsb2rgb(h1, s1, b1, red++, green++, blue++);
130         h1 += dh;
131         s1 += ds;
132         b1 += db;
133     }
134 }