]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Color.C
remove defaults stuff, let Qt handle no toolbar
[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
14 #include "Color.h"
15 #include <algorithm> // max
16 #include <cmath> // floor
17 #include FORMS_H_LOCATION
18
19 #ifndef CXX_GLOBAL_CSTD
20 using std::floor;
21 #endif
22
23 using std::max;
24 using std::min;
25
26 namespace {
27
28 int const nohue = -1;
29
30 } // namespace anon
31
32
33 bool getRGBColor(LColor::color col,
34                  unsigned int & r, unsigned int & g, unsigned int & b)
35 {
36         string const name = lcolor.getX11Name(col);
37         Display * const display = fl_get_display();
38         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
39         XColor xcol, ccol;
40
41         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
42                 r = 0;
43                 g = 0;
44                 b = 0;
45                 return false;
46         }
47
48         r = xcol.red   / 256;
49         g = xcol.green / 256;
50         b = xcol.blue  / 256;
51         return true;
52 }
53
54
55 RGBColor::RGBColor(HSVColor const & hsv)
56 {
57         double h = hsv.h;
58         double const s = hsv.s;
59         double const v = hsv.v;
60
61         double rd, gd, bd;
62
63         if (h == nohue || s == 0.0) {
64                 rd = gd = bd = v;
65         } else {
66                 if (h == 360.0) h = 0.0;
67                 h /= 60.0;
68
69                 int const j = max(0, static_cast<int>(::floor(h)));
70                 //if (j < 0) j = 0;
71
72                 double const f = h - j;
73                 double const p = v * (1.0 - s);
74                 double const q = v * (1.0 - (s * f));
75                 double const t = v * (1.0 - (s * (1.0 - f)));
76
77                 switch (j) {
78                 case 0:
79                         rd = v;
80                         gd = t;
81                         bd = p;
82                         break;
83                 case 1:
84                         rd = q;
85                         gd = v;
86                         bd = p;
87                         break;
88                 case 2:
89                         rd = p;
90                         gd = v;
91                         bd = t;
92                         break;
93                 case 3:
94                         rd = p;
95                         gd = q;
96                         bd = v;
97                         break;
98                 case 4:
99                         rd = t;
100                         gd = p;
101                         bd = v;
102                         break;
103                 case 5:
104                         rd = v;
105                         gd = p;
106                         bd = q;
107                         break;
108                 default:
109                         rd = v;
110                         gd = t;
111                         bd = p;
112                         break;  // should never happen.
113                 }
114         }
115
116         r = static_cast<int>(::floor((rd * 255.0) + 0.5));
117         g = static_cast<int>(::floor((gd * 255.0) + 0.5));
118         b = static_cast<int>(::floor((bd * 255.0) + 0.5));
119 }
120
121
122 HSVColor::HSVColor(RGBColor const & rgb)
123 {
124         double const r = rgb.r / 255.0;
125         double const g = rgb.g / 255.0;
126         double const b = rgb.b / 255.0;
127
128         double const maxval = max(max(r, g), b);
129         double const minval = min(min(r, g), b);
130
131         v = maxval;
132
133         double const diff = maxval - minval;
134         if (maxval != 0.0)
135                 s = diff / maxval;
136         else
137                 s = 0.0;
138
139         h = nohue;
140         if (s != 0.0) {
141                 double const rc = (maxval - r) / diff;
142                 double const gc = (maxval - g) / diff;
143                 double const bc = (maxval - b) / diff;
144
145                 if (r == maxval)
146                         h = bc - gc;
147                 else if (g == maxval)
148                         h = 2.0 + rc - bc;
149                 else if (b == maxval)
150                         h = 4.0 + gc - rc;
151
152                 h *= 60.0;
153                 if (h < 0)
154                         h += 360;
155         }
156 }