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