ftp://ftp.ntnu.no/old/pub/X11/R5contrib/xscreensaver-1.17.tar.gz
[xscreensaver] / utils / hsv.c
1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@lucid.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 hsv_to_rgb (h,s,v, r,g,b)
20      int h;                     /* 0 - 360   */
21      double s, v;               /* 0.0 - 1.0 */
22      unsigned short *r, *g, *b; /* 0 - 65535 */
23 {
24   double H, S, V, R, G, B;
25   double p1, p2, p3;
26   double f;
27   int i;
28   S = s; V = v;
29   H = (h % 360) / 60.0;
30   i = H;
31   f = H - i;
32   p1 = V * (1 - S);
33   p2 = V * (1 - (S * f));
34   p3 = V * (1 - (S * (1 - f)));
35   if      (i == 0) { R = V;  G = p3; B = p1; }
36   else if (i == 1) { R = p2; G = V;  B = p1; }
37   else if (i == 2) { R = p1; G = V;  B = p3; }
38   else if (i == 3) { R = p1; G = p2; B = V;  }
39   else if (i == 4) { R = p3; G = p1; B = V;  }
40   else             { R = V;  G = p1; B = p2; }
41   *r = R * 65535;
42   *g = G * 65535;
43   *b = B * 65535;
44 }
45
46 void
47 rgb_to_hsv (r,g,b, h,s,v)
48      unsigned short r, g, b;    /* 0 - 65535 */
49      int *h;                    /* 0 - 360   */
50      double *s, *v;             /* 0.0 - 1.0 */
51 {
52   double R, G, B, H, S, V;
53   double cmax, cmin;
54   double cmm;
55   int imax;
56   R = ((double) r) / 65535.0;
57   G = ((double) g) / 65535.0;
58   B = ((double) b) / 65535.0;
59   cmax = R; cmin = G; imax = 1;
60   if  ( cmax < G ) { cmax = G; cmin = R; imax = 2; }
61   if  ( cmax < B ) { cmax = B; imax = 3; }
62   if  ( cmin > B ) { cmin = B; }
63   cmm = cmax - cmin;
64   V = cmax;
65   if (cmm == 0)
66     S = H = 0;
67   else
68     {
69       S = cmm / cmax;
70       if      (imax == 1) H =       (G - B) / cmm;
71       else if (imax == 2) H = 2.0 + (B - R) / cmm;
72       else if (imax == 3) H = 4.0 + (R - G) / cmm;
73       if (H < 0) H += 6.0;
74     }
75   *h = (H * 60.0);
76   *s = S;
77   *v = V;
78 }
79
80
81 void
82 make_color_ramp (h1, s1, v1, h2, s2, v2,
83                  pixels, npixels)
84      int h1, h2;                        /* 0 - 360   */
85      double s1, s2, v1, v2;             /* 0.0 - 1.0 */
86      XColor *pixels;
87      int npixels;
88 {
89   int dh = (h2 - h1) / npixels;
90   double ds = (s2 - s1) / npixels;
91   double dv = (v2 - v1) / npixels;
92   int i;
93   for (i = 0; i < npixels; i++)
94     hsv_to_rgb ((h1 += dh), (s1 += ds), (v1 += dv),
95                 &pixels [i].red, &pixels [i].green, &pixels [i].blue);
96 }
97
98
99 void
100 cycle_hue (color, degrees)
101      XColor *color;
102      int degrees;
103 {
104   int h;
105   double s, v;
106   rgb_to_hsv (color->red, color->green, color->blue,
107               &h, &s, &v);
108   h = (h + degrees) % 360;
109   hsv_to_rgb (h, s, v, &color->red, &color->green, &color->blue);
110 }