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