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