1 /* xscreensaver, Copyright (c) 1992, 1997 Jamie Zawinski <jwz@jwz.org>
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 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19
20 #include <stdlib.h>
21
22 #include <X11/Xlib.h>
23
24 #include "hsv.h"
25
26 void
hsv_to_rgb(int h,double s,double v,unsigned short * r,unsigned short * g,unsigned short * b)27 hsv_to_rgb (int h, double s, double v,
28 unsigned short *r, unsigned short *g, unsigned short *b)
29 {
30 double H, S, V, R, G, B;
31 double p1, p2, p3;
32 double f;
33 int i;
34
35 if (s < 0) s = 0;
36 if (v < 0) v = 0;
37 if (s > 1) s = 1;
38 if (v > 1) v = 1;
39
40 S = s; V = v;
41 H = (h % 360) / 60.0;
42 i = H;
43 f = H - i;
44 p1 = V * (1 - S);
45 p2 = V * (1 - (S * f));
46 p3 = V * (1 - (S * (1 - f)));
47 if (i == 0) { R = V; G = p3; B = p1; }
48 else if (i == 1) { R = p2; G = V; B = p1; }
49 else if (i == 2) { R = p1; G = V; B = p3; }
50 else if (i == 3) { R = p1; G = p2; B = V; }
51 else if (i == 4) { R = p3; G = p1; B = V; }
52 else { R = V; G = p1; B = p2; }
53 *r = R * 65535;
54 *g = G * 65535;
55 *b = B * 65535;
56 }
57
58 void
rgb_to_hsv(unsigned short r,unsigned short g,unsigned short b,int * h,double * s,double * v)59 rgb_to_hsv (unsigned short r, unsigned short g, unsigned short b,
60 int *h, double *s, double *v)
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