3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "ColorHandler.h"
19 #include "support/lstrings.h"
20 #include "support/tostr.h"
22 #include "lyx_forms.h"
24 #include <boost/scoped_array.hpp>
28 using lyx::support::bformat;
30 #ifndef CXX_GLOBAL_CSTD
40 string tostr(XColor const & col)
42 return bformat("(%1$s,%2$s,%3$s)",
43 ::tostr(col.red), ::tostr(col.green), ::tostr(col.blue));
49 LyXColorHandler::LyXColorHandler()
50 : colorGCcache(LColor::ignore + 1)
52 display = fl_get_display();
53 drawable = XCreatePixmap(display,
54 RootWindow(display, fl_screen),
55 10, 10, fl_get_visual_depth());
57 colormap = fl_state[fl_get_vclass()].colormap;
59 for (string::size_type i = 0; i < colorGCcache.size(); ++i) {
65 LyXColorHandler::~LyXColorHandler()
67 // Release all the registered GCs
68 for (unsigned i = 0; i < colorGCcache.size(); ++i) {
69 if (colorGCcache[i] != 0) {
70 XFreeGC(display, colorGCcache[i]);
73 // Iterate over the line cache and Free the GCs
74 for (LineGCCache::iterator lit = lineGCcache.begin();
75 lit != lineGCcache.end(); ++lit) {
76 XFreeGC(display, lit->second);
81 unsigned long LyXColorHandler::colorPixel(LColor_color c)
84 XGetGCValues(display, getGCForeground(c), GCForeground, &val);
85 return val.foreground;
89 GC LyXColorHandler::getGCForeground(string const & s)
94 // Look up the RGB values for the color, and an approximate
95 // color that we can hope to get on this display.
96 if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
98 _("LyX: Unknown X11 color %1$s\n"
99 " Using black instead, sorry!"),
101 unsigned long bla = BlackPixel(display,
102 DefaultScreen(display));
103 val.foreground = bla;
104 // Try the exact RGB values first, then the approximate.
105 } else if (XAllocColor(display, colormap, &xcol) != 0) {
106 if (lyxerr.debugging(Debug::GUI)) {
107 lyxerr << bformat(_("LyX: X11 color %1$s allocated"),
110 val.foreground = xcol.pixel;
112 // Here we are traversing the current colormap to find
113 // the color closest to the one we want.
114 Visual * vi = DefaultVisual(display, DefaultScreen(display));
116 boost::scoped_array<XColor> cmap(new XColor[vi->map_entries]);
118 for (int i = 0; i < vi->map_entries; ++i) {
121 XQueryColors(display, colormap, cmap.get(), vi->map_entries);
123 // Walk through the cmap and look for close colors.
124 int closest_pixel = 0;
125 double closest_distance = 1e20; // we want to minimize this
127 for (int t = 0; t < vi->map_entries; ++t) {
128 // The Euclidean distance between two points in
129 // a three-dimensional space, the RGB color-cube,
130 // is used as the distance measurement between two
133 // Since square-root is monotonous, we don't have to
134 // take the square-root to find the minimum, and thus
135 // we use the squared distance instead to be faster.
137 // If we want to get fancy, we could convert the RGB
138 // coordinates to a different color-cube, maybe HSV,
139 // but the RGB cube seems to work great. (Asger)
140 distance = pow(cmap[t].red - xcol.red, 2.0) +
141 pow(cmap[t].green - xcol.green, 2.0) +
142 pow(cmap[t].blue - xcol.blue, 2.0);
143 if (distance < closest_distance) {
144 closest_distance = distance;
150 _("LyX: Couldn't allocate '%1$s' with (r,g,b)=%3$s.\n"),
154 _(" Using closest allocated color with (r,g,b)=%1$s instead.\n"
155 "Pixel [%2$s] is used."),
156 tostr(cmap[closest_pixel]), tostr(closest_pixel)) << endl;
158 val.foreground = cmap[closest_pixel].pixel;
160 val.function = GXcopy;
161 return XCreateGC(display, drawable,
162 GCForeground | GCFunction, &val);
165 // Gets GC according to color
167 GC LyXColorHandler::getGCForeground(LColor_color c)
169 if (static_cast<unsigned>(c) >= colorGCcache.size()) {
170 colorGCcache.resize(c + 1, 0);
173 if (colorGCcache[c] != 0) {
174 return colorGCcache[c];
178 string const s = lcolor.getX11Name(c);
179 // Look up the RGB values for the color, and an approximate
180 // color that we can hope to get on this display.
181 if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
183 _("LyX: Unknown X11 color %1$s for %2$s\n"),
184 s, lcolor.getGUIName(c)) << endl;
186 return colorGCcache[c] = getGCForeground(s);
191 GC LyXColorHandler::getGCLinepars(Painter::line_style ls,
192 Painter::line_width lw, LColor_color c)
194 //if (lyxerr.debugging()) {
195 // lyxerr << "Painter drawable: " << drawable() << endl;
198 int index = lw + (ls << 1) + (c << 6);
200 LineGCCache::iterator it = lineGCcache.find(index);
201 if (it != lineGCcache.end())
205 XGetGCValues(display, getGCForeground(c), GCForeground, &val);
208 case Painter::line_thin:
211 case Painter::line_thick:
217 case Painter::line_solid:
218 val.line_style = LineSolid;
220 case Painter::line_onoffdash:
221 val.line_style = LineOnOffDash;
226 val.cap_style = CapRound;
227 val.join_style = JoinRound;
228 val.function = GXcopy;
230 return lineGCcache[index] =
231 XCreateGC(display, drawable,
232 GCForeground | GCLineStyle | GCLineWidth |
233 GCCapStyle | GCJoinStyle | GCFunction, &val);
237 // update GC cache after color redefinition
238 void LyXColorHandler::updateColor (LColor_color c)
241 GC gc = colorGCcache[c];
243 XFreeGC(display, gc);
244 colorGCcache[c] = NULL;
250 for (int ls = 0; ls < 3; ++ls)
251 for (int lw = 0; lw < 2; ++lw) {
252 int const index = lw + (ls << 1) + (c << 6);
253 LineGCCache::iterator it = lineGCcache.find(index);
254 if (it != lineGCcache.end()) {
256 XFreeGC(display, gc);
257 lineGCcache.erase(it);
258 getGCLinepars(Painter::line_style(ls),
259 Painter::line_width(lw), c);
266 boost::scoped_ptr<LyXColorHandler> lyxColorHandler;