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
hexstrToInt(string const & str)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
X11hexname(RGBColor const & col)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
rgbFromHexName(string const & x11hexname)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
outputLaTeXColor(RGBColor const & color)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
RGBColorFromLaTeX(string const & color)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
Color(ColorCode base_color)155 Color::Color(ColorCode base_color) : baseColor(base_color),
156 mergeColor(Color_ignore)
157 {}
158
159
operator ==(Color const & color) const160 bool Color::operator==(Color const & color) const
161 {
162 return baseColor == color.baseColor;
163 }
164
165
operator !=(Color const & color) const166 bool Color::operator!=(Color const & color) const
167 {
168 return baseColor != color.baseColor;
169 }
170
171
operator <(Color const & color) const172 bool Color::operator<(Color const & color) const
173 {
174 return baseColor < color.baseColor;
175 }
176
177
operator <=(Color const & color) const178 bool Color::operator<=(Color const & color) const
179 {
180 return baseColor <= color.baseColor;
181 }
182
183
operator <<(std::ostream & os,Color color)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
ColorSet()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_blue, N_("blue"), "blue", "blue", "blue" },
207 { Color_brown, N_("brown"), "brown", "brown", "brown" },
208 { Color_cyan, N_("cyan"), "cyan", "cyan", "cyan" },
209 { Color_darkgray, N_("darkgray"), "darkgray", "darkgray", "darkgray" },
210 { Color_gray, N_("gray"), "gray", "gray", "gray" },
211 { Color_green, N_("green"), "green", "green", "green" },
212 { Color_lightgray, N_("lightgray"), "lightgray", "lightgray", "lightgray" },
213 { Color_lime, N_("lime"), "lime", "lime", "lime" },
214 { Color_magenta, N_("magenta"), "magenta", "magenta", "magenta" },
215 { Color_olive, N_("olive"), "olive", "olive", "olive" },
216 { Color_orange, N_("orange"), "orange", "orange", "orange" },
217 { Color_pink, N_("pink"), "pink", "pink", "pink" },
218 { Color_purple, N_("purple"), "purple", "purple", "purple" },
219 { Color_red, N_("red"), "red", "red", "red" },
220 { Color_teal, N_("teal"), "teal", "teal", "teal" },
221 { Color_violet, N_("violet"), "violet", "violet", "violet" },
222 { Color_yellow, N_("yellow"), "yellow", "yellow", "yellow" },
223 { Color_cursor, N_("cursor"), "cursor", "black", "cursor" },
224 { Color_background, N_("background"), "background", "linen", "background" },
225 { Color_foreground, N_("text"), "foreground", "black", "foreground" },
226 { Color_selection, N_("selection"), "selection", "LightBlue", "selection" },
227 { Color_selectiontext, N_("selected text"),
228 "selectiontext", "black", "selectiontext" },
229 { Color_latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
230 { Color_inlinecompletion, N_("inline completion"),
231 "inlinecompletion", grey60, "inlinecompletion" },
232 { Color_nonunique_inlinecompletion, N_("non-unique inline completion"),
233 "nonuniqueinlinecompletion", grey80, "nonuniqueinlinecompletion" },
234 { Color_preview, N_("previewed snippet"), "preview", "black", "preview" },
235 { Color_notelabel, N_("note label"), "note", "yellow", "note" },
236 { Color_notebg, N_("note background"), "notebg", "yellow", "notebg" },
237 { Color_commentlabel, N_("comment label"), "comment", "magenta", "comment" },
238 { Color_commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
239 { Color_greyedoutlabel, N_("greyedout inset label"), "greyedout", "#ff0080", "greyedout" },
240 { Color_greyedouttext, N_("greyedout inset text"), "greyedouttext", grey80, "greyedouttext" },
241 { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
242 { Color_phantomtext, N_("phantom inset text"), "phantomtext", "#7f7f7f", "phantomtext" },
243 { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
244 { Color_listingsbg, N_("listings background"), "listingsbg", "white", "listingsbg" },
245 { Color_branchlabel, N_("branch label"), "branchlabel", "#c88000", "branchlabel" },
246 { Color_footlabel, N_("footnote label"), "footlabel", "#00aaff", "footlabel" },
247 { Color_indexlabel, N_("index label"), "indexlabel", "green", "indexlabel" },
248 { Color_marginlabel, N_("margin note label"), "marginlabel", "#aa55ff", "marginlabel" },
249 { Color_urllabel, N_("URL label"), "urllabel", "blue", "urllabel" },
250 { Color_urltext, N_("URL text"), "urltext", "blue", "urltext" },
251 { Color_depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
252 { Color_scroll, N_("scroll indicator"), "scroll", "IndianRed", "scroll" },
253 { Color_language, N_("language"), "language", "Blue", "language" },
254 { Color_command, N_("command inset"), "command", "black", "command" },
255 { Color_commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
256 { Color_commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
257 { Color_special, N_("special character"), "special", "RoyalBlue", "special" },
258 { Color_math, N_("math"), "math", "DarkBlue", "math" },
259 { Color_mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
260 { Color_graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
261 { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
262 { Color_mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
263 { Color_mathcorners, N_("math corners"), "mathcorners", "linen", "mathcorners" },
264 { Color_mathline, N_("math line"), "mathline", "Blue", "mathline" },
265 { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "#ede2d8", "mathmacrobg" },
266 { Color_mathmacrohoverbg, N_("math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", "mathmacrohoverbg" },
267 { Color_mathmacrolabel, N_("math macro label"), "mathmacrolabel", "#a19992", "mathmacrolabel" },
268 { Color_mathmacroframe, N_("math macro frame"), "mathmacroframe", "#ede2d8", "mathmacroframe" },
269 { Color_mathmacroblend, N_("math macro blended out"), "mathmacroblend", "black", "mathmacroblend" },
270 { Color_mathmacrooldarg, N_("math macro old parameter"), "mathmacrooldarg", grey80, "mathmacrooldarg" },
271 { Color_mathmacronewarg, N_("math macro new parameter"), "mathmacronewarg", "black", "mathmacronewarg" },
272 { Color_collapsible, N_("collapsible inset text"), "collapsible", "DarkRed", "collapsible" },
273 { Color_collapsibleframe, N_("collapsible inset frame"), "collapsibleframe", "IndianRed", "collapsibleframe" },
274 { Color_insetbg, N_("inset background"), "insetbg", grey80, "insetbg" },
275 { Color_insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
276 { Color_error, N_("LaTeX error"), "error", "Red", "error" },
277 { Color_eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
278 { Color_appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
279 { Color_changebar, N_("change bar"), "changebar", "Blue", "changebar" },
280 { Color_deletedtext, N_("deleted text"), "deletedtext", "#ff0000", "deletedtext" },
281 { Color_addedtext, N_("added text"), "addedtext", "#0000ff", "addedtext" },
282 { Color_changedtextauthor1, N_("changed text 1st author"), "changedtextauthor1", "#0000ff", "changedtextauthor1" },
283 { Color_changedtextauthor2, N_("changed text 2nd author"), "changedtextauthor2", "#ff00ff", "changedtextauthor2" },
284 { Color_changedtextauthor3, N_("changed text 3rd author"), "changedtextauthor3", "#ff0000", "changedtextauthor3" },
285 { Color_changedtextauthor4, N_("changed text 4th author"), "changedtextauthor4", "#aa00ff", "changedtextauthor4" },
286 { Color_changedtextauthor5, N_("changed text 5th author"), "changedtextauthor5", "#55aa00", "changedtextauthor5" },
287 { Color_deletedtextmodifier, N_("deleted text modifier"), "deletedtextmodifier", "white", "deletedtextmodifier" },
288 { Color_added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
289 { Color_tabularline, N_("table line"), "tabularline", "black", "tabularline" },
290 { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline",
291 "LightSteelBlue", "tabularonoffline" },
292 { Color_bottomarea, N_("bottom area"), "bottomarea", grey40, "bottomarea" },
293 { Color_newpage, N_("new page"), "newpage", "Blue", "newpage" },
294 { Color_pagebreak, N_("page break / line break"), "pagebreak", "RoyalBlue", "pagebreak" },
295 { Color_buttonframe, N_("button frame"), "buttonframe", "#dcd2c8", "buttonframe" },
296 { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
297 { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
298 { Color_paragraphmarker, N_("paragraph marker"), "paragraphmarker", grey80, "paragraphmarker"},
299 { Color_previewframe, N_("preview frame"), "previewframe", "black", "previewframe"},
300 { Color_inherit, N_("inherit"), "inherit", "black", "inherit" },
301 { Color_regexpframe, N_("regexp frame"), "regexpframe", "green", "regexpframe" },
302 { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
303 { Color_ignore, 0, 0, 0, 0 }
304 };
305
306 for (int i = 0; items[i].guiname; ++i)
307 fill(items[i]);
308 }
309
310
311 /// initialise a color entry
fill(ColorEntry const & entry)312 void ColorSet::fill(ColorEntry const & entry)
313 {
314 Information in;
315 in.lyxname = entry.lyxname;
316 in.latexname = entry.latexname;
317 in.x11name = entry.x11name;
318 in.guiname = entry.guiname;
319 infotab[entry.lcolor] = in;
320 lyxcolors[entry.lyxname] = entry.lcolor;
321 latexcolors[entry.latexname] = entry.lcolor;
322 }
323
324
getGUIName(ColorCode c) const325 docstring const ColorSet::getGUIName(ColorCode c) const
326 {
327 InfoTab::const_iterator it = infotab.find(c);
328 if (it != infotab.end())
329 return _(it->second.guiname);
330 return from_ascii("none");
331 }
332
333
getX11Name(ColorCode c) const334 string const ColorSet::getX11Name(ColorCode c) const
335 {
336 InfoTab::const_iterator it = infotab.find(c);
337 if (it != infotab.end())
338 return it->second.x11name;
339
340 lyxerr << "LyX internal error: Missing color"
341 " entry in Color.cpp for " << c << '\n'
342 << "Using black." << endl;
343 return "black";
344 }
345
346
getLaTeXName(ColorCode c) const347 string const ColorSet::getLaTeXName(ColorCode c) const
348 {
349 InfoTab::const_iterator it = infotab.find(c);
350 if (it != infotab.end())
351 return it->second.latexname;
352 return "black";
353 }
354
355
getLyXName(ColorCode c) const356 string const ColorSet::getLyXName(ColorCode c) const
357 {
358 InfoTab::const_iterator it = infotab.find(c);
359 if (it != infotab.end())
360 return it->second.lyxname;
361 return "black";
362 }
363
364
setColor(ColorCode col,string const & x11name)365 bool ColorSet::setColor(ColorCode col, string const & x11name)
366 {
367 InfoTab::iterator it = infotab.find(col);
368 if (it == infotab.end()) {
369 LYXERR0("Color " << col << " not found in database.");
370 return false;
371 }
372
373 // "inherit" is returned for colors not in the database
374 // (and anyway should not be redefined)
375 if (col == Color_none || col == Color_inherit || col == Color_ignore) {
376 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
377 return false;
378 }
379
380 it->second.x11name = x11name;
381 return true;
382 }
383
384
setColor(string const & lyxname,string const & x11name)385 bool ColorSet::setColor(string const & lyxname, string const &x11name)
386 {
387 string const lcname = ascii_lowercase(lyxname);
388 if (lyxcolors.find(lcname) == lyxcolors.end()) {
389 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
390 << lyxname << '"');
391 addColor(static_cast<ColorCode>(infotab.size()), lcname);
392 }
393
394 return setColor(lyxcolors[lcname], x11name);
395 }
396
397
addColor(ColorCode c,string const & lyxname)398 void ColorSet::addColor(ColorCode c, string const & lyxname)
399 {
400 ColorEntry ce = { c, "", "", "", lyxname.c_str() };
401 fill(ce);
402 }
403
404
getFromLyXName(string const & lyxname) const405 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
406 {
407 string const lcname = ascii_lowercase(lyxname);
408 Transform::const_iterator it = lyxcolors.find(lcname);
409 if (it == lyxcolors.end()) {
410 LYXERR0("ColorSet::getFromLyXName: Unknown color \""
411 << lyxname << '"');
412 return Color_none;
413 }
414
415 return it->second;
416 }
417
418
getFromLaTeXName(string const & latexname) const419 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
420 {
421 Transform::const_iterator it = latexcolors.find(latexname);
422 if (it == latexcolors.end()) {
423 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
424 << latexname << '"' << endl;
425 return Color_none;
426 }
427
428 return it->second;
429 }
430
431
432 // The evil global Color instance
433 ColorSet lcolor;
434 // An equally evil global system Color instance
435 ColorSet system_lcolor;
436
437
438 } // namespace lyx
439