1 /* xscreensaver, Copyright (c) 1992, 1997 Jamie Zawinski <jwz@jwz.org>
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
12 /* This file contains some utility routines for randomly picking the colors
13 to hack the screen with.
20 hsv_to_rgb (int h, double s, double v,
21 unsigned short *r, unsigned short *g, unsigned short *b)
23 double H, S, V, R, G, B;
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; }
52 rgb_to_hsv (unsigned short r, unsigned short g, unsigned short b,
53 int *h, double *s, double *v)
55 double R, G, B, H, S, V;
59 R = ((double) r) / 65535.0;
60 G = ((double) g) / 65535.0;
61 B = ((double) b) / 65535.0;
62 cmax = R; cmin = G; imax = 1;
63 if ( cmax < G ) { cmax = G; cmin = R; imax = 2; }
64 if ( cmax < B ) { cmax = B; imax = 3; }
65 if ( cmin > B ) { cmin = B; }
73 if (imax == 1) H = (G - B) / cmm;
74 else if (imax == 2) H = 2.0 + (B - R) / cmm;
75 else /*if (imax == 3)*/ H = 4.0 + (R - G) / cmm;