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