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