]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Color.C
compiling not tested. two patches from angus with some small fixes
[lyx.git] / src / frontends / xforms / Color.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 1995 Matthias Ettrich
8  *          Copyright 1995-2000 The LyX Team.
9  *
10  *======================================================*/
11
12 #include <config.h>
13 #include FORMS_H_LOCATION
14
15 #ifdef __GNUG_
16 #pragma implementation
17 #endif
18
19 #include <algorithm> // max
20 #include <cmath> // floor
21 #include "Color.h"
22
23 using std::max;
24 using std::min;
25
26 static int const nohue = -1;
27
28 RGBColor::RGBColor(HSVColor const & hsv)
29 {
30         double h = hsv.h;
31         double const s = hsv.s;
32         double const v = hsv.v;
33         
34         double rd, gd, bd;
35         
36         if (h == nohue || s == 0.0) {
37                 rd = gd = bd = v;
38         } else {
39                 if (h == 360.0) h = 0.0;
40                 h /= 60.0;
41
42                 int const j = max(0, static_cast<int>(::floor(h)));
43                 //if( j < 0 ) j = 0;
44
45                 double const f = h - j;
46                 double const p = v * (1.0 - s);
47                 double const q = v * (1.0 - (s * f));
48                 double const t = v * (1.0 - (s * (1.0 - f)));
49
50                 switch (j) {
51                 case 0:
52                         rd = v;
53                         gd = t;
54                         bd = p;
55                         break;
56                 case 1:
57                         rd = q;
58                         gd = v;
59                         bd = p;
60                         break;
61                 case 2:
62                         rd = p;
63                         gd = v;
64                         bd = t;
65                         break;
66                 case 3:
67                         rd = p;
68                         gd = q;
69                         bd = v;
70                         break;
71                 case 4:
72                         rd = t;
73                         gd = p;
74                         bd = v;
75                         break;
76                 case 5:
77                         rd = v;
78                         gd = p;
79                         bd = q;
80                         break;
81                 default:
82                         rd = v;
83                         gd = t;
84                         bd = p;
85                         break;  // should never happen.
86                 }
87         }
88
89         r = static_cast<int>( ::floor((rd * 255.0) + 0.5) );
90         g = static_cast<int>( ::floor((gd * 255.0) + 0.5) );
91         b = static_cast<int>( ::floor((bd * 255.0) + 0.5) );
92 }
93
94
95 HSVColor::HSVColor(RGBColor const & rgb)
96 {
97         // r, g, b lie in the range 0-1, not 0-255.
98         double const r = rgb.r / 255.0;
99         double const g = rgb.g / 255.0;
100         double const b = rgb.b / 255.0;
101
102         double const maxval = max( max( r, g ), b );
103         double const minval = max( min( r, g ), b );
104
105         v = maxval;
106
107         double const diff = maxval - minval;
108         if (maxval != 0.0)
109                 s = diff / maxval;
110         else
111                 s = 0.0;
112
113         h = nohue;
114         if (s != 0.0) {
115                 double const rc = (maxval - r) / diff;
116                 double const gc = (maxval - g) / diff;
117                 double const bc = (maxval - b) / diff;
118
119                 if (r == maxval)
120                         h = bc - gc;
121                 else if (g == maxval)
122                         h = 2.0 + rc - bc;
123                 else if (b == maxval)
124                         h = 4.0 + gc - rc;
125
126                 h *= 60.0;
127                 if (h < 0)
128                         h += 360;
129         }
130 }