]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Color.C
Introduce LFUN_PRINT.
[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 #include "Color.h"
14
15 #include "lyx_forms.h"
16
17 #include "LColor.h"
18
19 #include "support/std_sstream.h"
20
21 #include <cmath>
22 #include <iomanip>
23
24
25 #ifndef CXX_GLOBAL_CSTD
26 using std::floor;
27 #endif
28
29 using std::max;
30 using std::min;
31 using std::setw;
32
33 using std::istringstream;
34 using std::ostringstream;
35 using std::string;
36
37
38 namespace {
39
40 int const nohue = -1;
41
42 int hexstrToInt(string const & str)
43 {
44         int val = 0;
45         istringstream is(str);
46         is >> std::setbase(16) >> val;
47         return val;
48 }
49
50 } // namespace anon
51
52
53
54 bool getRGBColor(LColor_color col,
55                  unsigned int & r, unsigned int & g, unsigned int & b)
56 {
57         string const name = lcolor.getX11Name(col);
58         Display * const display = fl_get_display();
59         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
60         XColor xcol, ccol;
61
62         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
63                 r = 0;
64                 g = 0;
65                 b = 0;
66                 return false;
67         }
68
69         r = xcol.red   / 256;
70         g = xcol.green / 256;
71         b = xcol.blue  / 256;
72         return true;
73 }
74
75
76 string const X11hexname(RGBColor const & col)
77 {
78         ostringstream ostr;
79
80         ostr << '#' << std::setbase(16) << std::setfill('0')
81              << setw(2) << col.r
82              << setw(2) << col.g
83              << setw(2) << col.b;
84
85         return ostr.str();
86 }
87
88
89 RGBColor::RGBColor(string const & x11hexname)
90         : r(0), g(0), b(0)
91 {
92         BOOST_ASSERT(x11hexname.size() == 7 && x11hexname[0] == '#');
93         r = hexstrToInt(x11hexname.substr(1,2));
94         g = hexstrToInt(x11hexname.substr(3,2));
95         b = hexstrToInt(x11hexname.substr(5,2));
96 }
97
98
99 RGBColor::RGBColor(HSVColor const & hsv)
100 {
101         double h = hsv.h;
102         double const s = hsv.s;
103         double const v = hsv.v;
104
105         double rd, gd, bd;
106
107         if (h == nohue || s == 0.0) {
108                 rd = gd = bd = v;
109         } else {
110                 if (h == 360.0) h = 0.0;
111                 h /= 60.0;
112
113                 int const j = max(0, static_cast<int>(::floor(h)));
114                 //if (j < 0) j = 0;
115
116                 double const f = h - j;
117                 double const p = v * (1.0 - s);
118                 double const q = v * (1.0 - (s * f));
119                 double const t = v * (1.0 - (s * (1.0 - f)));
120
121                 switch (j) {
122                 case 0:
123                         rd = v;
124                         gd = t;
125                         bd = p;
126                         break;
127                 case 1:
128                         rd = q;
129                         gd = v;
130                         bd = p;
131                         break;
132                 case 2:
133                         rd = p;
134                         gd = v;
135                         bd = t;
136                         break;
137                 case 3:
138                         rd = p;
139                         gd = q;
140                         bd = v;
141                         break;
142                 case 4:
143                         rd = t;
144                         gd = p;
145                         bd = v;
146                         break;
147                 case 5:
148                         rd = v;
149                         gd = p;
150                         bd = q;
151                         break;
152                 default:
153                         rd = v;
154                         gd = t;
155                         bd = p;
156                         break;  // should never happen.
157                 }
158         }
159
160         r = static_cast<int>(::floor((rd * 255.0) + 0.5));
161         g = static_cast<int>(::floor((gd * 255.0) + 0.5));
162         b = static_cast<int>(::floor((bd * 255.0) + 0.5));
163 }
164
165
166 HSVColor::HSVColor(RGBColor const & rgb)
167 {
168         double const r = rgb.r / 255.0;
169         double const g = rgb.g / 255.0;
170         double const b = rgb.b / 255.0;
171
172         double const maxval = max(max(r, g), b);
173         double const minval = min(min(r, g), b);
174
175         v = maxval;
176
177         double const diff = maxval - minval;
178         if (maxval != 0.0)
179                 s = diff / maxval;
180         else
181                 s = 0.0;
182
183         h = nohue;
184         if (s != 0.0) {
185                 double const rc = (maxval - r) / diff;
186                 double const gc = (maxval - g) / diff;
187                 double const bc = (maxval - b) / diff;
188
189                 if (r == maxval)
190                         h = bc - gc;
191                 else if (g == maxval)
192                         h = 2.0 + rc - bc;
193                 else if (b == maxval)
194                         h = 4.0 + gc - rc;
195
196                 h *= 60.0;
197                 if (h < 0)
198                         h += 360;
199         }
200 }