]> git.lyx.org Git - lyx.git/blob - src/Color.cpp
* src/inset/InsetNomencl.cpp:
[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
21 #include "support/debug.h"
22 #include "support/gettext.h"
23 #include "support/lstrings.h"
24 #include "support/lassert.h"
25
26 #include <map>
27 #include <cmath>
28 #include <sstream>
29 #include <iomanip>
30
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37
38 struct ColorSet::ColorEntry {
39         ColorCode lcolor;
40         char const * guiname;
41         char const * latexname;
42         char const * x11name;
43         char const * lyxname;
44 };
45
46
47 static int hexstrToInt(string const & str)
48 {
49         int val = 0;
50         istringstream is(str);
51         is >> setbase(16) >> val;
52         return val;
53 }
54
55
56 /////////////////////////////////////////////////////////////////////
57 //
58 // RGBColor
59 //
60 /////////////////////////////////////////////////////////////////////
61
62
63 string const X11hexname(RGBColor const & col)
64 {
65         ostringstream ostr;
66
67         ostr << '#' << setbase(16) << setfill('0')
68              << setw(2) << col.r
69              << setw(2) << col.g
70              << setw(2) << col.b;
71
72         return ostr.str();
73 }
74
75
76 RGBColor rgbFromHexName(string const & x11hexname)
77 {
78         RGBColor c;
79         LASSERT(x11hexname.size() == 7 && x11hexname[0] == '#', /**/);
80         c.r = hexstrToInt(x11hexname.substr(1,2));
81         c.g = hexstrToInt(x11hexname.substr(3,2));
82         c.b = hexstrToInt(x11hexname.substr(5,2));
83         return c;
84 }
85
86
87 ColorSet::ColorSet()
88 {
89         char const * grey40 = "#666666";
90         char const * grey60 = "#999999";
91         char const * grey80 = "#cccccc";
92         //char const * grey90 = "#e5e5e5";
93         //  ColorCode, gui, latex, x11, lyx
94         static ColorEntry const items[] = {
95         { Color_none, N_("none"), "none", "black", "none" },
96         { Color_black, N_("black"), "black", "black", "black" },
97         { Color_white, N_("white"), "white", "white", "white" },
98         { Color_red, N_("red"), "red", "red", "red" },
99         { Color_green, N_("green"), "green", "green", "green" },
100         { Color_blue, N_("blue"), "blue", "blue", "blue" },
101         { Color_cyan, N_("cyan"), "cyan", "cyan", "cyan" },
102         { Color_magenta, N_("magenta"), "magenta", "magenta", "magenta" },
103         { Color_yellow, N_("yellow"), "yellow", "yellow", "yellow" },
104         { Color_cursor, N_("cursor"), "cursor", "black", "cursor" },
105         { Color_background, N_("background"), "background", "linen", "background" },
106         { Color_foreground, N_("text"), "foreground", "black", "foreground" },
107         { Color_selection, N_("selection"), "selection", "LightBlue", "selection" },
108         { Color_selectiontext, N_("selected text"),
109                 "selectiontext", "black", "selectiontext" },
110         { Color_latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
111         { Color_inlinecompletion, N_("inline completion"),
112                 "inlinecompletion", grey60, "inlinecompletion" },
113         { Color_nonunique_inlinecompletion, N_("non-unique inline completion"),
114                 "nonuniqueinlinecompletion", grey80, "nonuniqueinlinecompletion" },
115         { Color_preview, N_("previewed snippet"), "preview", "black", "preview" },
116         { Color_notelabel, N_("note label"), "note", "yellow", "note" },
117         { Color_notebg, N_("note background"), "notebg", "yellow", "notebg" },
118         { Color_commentlabel, N_("comment label"), "comment", "magenta", "comment" },
119         { Color_commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
120         { Color_greyedoutlabel, N_("greyedout inset label"), "greyedout", "#ff0080", "greyedout" },
121         { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
122         { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
123         { Color_branchlabel, N_("branch label"), "branchlabel", "#c88000", "branchlabel" },
124         { Color_footlabel, N_("footnote label"), "footlabel", "#00aaff", "footlabel" },
125         { Color_indexlabel, N_("index label"), "indexlabel", "green", "indexlabel" },
126         { Color_marginlabel, N_("margin note label"), "marginlabel", "#aa55ff", "marginlabel" },
127         { Color_urllabel, N_("URL label"), "urllabel", "blue", "urllabel" },
128         { Color_urltext, N_("URL text"), "urltext", "blue", "urltext" },
129         { Color_depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
130         { Color_language, N_("language"), "language", "Blue", "language" },
131         { Color_command, N_("command inset"), "command", "black", "command" },
132         { Color_commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
133         { Color_commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
134         { Color_special, N_("special character"), "special", "RoyalBlue", "special" },
135         { Color_math, N_("math"), "math", "DarkBlue", "math" },
136         { Color_mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
137         { Color_graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
138         { Color_mathmacrobg, N_("Math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
139         { Color_mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
140         { Color_mathcorners, N_("math corners"), "mathcorners", "linen", "mathcorners" },
141         { Color_mathline, N_("math line"), "mathline", "Blue", "mathline" },
142         { Color_mathmacrobg, N_("Math macro background"), "mathmacrobg", "#ede2d8", "mathmacrobg" },
143         { Color_mathmacrohoverbg, N_("Math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", "mathmacrohoverbg" },
144         { Color_mathmacrolabel, N_("Math macro label"), "mathmacrolabel", "#a19992", "mathmacrolabel" },
145         { Color_mathmacroframe, N_("Math macro frame"), "mathmacroframe", "#ede2d8", "mathmacroframe" },
146         { Color_mathmacroblend, N_("Math macro blended out"), "mathmacroblend", "black", "mathmacroblend" },
147         { Color_mathmacrooldarg, N_("Math macro old parameter"), "mathmacrooldarg", grey80, "mathmacrooldarg" },
148         { Color_mathmacronewarg, N_("Math macro new parameter"), "mathmacronewarg", "black", "mathmacronewarg" },
149         { Color_captionframe, N_("caption frame"), "captionframe", "DarkRed", "captionframe" },
150         { Color_collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
151         { Color_collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
152         { Color_insetbg, N_("inset background"), "insetbg", grey80, "insetbg" },
153         { Color_insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
154         { Color_error, N_("LaTeX error"), "error", "Red", "error" },
155         { Color_eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
156         { Color_appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
157         { Color_changebar, N_("change bar"), "changebar", "Blue", "changebar" },
158         { Color_deletedtext, N_("Deleted text"), "deletedtext", "#ff0000", "deletedtext" },
159         { Color_addedtext, N_("Added text"), "addedtext", "#0000ff", "addedtext" },
160         { Color_added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
161         { Color_topline, N_("top/bottom line"), "topline", "Brown", "topline" },
162         { Color_tabularline, N_("table line"), "tabularline", "black", "tabularline" },
163         { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline",
164              "LightSteelBlue", "tabularonoffline" },
165         { Color_bottomarea, N_("bottom area"), "bottomarea", grey40, "bottomarea" },
166         { Color_newpage, N_("new page"), "newpage", "Blue", "newpage" },
167         { Color_pagebreak, N_("page break / line break"), "pagebreak", "RoyalBlue", "pagebreak" },
168         { Color_buttonframe, N_("frame of button"), "buttonframe", "#dcd2c8", "buttonframe" },
169         { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
170         { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
171         { Color_inherit, N_("inherit"), "inherit", "black", "inherit" },
172         { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
173         { Color_ignore, 0, 0, 0, 0 }
174         };
175
176         for (int i = 0; items[i].guiname; ++i)
177                 fill(items[i]);
178 }
179
180
181 /// initialise a color entry
182 void ColorSet::fill(ColorEntry const & entry)
183 {
184         Information in;
185         in.lyxname   = entry.lyxname;
186         in.latexname = entry.latexname;
187         in.x11name   = entry.x11name;
188         in.guiname   = entry.guiname;
189         infotab[entry.lcolor] = in;
190         lyxcolors[entry.lyxname] = entry.lcolor;
191         latexcolors[entry.latexname] = entry.lcolor;
192 }
193
194
195 docstring const ColorSet::getGUIName(ColorCode c) const
196 {
197         InfoTab::const_iterator it = infotab.find(c);
198         if (it != infotab.end())
199                 return _(it->second.guiname);
200         return from_ascii("none");
201 }
202
203
204 string const ColorSet::getX11Name(ColorCode c) const
205 {
206         InfoTab::const_iterator it = infotab.find(c);
207         if (it != infotab.end())
208                 return it->second.x11name;
209
210         lyxerr << "LyX internal error: Missing color"
211                   " entry in Color.cpp for " << c << '\n'
212                << "Using black." << endl;
213         return "black";
214 }
215
216
217 string const ColorSet::getLaTeXName(ColorCode c) const
218 {
219         InfoTab::const_iterator it = infotab.find(c);
220         if (it != infotab.end())
221                 return it->second.latexname;
222         return "black";
223 }
224
225
226 string const ColorSet::getLyXName(ColorCode c) const
227 {
228         InfoTab::const_iterator it = infotab.find(c);
229         if (it != infotab.end())
230                 return it->second.lyxname;
231         return "black";
232 }
233
234
235 bool ColorSet::setColor(ColorCode col, string const & x11name)
236 {
237         InfoTab::iterator it = infotab.find(col);
238         if (it == infotab.end()) {
239                 LYXERR0("Color " << col << " not found in database.");
240                 return false;
241         }
242
243         // "inherit" is returned for colors not in the database
244         // (and anyway should not be redefined)
245         if (col == Color_none || col == Color_inherit || col == Color_ignore) {
246                 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
247                 return false;
248         }
249
250         it->second.x11name = x11name;
251         return true;
252 }
253
254
255 bool ColorSet::setColor(string const & lyxname, string const &x11name)
256 {
257         string const lcname = ascii_lowercase(lyxname);
258         if (lyxcolors.find(lcname) == lyxcolors.end()) {
259                 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
260                        << lyxname << '"');
261                 addColor(static_cast<ColorCode>(infotab.size()), lcname);
262         }
263
264         return setColor(lyxcolors[lcname], x11name);
265 }
266
267
268 void ColorSet::addColor(ColorCode c, string const & lyxname)
269 {
270         ColorEntry ce = { c, "", "", "", lyxname.c_str() };
271         fill(ce);
272 }
273
274
275 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
276 {
277         string const lcname = ascii_lowercase(lyxname);
278         Transform::const_iterator it = lyxcolors.find(lcname);
279         if (it == lyxcolors.end()) {
280                 LYXERR0("ColorSet::getFromLyXName: Unknown color \""
281                        << lyxname << '"');
282                 return Color_none;
283         }
284
285         return it->second;
286 }
287
288
289 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
290 {
291         Transform::const_iterator it = latexcolors.find(latexname);
292         if (it == latexcolors.end()) {
293                 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
294                        << latexname << '"' << endl;
295                 return Color_none;
296         }
297
298         return it->second;
299 }
300
301
302 // The evil global Color instance
303 ColorSet lcolor;
304 // An equally evil global system Color instance
305 ColorSet system_lcolor;
306
307
308 } // namespace lyx