3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
15 #include "support/LAssert.h"
17 #include "lyx_forms.h"
19 #include "support/std_sstream.h"
22 namespace support = lyx::support;
24 #ifndef CXX_GLOBAL_CSTD
37 int hexstrToInt(string const & str)
40 istringstream is(str);
41 is >> std::setbase(16) >> val;
49 bool getRGBColor(LColor::color col,
50 unsigned int & r, unsigned int & g, unsigned int & b)
52 string const name = lcolor.getX11Name(col);
53 Display * const display = fl_get_display();
54 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
57 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
71 string const X11hexname(RGBColor const & col)
75 ostr << '#' << std::setbase(16) << std::setfill('0')
80 return STRCONV(ostr.str());
84 RGBColor::RGBColor(string const & x11hexname)
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));
94 RGBColor::RGBColor(HSVColor const & hsv)
97 double const s = hsv.s;
98 double const v = hsv.v;
102 if (h == nohue || s == 0.0) {
105 if (h == 360.0) h = 0.0;
108 int const j = max(0, static_cast<int>(::floor(h)));
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)));
151 break; // should never happen.
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));
161 HSVColor::HSVColor(RGBColor const & rgb)
163 double const r = rgb.r / 255.0;
164 double const g = rgb.g / 255.0;
165 double const b = rgb.b / 255.0;
167 double const maxval = max(max(r, g), b);
168 double const minval = min(min(r, g), b);
172 double const diff = maxval - minval;
180 double const rc = (maxval - r) / diff;
181 double const gc = (maxval - g) / diff;
182 double const bc = (maxval - b) / diff;
186 else if (g == maxval)
188 else if (b == maxval)