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