c5109120af08a8f73f8936d17c83ac06e5bc50c9
[xscreensaver] / utils / hsv.c
1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@netscape.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 /* This file contains some utility routines for randomly picking the colors
13    to hack the screen with.
14  */
15
16 #include <X11/Xlib.h>
17
18 void
19 #if __STDC__
20 hsv_to_rgb (int h, double s, double v,
21             unsigned short *r, unsigned short *g, unsigned short *b)
22 #else
23 hsv_to_rgb (h,s,v, r,g,b)
24      int h;                     /* 0 - 360   */
25      double s, v;               /* 0.0 - 1.0 */
26      unsigned short *r, *g, *b; /* 0 - 65535 */
27 #endif
28 {
29   double H, S, V, R, G, B;
30   double p1, p2, p3;
31   double f;
32   int i;
33   S = s; V = v;
34   H = (h % 360) / 60.0;
35   i = H;
36   f = H - i;
37   p1 = V * (1 - S);
38   p2 = V * (1 - (S * f));
39   p3 = V * (1 - (S * (1 - f)));
40   if      (i == 0) { R = V;  G = p3; B = p1; }
41   else if (i == 1) { R = p2; G = V;  B = p1; }
42   else if (i == 2) { R = p1; G = V;  B = p3; }
43   else if (i == 3) { R = p1; G = p2; B = V;  }
44   else if (i == 4) { R = p3; G = p1; B = V;  }
45   else             { R = V;  G = p1; B = p2; }
46   *r = R * 65535;
47   *g = G * 65535;
48   *b = B * 65535;
49 }
50
51 void
52 #if __STDC__
53 rgb_to_hsv (unsigned short r, unsigned short g, unsigned short b,
54             int *h, double *s, double *v)
55 #else
56 rgb_to_hsv (r,g,b, h,s,v)
57      unsigned short r, g, b;    /* 0 - 65535 */
58      int *h;                    /* 0 - 360   */
59      double *s, *v;             /* 0.0 - 1.0 */
60 #endif
61 {
62   double R, G, B, H, S, V;
63   double cmax, cmin;
64   double cmm;
65   int imax;
66   R = ((double) r) / 65535.0;
67   G = ((double) g) / 65535.0;
68   B = ((double) b) / 65535.0;
69   cmax = R; cmin = G; imax = 1;
70   if  ( cmax < G ) { cmax = G; cmin = R; imax = 2; }
71   if  ( cmax < B ) { cmax = B; imax = 3; }
72   if  ( cmin > B ) { cmin = B; }
73   cmm = cmax - cmin;
74   V = cmax;
75   if (cmm == 0)
76     S = H = 0;
77   else
78     {
79       S = cmm / cmax;
80       if      (imax == 1) H =       (G - B) / cmm;
81       else if (imax == 2) H = 2.0 + (B - R) / cmm;
82       else if (imax == 3) H = 4.0 + (R - G) / cmm;
83       if (H < 0) H += 6.0;
84     }
85   *h = (H * 60.0);
86   *s = S;
87   *v = V;
88 }
89
90
91 void
92 make_color_ramp (h1, s1, v1, h2, s2, v2,
93                  pixels, npixels)
94      int h1, h2;                        /* 0 - 360   */
95      double s1, s2, v1, v2;             /* 0.0 - 1.0 */
96      XColor *pixels;
97      int npixels;
98 {
99   int dh = (h2 - h1) / npixels;
100   double ds = (s2 - s1) / npixels;
101   double dv = (v2 - v1) / npixels;
102   int i;
103   for (i = 0; i < npixels; i++)
104     hsv_to_rgb ((h1 += dh), (s1 += ds), (v1 += dv),
105                 &pixels [i].red, &pixels [i].green, &pixels [i].blue);
106 }
107
108
109 void
110 cycle_hue (color, degrees)
111      XColor *color;
112      int degrees;
113 {
114   int h;
115   double s, v;
116   rgb_to_hsv (color->red, color->green, color->blue,
117               &h, &s, &v);
118   h = (h + degrees) % 360;
119   hsv_to_rgb (h, s, v, &color->red, &color->green, &color->blue);
120 }