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