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;
49 static int hexstrToInt(string const & str)
52 istringstream is(str);
53 is >> setbase(16) >> val;
58 /////////////////////////////////////////////////////////////////////
62 /////////////////////////////////////////////////////////////////////
65 string const X11hexname(RGBColor const & col)
69 ostr << '#' << setbase(16) << setfill('0')
78 RGBColor rgbFromHexName(string const & x11hexname)
81 LASSERT(x11hexname.size() == 7 && x11hexname[0] == '#',
83 c.r = hexstrToInt(x11hexname.substr(1, 2));
84 c.g = hexstrToInt(x11hexname.substr(3, 2));
85 c.b = hexstrToInt(x11hexname.substr(5, 2));
90 string const outputLaTeXColor(RGBColor const & color)
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
97 #ifdef USE_CORRECT_RGB_CONVERSION
98 int const scale = 255;
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.
111 int const scale = 256;
114 output = convert<string>(float(red) / scale) + ", "
115 + convert<string>(float(green) / scale) + ", "
116 + convert<string>(float(blue) / scale);
121 RGBColor const RGBColorFromLaTeX(string const & color)
123 vector<string> rgb = getVectorFromString(color);
124 while (rgb.size() < 3)
127 for (int i = 0; i < 3; ++i) {
128 rgb[i] = trim(rgb[i]);
129 if (!isStrDbl(rgb[i]))
132 #ifdef USE_CORRECT_RGB_CONVERSION
133 int const scale = 255;
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;
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
155 Color::Color(ColorCode base_color) : baseColor(base_color),
156 mergeColor(Color_ignore)
160 bool Color::operator==(Color const & color) const
162 return baseColor == color.baseColor;
166 bool Color::operator!=(Color const & color) const
168 return baseColor != color.baseColor;
172 bool Color::operator<(Color const & color) const
174 return baseColor < color.baseColor;
178 bool Color::operator<=(Color const & color) const
180 return baseColor <= color.baseColor;
184 std::ostream & operator<<(std::ostream & os, Color color)
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)) << "]";
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_collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
273 { Color_collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
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"), "green", "green", "green" },
302 { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
303 { Color_ignore, 0, 0, 0, 0 }
306 for (int i = 0; items[i].guiname; ++i)
311 /// initialise a color entry
312 void ColorSet::fill(ColorEntry const & entry)
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;
325 docstring const ColorSet::getGUIName(ColorCode c) const
327 InfoTab::const_iterator it = infotab.find(c);
328 if (it != infotab.end())
329 return _(it->second.guiname);
330 return from_ascii("none");
334 string const ColorSet::getX11Name(ColorCode c) const
336 InfoTab::const_iterator it = infotab.find(c);
337 if (it != infotab.end())
338 return it->second.x11name;
340 lyxerr << "LyX internal error: Missing color"
341 " entry in Color.cpp for " << c << '\n'
342 << "Using black." << endl;
347 string const ColorSet::getLaTeXName(ColorCode c) const
349 InfoTab::const_iterator it = infotab.find(c);
350 if (it != infotab.end())
351 return it->second.latexname;
356 string const ColorSet::getLyXName(ColorCode c) const
358 InfoTab::const_iterator it = infotab.find(c);
359 if (it != infotab.end())
360 return it->second.lyxname;
365 bool ColorSet::setColor(ColorCode col, string const & x11name)
367 InfoTab::iterator it = infotab.find(col);
368 if (it == infotab.end()) {
369 LYXERR0("Color " << col << " not found in database.");
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.");
380 it->second.x11name = x11name;
385 bool ColorSet::setColor(string const & lyxname, string const &x11name)
387 string const lcname = ascii_lowercase(lyxname);
388 if (lyxcolors.find(lcname) == lyxcolors.end()) {
389 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
391 addColor(static_cast<ColorCode>(infotab.size()), lcname);
394 return setColor(lyxcolors[lcname], x11name);
398 void ColorSet::addColor(ColorCode c, string const & lyxname)
400 ColorEntry ce = { c, "", "", "", lyxname.c_str() };
405 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
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 \""
419 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
421 Transform::const_iterator it = latexcolors.find(latexname);
422 if (it == latexcolors.end()) {
423 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
424 << latexname << '"' << endl;
432 // The evil global Color instance
434 // An equally evil global system Color instance
435 ColorSet system_lcolor;