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