]> git.lyx.org Git - lyx.git/blob - src/Color.cpp
Update my email and status.
[lyx.git] / src / Color.cpp
1 /**
2  * \file Color.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Matthias Ettrich
9  * \author Jean-Marc Lasgouttes
10  * \author John Levon
11  * \author André Pönitz
12  * \author Martin Vermeer
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "Color.h"
20 #include "ColorSet.h"
21
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26 #include "support/lassert.h"
27
28 #include <map>
29 #include <cmath>
30 #include <sstream>
31 #include <iomanip>
32
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39
40 struct ColorSet::ColorEntry {
41         ColorCode lcolor;
42         char const * guiname;
43         char const * latexname;
44         char const * x11name;
45         char const * lyxname;
46 };
47
48
49 static int hexstrToInt(string const & str)
50 {
51         int val = 0;
52         istringstream is(str);
53         is >> setbase(16) >> val;
54         return val;
55 }
56
57
58 /////////////////////////////////////////////////////////////////////
59 //
60 // RGBColor
61 //
62 /////////////////////////////////////////////////////////////////////
63
64
65 string const X11hexname(RGBColor const & col)
66 {
67         ostringstream ostr;
68
69         ostr << '#' << setbase(16) << setfill('0')
70              << setw(2) << col.r
71              << setw(2) << col.g
72              << setw(2) << col.b;
73
74         return ostr.str();
75 }
76
77
78 RGBColor rgbFromHexName(string const & x11hexname)
79 {
80         RGBColor c;
81         LASSERT(x11hexname.size() == 7 && x11hexname[0] == '#', /**/);
82         c.r = hexstrToInt(x11hexname.substr(1, 2));
83         c.g = hexstrToInt(x11hexname.substr(3, 2));
84         c.b = hexstrToInt(x11hexname.substr(5, 2));
85         return c;
86 }
87
88
89 string const outputLaTeXColor(RGBColor const & color)
90 {
91         // this routine returns a LaTeX readable color string in the form
92         // "red, green, blue" where the colors are a number in the range 0-1 
93         int red = color.r;
94         int green = color.g;
95         int blue = color.b;
96 #ifdef USE_CORRECT_RGB_CONVERSION
97         int const scale = 255;
98 #else
99         // the color values are given in the range of 0-255, so to get
100         // an output of "0.5" for the value 127 we need to do the following
101         // FIXME: This is wrong, since it creates a nonlinear mapping:
102         //        There is a gap between 0/256 and 2/256!
103         //        0.5 cannot be represented in 8bit hex RGB, it would be 127.5.
104         if (red != 0)
105                 ++red;
106         if (green != 0)
107                 ++green;
108         if (blue != 0)
109                 ++blue;
110         int const scale = 256;
111 #endif
112         string output;
113         output = convert<string>(float(red) / scale) + ", "
114                          + convert<string>(float(green) / scale) + ", "
115                          + convert<string>(float(blue) / scale);
116         return output;
117 }
118
119
120 RGBColor const RGBColorFromLaTeX(string const & color)
121 {
122         vector<string> rgb = getVectorFromString(color);
123         while (rgb.size() < 3)
124                 rgb.push_back("0");
125         RGBColor c;
126         for (int i = 0; i < 3; ++i) {
127                 rgb[i] = trim(rgb[i]);
128                 if (!isStrDbl(rgb[i]))
129                         return c;
130         }
131 #ifdef USE_CORRECT_RGB_CONVERSION
132         int const scale = 255;
133 #else
134         // FIXME: This is wrong, since it creates a nonlinear mapping:
135         //        Both 0/256 and 1/256 are mapped to 0!
136         //        The wrong code exists only to match outputLaTeXColor().
137         int const scale = 256;
138 #endif
139         c.r = static_cast<unsigned int>(scale * convert<double>(rgb[0]) + 0.5);
140         c.g = static_cast<unsigned int>(scale * convert<double>(rgb[1]) + 0.5);
141         c.b = static_cast<unsigned int>(scale * convert<double>(rgb[2]) + 0.5);
142 #ifndef USE_CORRECT_RGB_CONVERSION
143         if (c.r != 0)
144                 c.r--;
145         if (c.g != 0)
146                 c.g--;
147         if (c.b != 0)
148                 c.b--;
149 #endif
150         return c;
151 }
152
153
154 Color::Color(ColorCode base_color) : baseColor(base_color), 
155         mergeColor(Color_ignore)
156 {}
157
158
159 bool Color::operator==(Color const & color) const
160 {
161         return baseColor == color.baseColor;
162 }
163
164
165 bool Color::operator!=(Color const & color) const       
166 {
167         return baseColor != color.baseColor;
168 }
169
170
171 bool Color::operator<(Color const & color) const
172 {
173         return baseColor < color.baseColor;
174 }
175
176
177 bool Color::operator<=(Color const & color) const
178 {
179         return baseColor <= color.baseColor;
180 }
181
182
183 std::ostream & operator<<(std::ostream & os, Color color)
184 {
185         os << to_ascii(lcolor.getGUIName(color.baseColor));
186         if (color.mergeColor != Color_ignore)
187                 os << "[merged with:"
188                         << to_ascii(lcolor.getGUIName(color.mergeColor)) << "]";
189         return os;
190 }
191
192
193 ColorSet::ColorSet()
194 {
195         char const * grey40 = "#666666";
196         char const * grey60 = "#999999";
197         char const * grey80 = "#cccccc";
198         //char const * grey90 = "#e5e5e5";
199         //  ColorCode, gui, latex, x11, lyx
200         // Warning: several of these entries are overridden in GuiApplication constructor
201         static ColorEntry const items[] = {
202         { Color_none, N_("none"), "none", "black", "none" },
203         { Color_black, N_("black"), "black", "black", "black" },
204         { Color_white, N_("white"), "white", "white", "white" },
205         { Color_red, N_("red"), "red", "red", "red" },
206         { Color_green, N_("green"), "green", "green", "green" },
207         { Color_blue, N_("blue"), "blue", "blue", "blue" },
208         { Color_cyan, N_("cyan"), "cyan", "cyan", "cyan" },
209         { Color_magenta, N_("magenta"), "magenta", "magenta", "magenta" },
210         { Color_yellow, N_("yellow"), "yellow", "yellow", "yellow" },
211         { Color_cursor, N_("cursor"), "cursor", "black", "cursor" },
212         { Color_background, N_("background"), "background", "linen", "background" },
213         { Color_foreground, N_("text"), "foreground", "black", "foreground" },
214         { Color_selection, N_("selection"), "selection", "LightBlue", "selection" },
215         { Color_selectiontext, N_("selected text"),
216                 "selectiontext", "black", "selectiontext" },
217         { Color_latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
218         { Color_inlinecompletion, N_("inline completion"),
219                 "inlinecompletion", grey60, "inlinecompletion" },
220         { Color_nonunique_inlinecompletion, N_("non-unique inline completion"),
221                 "nonuniqueinlinecompletion", grey80, "nonuniqueinlinecompletion" },
222         { Color_preview, N_("previewed snippet"), "preview", "black", "preview" },
223         { Color_notelabel, N_("note label"), "note", "yellow", "note" },
224         { Color_notebg, N_("note background"), "notebg", "yellow", "notebg" },
225         { Color_commentlabel, N_("comment label"), "comment", "magenta", "comment" },
226         { Color_commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
227         { Color_greyedoutlabel, N_("greyedout inset label"), "greyedout", "#ff0080", "greyedout" },
228         { Color_greyedouttext, N_("greyedout inset text"), "greyedouttext", grey80, "greyedouttext" },
229         { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
230         { Color_phantomtext, N_("phantom inset text"), "phantomtext", "#7f7f7f", "phantomtext" },
231         { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
232         { Color_listingsbg, N_("listings background"), "listingsbg", "white", "listingsbg" },
233         { Color_branchlabel, N_("branch label"), "branchlabel", "#c88000", "branchlabel" },
234         { Color_footlabel, N_("footnote label"), "footlabel", "#00aaff", "footlabel" },
235         { Color_indexlabel, N_("index label"), "indexlabel", "green", "indexlabel" },
236         { Color_marginlabel, N_("margin note label"), "marginlabel", "#aa55ff", "marginlabel" },
237         { Color_urllabel, N_("URL label"), "urllabel", "blue", "urllabel" },
238         { Color_urltext, N_("URL text"), "urltext", "blue", "urltext" },
239         { Color_depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
240         { Color_language, N_("language"), "language", "Blue", "language" },
241         { Color_command, N_("command inset"), "command", "black", "command" },
242         { Color_commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
243         { Color_commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
244         { Color_special, N_("special character"), "special", "RoyalBlue", "special" },
245         { Color_math, N_("math"), "math", "DarkBlue", "math" },
246         { Color_mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
247         { Color_graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
248         { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
249         { Color_mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
250         { Color_mathcorners, N_("math corners"), "mathcorners", "linen", "mathcorners" },
251         { Color_mathline, N_("math line"), "mathline", "Blue", "mathline" },
252         { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "#ede2d8", "mathmacrobg" },
253         { Color_mathmacrohoverbg, N_("math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", "mathmacrohoverbg" },
254         { Color_mathmacrolabel, N_("math macro label"), "mathmacrolabel", "#a19992", "mathmacrolabel" },
255         { Color_mathmacroframe, N_("math macro frame"), "mathmacroframe", "#ede2d8", "mathmacroframe" },
256         { Color_mathmacroblend, N_("math macro blended out"), "mathmacroblend", "black", "mathmacroblend" },
257         { Color_mathmacrooldarg, N_("math macro old parameter"), "mathmacrooldarg", grey80, "mathmacrooldarg" },
258         { Color_mathmacronewarg, N_("math macro new parameter"), "mathmacronewarg", "black", "mathmacronewarg" },
259         { Color_collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
260         { Color_collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
261         { Color_insetbg, N_("inset background"), "insetbg", grey80, "insetbg" },
262         { Color_insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
263         { Color_error, N_("LaTeX error"), "error", "Red", "error" },
264         { Color_eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
265         { Color_appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
266         { Color_changebar, N_("change bar"), "changebar", "Blue", "changebar" },
267         { Color_deletedtext, N_("deleted text"), "deletedtext", "#ff0000", "deletedtext" },
268         { Color_addedtext, N_("added text"), "addedtext", "#0000ff", "addedtext" },
269         { Color_changedtextauthor1, N_("changed text 1st author"), "changedtextauthor1", "#0000ff", "changedtextauthor1" },
270         { Color_changedtextauthor2, N_("changed text 2nd author"), "changedtextauthor2", "#ff00ff", "changedtextauthor2" },
271         { Color_changedtextauthor3, N_("changed text 3rd author"), "changedtextauthor3", "#ff0000", "changedtextauthor3" },
272         { Color_changedtextauthor4, N_("changed text 4th author"), "changedtextauthor4", "#aa00ff", "changedtextauthor4" },
273         { Color_changedtextauthor5, N_("changed text 5th author"), "changedtextauthor5", "#55aa00", "changedtextauthor5" },
274         { Color_deletedtextmodifier, N_("deleted text modifier"), "deletedtextmodifier", "white", "deletedtextmodifier" },
275         { Color_added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
276         { Color_tabularline, N_("table line"), "tabularline", "black", "tabularline" },
277         { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline",
278              "LightSteelBlue", "tabularonoffline" },
279         { Color_bottomarea, N_("bottom area"), "bottomarea", grey40, "bottomarea" },
280         { Color_newpage, N_("new page"), "newpage", "Blue", "newpage" },
281         { Color_pagebreak, N_("page break / line break"), "pagebreak", "RoyalBlue", "pagebreak" },
282         { Color_buttonframe, N_("frame of button"), "buttonframe", "#dcd2c8", "buttonframe" },
283         { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
284         { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
285         { Color_paragraphmarker, N_("paragraph marker"), "paragraphmarker", grey80, "paragraphmarker"},
286         { Color_previewframe, N_("preview frame"), "previewframe", "black", "previewframe"},
287         { Color_inherit, N_("inherit"), "inherit", "black", "inherit" },
288         { Color_regexpframe, N_("regexp frame"), "green", "green", "green" },
289         { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
290         { Color_ignore, 0, 0, 0, 0 }
291         };
292
293         for (int i = 0; items[i].guiname; ++i)
294                 fill(items[i]);
295 }
296
297
298 /// initialise a color entry
299 void ColorSet::fill(ColorEntry const & entry)
300 {
301         Information in;
302         in.lyxname   = entry.lyxname;
303         in.latexname = entry.latexname;
304         in.x11name   = entry.x11name;
305         in.guiname   = entry.guiname;
306         infotab[entry.lcolor] = in;
307         lyxcolors[entry.lyxname] = entry.lcolor;
308         latexcolors[entry.latexname] = entry.lcolor;
309 }
310
311
312 docstring const ColorSet::getGUIName(ColorCode c) const
313 {
314         InfoTab::const_iterator it = infotab.find(c);
315         if (it != infotab.end())
316                 return _(it->second.guiname);
317         return from_ascii("none");
318 }
319
320
321 string const ColorSet::getX11Name(ColorCode c) const
322 {
323         InfoTab::const_iterator it = infotab.find(c);
324         if (it != infotab.end())
325                 return it->second.x11name;
326
327         lyxerr << "LyX internal error: Missing color"
328                   " entry in Color.cpp for " << c << '\n'
329                << "Using black." << endl;
330         return "black";
331 }
332
333
334 string const ColorSet::getLaTeXName(ColorCode c) const
335 {
336         InfoTab::const_iterator it = infotab.find(c);
337         if (it != infotab.end())
338                 return it->second.latexname;
339         return "black";
340 }
341
342
343 string const ColorSet::getLyXName(ColorCode c) const
344 {
345         InfoTab::const_iterator it = infotab.find(c);
346         if (it != infotab.end())
347                 return it->second.lyxname;
348         return "black";
349 }
350
351
352 bool ColorSet::setColor(ColorCode col, string const & x11name)
353 {
354         InfoTab::iterator it = infotab.find(col);
355         if (it == infotab.end()) {
356                 LYXERR0("Color " << col << " not found in database.");
357                 return false;
358         }
359
360         // "inherit" is returned for colors not in the database
361         // (and anyway should not be redefined)
362         if (col == Color_none || col == Color_inherit || col == Color_ignore) {
363                 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
364                 return false;
365         }
366
367         it->second.x11name = x11name;
368         return true;
369 }
370
371
372 bool ColorSet::setColor(string const & lyxname, string const &x11name)
373 {
374         string const lcname = ascii_lowercase(lyxname);
375         if (lyxcolors.find(lcname) == lyxcolors.end()) {
376                 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
377                        << lyxname << '"');
378                 addColor(static_cast<ColorCode>(infotab.size()), lcname);
379         }
380
381         return setColor(lyxcolors[lcname], x11name);
382 }
383
384
385 void ColorSet::addColor(ColorCode c, string const & lyxname)
386 {
387         ColorEntry ce = { c, "", "", "", lyxname.c_str() };
388         fill(ce);
389 }
390
391
392 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
393 {
394         string const lcname = ascii_lowercase(lyxname);
395         Transform::const_iterator it = lyxcolors.find(lcname);
396         if (it == lyxcolors.end()) {
397                 LYXERR0("ColorSet::getFromLyXName: Unknown color \""
398                        << lyxname << '"');
399                 return Color_none;
400         }
401
402         return it->second;
403 }
404
405
406 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
407 {
408         Transform::const_iterator it = latexcolors.find(latexname);
409         if (it == latexcolors.end()) {
410                 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
411                        << latexname << '"' << endl;
412                 return Color_none;
413         }
414
415         return it->second;
416 }
417
418
419 // The evil global Color instance
420 ColorSet lcolor;
421 // An equally evil global system Color instance
422 ColorSet system_lcolor;
423
424
425 } // namespace lyx