3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
11 * \author André Pönitz
12 * \author Martin Vermeer
14 * Full author contact details are available in file CREDITS.
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"
35 using namespace lyx::support;
40 struct ColorSet::ColorEntry {
43 char const * latexname;
44 char const * x11hexname;
45 char const * x11darkhexname;
50 static int hexstrToInt(string const & str)
53 istringstream is(str);
54 is >> setbase(16) >> val;
59 /////////////////////////////////////////////////////////////////////
63 /////////////////////////////////////////////////////////////////////
66 string const X11hexname(RGBColor const & col)
70 ostr << '#' << setbase(16) << setfill('0')
79 RGBColor rgbFromHexName(string const & x11hexname)
82 LASSERT(x11hexname.size() == 7 && x11hexname[0] == '#',
84 c.r = hexstrToInt(x11hexname.substr(1, 2));
85 c.g = hexstrToInt(x11hexname.substr(3, 2));
86 c.b = hexstrToInt(x11hexname.substr(5, 2));
91 string const outputLaTeXColor(RGBColor const & color)
93 // this routine returns a LaTeX readable color string in the form
94 // "red, green, blue" where the colors are a number in the range 0-1
98 #ifdef USE_CORRECT_RGB_CONVERSION
99 int const scale = 255;
101 // the color values are given in the range of 0-255, so to get
102 // an output of "0.5" for the value 127 we need to do the following
103 // FIXME: This is wrong, since it creates a nonlinear mapping:
104 // There is a gap between 0/256 and 2/256!
105 // 0.5 cannot be represented in 8bit hex RGB, it would be 127.5.
112 int const scale = 256;
115 output = convert<string>(float(red) / scale) + ", "
116 + convert<string>(float(green) / scale) + ", "
117 + convert<string>(float(blue) / scale);
122 RGBColor const RGBColorFromLaTeX(string const & color)
124 vector<string> rgb = getVectorFromString(color);
125 while (rgb.size() < 3)
128 for (int i = 0; i < 3; ++i) {
129 rgb[i] = trim(rgb[i]);
130 if (!isStrDbl(rgb[i]))
133 #ifdef USE_CORRECT_RGB_CONVERSION
134 int const scale = 255;
136 // FIXME: This is wrong, since it creates a nonlinear mapping:
137 // Both 0/256 and 1/256 are mapped to 0!
138 // The wrong code exists only to match outputLaTeXColor().
139 int const scale = 256;
141 c.r = static_cast<unsigned int>(scale * convert<double>(rgb[0]) + 0.5);
142 c.g = static_cast<unsigned int>(scale * convert<double>(rgb[1]) + 0.5);
143 c.b = static_cast<unsigned int>(scale * convert<double>(rgb[2]) + 0.5);
144 #ifndef USE_CORRECT_RGB_CONVERSION
156 RGBColor const inverseRGBColor(RGBColor color)
158 color.r = 255 - color.r;
159 color.g = 255 - color.g;
160 color.b = 255 - color.b;
166 Color::Color(ColorCode base_color) : baseColor(base_color),
167 mergeColor(Color_ignore)
171 bool Color::operator==(Color const & color) const
173 return baseColor == color.baseColor;
177 bool Color::operator!=(Color const & color) const
179 return baseColor != color.baseColor;
183 bool Color::operator<(Color const & color) const
185 return baseColor < color.baseColor;
189 bool Color::operator<=(Color const & color) const
191 return baseColor <= color.baseColor;
195 std::ostream & operator<<(std::ostream & os, Color color)
197 os << to_ascii(lcolor.getGUIName(color.baseColor));
198 if (color.mergeColor != Color_ignore)
199 os << "[merged with:"
200 << to_ascii(lcolor.getGUIName(color.mergeColor)) << "]";
207 char const * grey40 = "#666666";
208 char const * grey60 = "#999999";
209 char const * grey80 = "#cccccc";
210 // latex colors (xcolor package)
211 char const * black = "#000000";
212 char const * white = "#ffffff";
213 char const * blue = "#0000ff";
214 char const * brown = "#bf8040";
215 char const * cyan = "#00ffff";
216 char const * darkgray = "#404040";
217 char const * gray = "#808080";
218 char const * green = "#00ff00";
219 char const * lightgray = "#bfbfbf";
220 char const * lime = "#bfff00";
221 char const * magenta = "#ff00ff";
222 char const * olive = "#808000";
223 char const * orange = "#ff8000";
224 char const * pink = "#ffbfbf";
225 char const * purple = "#bf0040";
226 char const * red = "#ff0000";
227 char const * teal = "#008080";
228 char const * violet = "#800080";
229 char const * yellow = "#ffff00";
231 char const * Brown = "#a52a2a";
232 char const * DarkRed = "#8b0000";
233 char const * Green = "#008000";
234 char const * IndianRed = "#cd5c5c";
235 char const * Linen = "#faf0e6";
236 char const * RoyalBlue = "#4169e1";
238 //char const * grey90 = "#e5e5e5";
239 // ColorCode, gui, latex, x11hexname, x11darkhexname, lyx
240 // Warning: several of these entries are overridden in GuiApplication constructor
241 static ColorEntry const items[] = {
242 { Color_none, N_("none"), "none", black, black, "none" },
243 { Color_black, N_("black"), "black", black, black, "black" },
244 { Color_white, N_("white"), "white", white, white, "white" },
245 { Color_blue, N_("blue"), "blue", blue, blue, "blue" },
246 { Color_brown, N_("brown"), "brown", brown, brown, "brown" },
247 { Color_cyan, N_("cyan"), "cyan", cyan, cyan, "cyan" },
248 { Color_darkgray, N_("darkgray"), "darkgray", darkgray, darkgray, "darkgray" },
249 { Color_gray, N_("gray"), "gray", gray, gray, "gray" },
250 { Color_green, N_("green"), "green", green, green, "green" },
251 { Color_lightgray, N_("lightgray"), "lightgray", lightgray, lightgray, "lightgray" },
252 { Color_lime, N_("lime"), "lime", lime, lime, "lime" },
253 { Color_magenta, N_("magenta"), "magenta", magenta, magenta, "magenta" },
254 { Color_olive, N_("olive"), "olive", olive, olive, "olive" },
255 { Color_orange, N_("orange"), "orange", orange, orange, "orange" },
256 { Color_pink, N_("pink"), "pink", pink, pink, "pink" },
257 { Color_purple, N_("purple"), "purple", purple, purple, "purple" },
258 { Color_red, N_("red"), "red", red, red, "red" },
259 { Color_teal, N_("teal"), "teal", teal, teal, "teal" },
260 { Color_violet, N_("violet"), "violet", violet, violet, "violet" },
261 { Color_yellow, N_("yellow"), "yellow", yellow, yellow, "yellow" },
262 { Color_cursor, N_("cursor"), "cursor", black, Linen, "cursor" },
263 { Color_background, N_("background"), "background", Linen, black, "background" },
264 { Color_foreground, N_("text"), "foreground", black, Linen, "foreground" },
265 { Color_selection, N_("selection"), "selection", "#add8e6", "#add8e6", "selection" },
266 { Color_selectiontext, N_("selected text"), "selectiontext", black, black, "selectiontext" },
267 { Color_latex, N_("LaTeX text"), "latex", DarkRed, "#D66613", "latex" },
268 { Color_textlabel1, N_("Text label 1"), "textlabel1", blue, "#86a4ff", "textlabel1" },
269 { Color_textlabel2, N_("Text label 2"), "textlabel2", Green, green, "textlabel2" },
270 { Color_textlabel3, N_("Text label 3"), "textlabel3", magenta, magenta, "textlabel3" },
271 { Color_inlinecompletion, N_("inline completion"),
272 "inlinecompletion", grey60, grey40, "inlinecompletion" },
273 { Color_nonunique_inlinecompletion, N_("non-unique inline completion"),
274 "nonuniqueinlinecompletion", grey80, grey60, "nonuniqueinlinecompletion" },
275 { Color_preview, N_("previewed snippet"), "preview", black, Linen, "preview" },
276 { Color_notelabel, N_("note label"), "note", yellow, "#FF6200", "note" },
277 { Color_notebg, N_("note background"), "notebg", yellow, "#5b5903", "notebg" },
278 { Color_commentlabel, N_("comment label"), "comment", magenta, olive, "comment" },
279 { Color_commentbg, N_("comment background"), "commentbg", Linen, black, "commentbg" },
280 { Color_greyedoutlabel, N_("greyedout inset label"), "greyedout", "#ff0080", "#ff0080", "greyedout" },
281 { Color_greyedouttext, N_("greyedout inset text"), "greyedouttext", grey80, grey40, "greyedouttext" },
282 { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", Linen, black, "greyedoutbg" },
283 { Color_phantomtext, N_("phantom inset text"), "phantomtext", "#7f7f7f", "#7f7f7f", "phantomtext" },
284 { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "#f2af7d", "shaded" },
285 { Color_listingsbg, N_("listings background"), "listingsbg", white, black, "listingsbg" },
286 { Color_branchlabel, N_("branch label"), "branchlabel", "#c88000", "#c88000", "branchlabel" },
287 { Color_footlabel, N_("footnote label"), "footlabel", "#00aaff", blue, "footlabel" },
288 { Color_indexlabel, N_("index label"), "indexlabel", Green, teal, "indexlabel" },
289 { Color_marginlabel, N_("margin note label"), "marginlabel", "#aa55ff", violet, "marginlabel" },
290 { Color_urllabel, N_("URL label"), "urllabel", blue, blue, "urllabel" },
291 { Color_urltext, N_("URL text"), "urltext", blue, "#86a4ff", "urltext" },
292 { Color_depthbar, N_("depth bar"), "depthbar", IndianRed, IndianRed, "depthbar" },
293 { Color_scroll, N_("scroll indicator"), "scroll", IndianRed, IndianRed, "scroll" },
294 { Color_language, N_("language"), "language", blue, "#86a4ff", "language" },
295 { Color_command, N_("command inset"), "command", black, black, "command" },
296 { Color_commandbg, N_("command inset background"), "commandbg", "#f0ffff", "#f0ffff", "commandbg" },
297 { Color_commandframe, N_("command inset frame"), "commandframe", black, Linen, "commandframe" },
298 { Color_command_broken, N_("command inset (broken reference)"), "command", white, white, "command_broken" },
299 { Color_buttonbg_broken, N_("button background (broken reference)"), "commandbg", red, red, "commandbg_broken" },
300 { Color_buttonframe_broken, N_("button frame (broken reference)"), "commandframe", red, red, "commandframe_broken" },
301 { Color_buttonhoverbg_broken, N_("button background (broken reference) under focus"), "buttonhoverbg", "#DB0B0B", "#DB0B0B", "buttonhoverbg_broken" },
302 { Color_special, N_("special character"), "special", RoyalBlue, RoyalBlue, "special" },
303 { Color_math, N_("math"), "math", "#00008B", "#85F0FE", "math" },
304 { Color_mathbg, N_("math background"), "mathbg", Linen, black, "mathbg" },
305 { Color_graphicsbg, N_("graphics background"), "graphicsbg", Linen, black, "graphicsbg" },
306 { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", Linen, black, "mathmacrobg" },
307 { Color_mathframe, N_("math frame"), "mathframe", magenta, magenta, "mathframe" },
308 { Color_mathcorners, N_("math corners"), "mathcorners", Linen, black, "mathcorners" },
309 { Color_mathline, N_("math line"), "mathline", blue, "#86a4ff", "mathline" },
310 { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "#ede2d8", black, "mathmacrobg" },
311 { Color_mathmacrohoverbg, N_("math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", grey80, "mathmacrohoverbg" },
312 { Color_mathmacrolabel, N_("math macro label"), "mathmacrolabel", "#a19992", "#a19992", "mathmacrolabel" },
313 { Color_mathmacroframe, N_("math macro frame"), "mathmacroframe", "#ede2d8", black, "mathmacroframe" },
314 { Color_mathmacroblend, N_("math macro blended out"), "mathmacroblend", black, Linen, "mathmacroblend" },
315 { Color_mathmacrooldarg, N_("math macro old parameter"), "mathmacrooldarg", grey80, grey40, "mathmacrooldarg" },
316 { Color_mathmacronewarg, N_("math macro new parameter"), "mathmacronewarg", black, Linen, "mathmacronewarg" },
317 { Color_collapsible, N_("collapsible inset text"), "collapsible", DarkRed, DarkRed, "collapsible" },
318 { Color_collapsibleframe, N_("collapsible inset frame"), "collapsibleframe", IndianRed, IndianRed, "collapsibleframe" },
319 { Color_insetbg, N_("inset background"), "insetbg", grey80, grey80, "insetbg" },
320 { Color_insetframe, N_("inset frame"), "insetframe", IndianRed, IndianRed, "insetframe" },
321 { Color_error, N_("LaTeX error"), "error", red, DarkRed, "error" },
322 { Color_eolmarker, N_("end-of-line marker"), "eolmarker", Brown, Brown, "eolmarker" },
323 { Color_appendix, N_("appendix marker"), "appendix", Brown, Brown, "appendix" },
324 { Color_changebar, N_("change bar"), "changebar", blue, "#86a4ff", "changebar" },
325 { Color_deletedtext, N_("deleted text (output)"), "deletedtext", "#ff0000", "#ff0000", "deletedtext" },
326 { Color_addedtext, N_("added text (output)"), "addedtext", "#0000ff", "#0000ff", "addedtext" },
327 { Color_addedtextauthor1, N_("added text (workarea, 1st author)"), "changedtextauthor1", "#0000ff", "#86a4ff", "changedtextauthor1" },
328 { Color_addedtextauthor2, N_("added text (workarea, 2nd author)"), "changedtextauthor2", "#ff00ff", "#ee86ee", "changedtextauthor2" },
329 { Color_addedtextauthor3, N_("added text (workarea, 3rd author)"), "changedtextauthor3", "#ff0000", "#ea8989", "changedtextauthor3" },
330 { Color_addedtextauthor4, N_("added text (workarea, 4th author)"), "changedtextauthor4", "#aa00ff", "#c371ec", "changedtextauthor4" },
331 { Color_addedtextauthor5, N_("added text (workarea, 5th author)"), "changedtextauthor5", "#55aa00", "#acd780", "changedtextauthor5" },
332 { Color_deletedtextmodifier, N_("deleted text modifier (workarea)"), "deletedtextmodifier", white, white, "deletedtextmodifier" },
333 { Color_added_space, N_("added space markers"), "added_space", Brown, Brown, "added_space" },
334 { Color_tabularline, N_("table line"), "tabularline", black, Linen, "tabularline" },
335 { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline", "#b0c4de", "#23497b", "tabularonoffline" },
336 { Color_bottomarea, N_("bottom area"), "bottomarea", grey40, grey80, "bottomarea" },
337 { Color_newpage, N_("new page"), "newpage", blue, "#86a4ff", "newpage" },
338 { Color_pagebreak, N_("page break / line break"), "pagebreak", RoyalBlue, RoyalBlue, "pagebreak" },
339 { Color_buttonframe, N_("button frame"), "buttonframe", "#dcd2c8", "#dcd2c8", "buttonframe" },
340 { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "#dcd2c8", "buttonbg" },
341 { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "#C7C7CA", "buttonhoverbg" },
342 { Color_paragraphmarker, N_("paragraph marker"), "paragraphmarker", grey80, grey40, "paragraphmarker"},
343 { Color_previewframe, N_("preview frame"), "previewframe", black, Linen, "previewframe"},
344 { Color_inherit, N_("inherit"), "inherit", black, Linen, "inherit" },
345 { Color_regexpframe, N_("regexp frame"), "regexpframe", Green, green, "regexpframe" },
346 { Color_ignore, N_("ignore"), "ignore", black, Linen, "ignore" },
347 { Color_ignore, nullptr, nullptr, nullptr, nullptr, nullptr }
350 for (int i = 0; items[i].guiname; ++i)
355 /// initialise a color entry
356 void ColorSet::fill(ColorEntry const & entry)
359 in.lyxname = entry.lyxname;
360 in.latexname = entry.latexname;
361 in.x11hexname = entry.x11hexname;
362 in.x11darkhexname = entry.x11darkhexname;
363 in.guiname = entry.guiname;
364 infotab[entry.lcolor] = in;
365 lyxcolors[entry.lyxname] = entry.lcolor;
366 latexcolors[entry.latexname] = entry.lcolor;
370 docstring const ColorSet::getGUIName(ColorCode c) const
372 InfoTab::const_iterator it = infotab.find(c);
373 if (it != infotab.end())
374 return _(it->second.guiname);
375 return from_ascii("none");
379 string const ColorSet::getX11HexName(ColorCode c, bool const darkmode) const
381 InfoTab::const_iterator it = infotab.find(c);
382 if (it != infotab.end())
383 return darkmode ? it->second.x11darkhexname : it->second.x11hexname;
385 lyxerr << "LyX internal error: Missing color"
386 " entry in Color.cpp for " << c << '\n'
387 << "Using black." << endl;
388 return darkmode ? "#faf0e6" : "black";
392 pair<string, string> const ColorSet::getAllX11HexNames(ColorCode c) const
394 InfoTab::const_iterator it = infotab.find(c);
395 if (it != infotab.end())
396 return make_pair(it->second.x11hexname, it->second.x11darkhexname);
398 lyxerr << "LyX internal error: Missing color"
399 " entry in Color.cpp for " << c << '\n'
400 << "Using black." << endl;
401 return make_pair("black", "#faf0e6");
405 string const ColorSet::getLaTeXName(ColorCode c) const
407 InfoTab::const_iterator it = infotab.find(c);
408 if (it != infotab.end())
409 return it->second.latexname;
414 string const ColorSet::getLyXName(ColorCode c) const
416 InfoTab::const_iterator it = infotab.find(c);
417 if (it != infotab.end())
418 return it->second.lyxname;
423 bool ColorSet::setColor(ColorCode col, string const & x11hexname,
424 string const & x11darkhexname)
426 InfoTab::iterator it = infotab.find(col);
427 if (it == infotab.end()) {
428 LYXERR0("Color " << col << " not found in database.");
432 // "inherit" is returned for colors not in the database
433 // (and anyway should not be redefined)
434 if (col == Color_none || col == Color_inherit || col == Color_ignore) {
435 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
439 if (!x11hexname.empty())
440 it->second.x11hexname = x11hexname;
441 it->second.x11darkhexname = (x11darkhexname.empty()) ? x11hexname : x11darkhexname;
446 bool ColorSet::setColor(string const & lyxname, string const & x11hexname,
447 string const & x11darkhexname)
449 string const lcname = ascii_lowercase(lyxname);
450 if (lyxcolors.find(lcname) == lyxcolors.end()) {
451 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
453 addColor(static_cast<ColorCode>(infotab.size()), lcname);
456 return setColor(lyxcolors[lcname], x11hexname, x11darkhexname);
460 bool ColorSet::setLaTeXName(string const & lyxname, string const & latexname)
462 string const lcname = ascii_lowercase(lyxname);
463 if (lyxcolors.find(lcname) == lyxcolors.end()) {
464 LYXERR(Debug::GUI, "ColorSet::setLaTeXName: Unknown color \""
466 addColor(static_cast<ColorCode>(infotab.size()), lcname);
469 ColorCode col = lyxcolors[lcname];
470 InfoTab::iterator it = infotab.find(col);
471 if (it == infotab.end()) {
472 LYXERR0("Color " << col << " not found in database.");
476 // "inherit" is returned for colors not in the database
477 // (and anyway should not be redefined)
478 if (col == Color_none || col == Color_inherit || col == Color_ignore) {
479 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
483 if (!latexname.empty())
484 it->second.latexname = latexname;
489 bool ColorSet::setGUIName(string const & lyxname, string const & guiname)
491 string const lcname = ascii_lowercase(lyxname);
492 if (lyxcolors.find(lcname) == lyxcolors.end()) {
493 LYXERR(Debug::GUI, "ColorSet::setGUIName: Unknown color \""
498 ColorCode col = lyxcolors[lcname];
499 InfoTab::iterator it = infotab.find(col);
500 if (it == infotab.end()) {
501 LYXERR0("Color " << col << " not found in database.");
505 // "inherit" is returned for colors not in the database
506 // (and anyway should not be redefined)
507 if (col == Color_none || col == Color_inherit || col == Color_ignore) {
508 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
512 if (!guiname.empty())
513 it->second.guiname = guiname;
518 void ColorSet::addColor(ColorCode c, string const & lyxname)
520 ColorEntry ce = { c, "", "", "", "", lyxname.c_str() };
525 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
527 string const lcname = ascii_lowercase(lyxname);
528 Transform::const_iterator it = lyxcolors.find(lcname);
529 if (it == lyxcolors.end()) {
530 LYXERR0("ColorSet::getFromLyXName: Unknown color \""
539 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
541 Transform::const_iterator it = latexcolors.find(latexname);
542 if (it == latexcolors.end()) {
543 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
544 << latexname << '"' << endl;
552 // The evil global Color instance
554 // An equally evil global system Color instance
555 ColorSet system_lcolor;