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