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] == '#', /**/);
82 c.r = hexstrToInt(x11hexname.substr(1, 2));
83 c.g = hexstrToInt(x11hexname.substr(3, 2));
84 c.b = hexstrToInt(x11hexname.substr(5, 2));
88 string const outputLaTeXColor(RGBColor const & color)
90 // this routine returns a LaTeX readable color string in the form
91 // "red, green, blue" where the colors are a number in the range 0-1
95 #ifdef USE_CORRECT_RGB_CONVERSION
96 int const scale = 255;
98 // the color values are given in the range of 0-255, so to get
99 // an output of "0.5" for the value 127 we need to do the following
100 // FIXME: This is wrong, since it creates a nonlinear mapping:
101 // There is a gap between 0/256 and 2/256!
102 // 0.5 cannot be represented in 8bit hex RGB, it would be 127.5.
109 int const scale = 256;
112 output = convert<string>(float(red) / scale) + ", "
113 + convert<string>(float(green) / scale) + ", "
114 + convert<string>(float(blue) / scale);
119 RGBColor const RGBColorFromLaTeX(string const & color)
121 vector<string> rgb = getVectorFromString(color);
122 while (rgb.size() < 3)
125 for (int i = 0; i < 3; ++i) {
126 rgb[i] = trim(rgb[i]);
127 if (!isStrDbl(rgb[i]))
130 #ifdef USE_CORRECT_RGB_CONVERSION
131 int const scale = 255;
133 // FIXME: This is wrong, since it creates a nonlinear mapping:
134 // Both 0/256 and 1/256 are mapped to 0!
135 // The wrong code exists only to match outputLaTeXColor().
136 int const scale = 256;
138 c.r = static_cast<unsigned int>(scale * convert<double>(rgb[0]) + 0.5);
139 c.g = static_cast<unsigned int>(scale * convert<double>(rgb[1]) + 0.5);
140 c.b = static_cast<unsigned int>(scale * convert<double>(rgb[2]) + 0.5);
141 #ifndef USE_CORRECT_RGB_CONVERSION
153 Color::Color(ColorCode base_color) : baseColor(base_color),
154 mergeColor(Color_ignore)
158 bool Color::operator==(Color const & color) const
160 return baseColor == color.baseColor;
164 bool Color::operator!=(Color const & color) const
166 return baseColor != color.baseColor;
170 bool Color::operator<(Color const & color) const
172 return baseColor < color.baseColor;
176 bool Color::operator<=(Color const & color) const
178 return baseColor <= color.baseColor;
182 std::ostream & operator<<(std::ostream & os, Color color)
184 os << to_ascii(lcolor.getGUIName(color.baseColor));
185 if (color.mergeColor != Color_ignore)
186 os << "[merged with:"
187 << to_ascii(lcolor.getGUIName(color.mergeColor)) << "]";
194 char const * grey40 = "#666666";
195 char const * grey60 = "#999999";
196 char const * grey80 = "#cccccc";
197 //char const * grey90 = "#e5e5e5";
198 // ColorCode, gui, latex, x11, lyx
199 // Warning: several of these entries are overridden in GuiApplication constructor
200 static ColorEntry const items[] = {
201 { Color_none, N_("none"), "none", "black", "none" },
202 { Color_black, N_("black"), "black", "black", "black" },
203 { Color_white, N_("white"), "white", "white", "white" },
204 { Color_red, N_("red"), "red", "red", "red" },
205 { Color_green, N_("green"), "green", "green", "green" },
206 { Color_blue, N_("blue"), "blue", "blue", "blue" },
207 { Color_cyan, N_("cyan"), "cyan", "cyan", "cyan" },
208 { Color_magenta, N_("magenta"), "magenta", "magenta", "magenta" },
209 { Color_yellow, N_("yellow"), "yellow", "yellow", "yellow" },
210 { Color_cursor, N_("cursor"), "cursor", "black", "cursor" },
211 { Color_background, N_("background"), "background", "linen", "background" },
212 { Color_foreground, N_("text"), "foreground", "black", "foreground" },
213 { Color_selection, N_("selection"), "selection", "LightBlue", "selection" },
214 { Color_selectiontext, N_("selected text"),
215 "selectiontext", "black", "selectiontext" },
216 { Color_latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
217 { Color_inlinecompletion, N_("inline completion"),
218 "inlinecompletion", grey60, "inlinecompletion" },
219 { Color_nonunique_inlinecompletion, N_("non-unique inline completion"),
220 "nonuniqueinlinecompletion", grey80, "nonuniqueinlinecompletion" },
221 { Color_preview, N_("previewed snippet"), "preview", "black", "preview" },
222 { Color_notelabel, N_("note label"), "note", "yellow", "note" },
223 { Color_notebg, N_("note background"), "notebg", "yellow", "notebg" },
224 { Color_commentlabel, N_("comment label"), "comment", "magenta", "comment" },
225 { Color_commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
226 { Color_greyedoutlabel, N_("greyedout inset label"), "greyedout", "#ff0080", "greyedout" },
227 { Color_greyedouttext, N_("greyedout inset text"), "greyedouttext", grey80, "greyedouttext" },
228 { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
229 { Color_phantomtext, N_("phantom inset text"), "phantomtext", "#7f7f7f", "phantomtext" },
230 { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
231 { Color_listingsbg, N_("listings background"), "listingsbg", "white", "listingsbg" },
232 { Color_branchlabel, N_("branch label"), "branchlabel", "#c88000", "branchlabel" },
233 { Color_footlabel, N_("footnote label"), "footlabel", "#00aaff", "footlabel" },
234 { Color_indexlabel, N_("index label"), "indexlabel", "green", "indexlabel" },
235 { Color_marginlabel, N_("margin note label"), "marginlabel", "#aa55ff", "marginlabel" },
236 { Color_urllabel, N_("URL label"), "urllabel", "blue", "urllabel" },
237 { Color_urltext, N_("URL text"), "urltext", "blue", "urltext" },
238 { Color_depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
239 { Color_language, N_("language"), "language", "Blue", "language" },
240 { Color_command, N_("command inset"), "command", "black", "command" },
241 { Color_commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
242 { Color_commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
243 { Color_special, N_("special character"), "special", "RoyalBlue", "special" },
244 { Color_math, N_("math"), "math", "DarkBlue", "math" },
245 { Color_mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
246 { Color_graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
247 { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
248 { Color_mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
249 { Color_mathcorners, N_("math corners"), "mathcorners", "linen", "mathcorners" },
250 { Color_mathline, N_("math line"), "mathline", "Blue", "mathline" },
251 { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "#ede2d8", "mathmacrobg" },
252 { Color_mathmacrohoverbg, N_("math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", "mathmacrohoverbg" },
253 { Color_mathmacrolabel, N_("math macro label"), "mathmacrolabel", "#a19992", "mathmacrolabel" },
254 { Color_mathmacroframe, N_("math macro frame"), "mathmacroframe", "#ede2d8", "mathmacroframe" },
255 { Color_mathmacroblend, N_("math macro blended out"), "mathmacroblend", "black", "mathmacroblend" },
256 { Color_mathmacrooldarg, N_("math macro old parameter"), "mathmacrooldarg", grey80, "mathmacrooldarg" },
257 { Color_mathmacronewarg, N_("math macro new parameter"), "mathmacronewarg", "black", "mathmacronewarg" },
258 { Color_collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
259 { Color_collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
260 { Color_insetbg, N_("inset background"), "insetbg", grey80, "insetbg" },
261 { Color_insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
262 { Color_error, N_("LaTeX error"), "error", "Red", "error" },
263 { Color_eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
264 { Color_appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
265 { Color_changebar, N_("change bar"), "changebar", "Blue", "changebar" },
266 { Color_deletedtext, N_("deleted text"), "deletedtext", "#ff0000", "deletedtext" },
267 { Color_addedtext, N_("added text"), "addedtext", "#0000ff", "addedtext" },
268 { Color_changedtextauthor1, N_("changed text 1st author"), "changedtextauthor1", "#0000ff", "changedtextauthor1" },
269 { Color_changedtextauthor2, N_("changed text 2nd author"), "changedtextauthor2", "#ff00ff", "changedtextauthor2" },
270 { Color_changedtextauthor3, N_("changed text 3rd author"), "changedtextauthor3", "#ff0000", "changedtextauthor3" },
271 { Color_changedtextauthor4, N_("changed text 4th author"), "changedtextauthor4", "#aa00ff", "changedtextauthor4" },
272 { Color_changedtextauthor5, N_("changed text 5th author"), "changedtextauthor5", "#55aa00", "changedtextauthor5" },
273 { Color_deletedtextmodifier, N_("deleted text modifier"), "deletedtextmodifier", "white", "deletedtextmodifier" },
274 { Color_added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
275 { Color_tabularline, N_("table line"), "tabularline", "black", "tabularline" },
276 { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline",
277 "LightSteelBlue", "tabularonoffline" },
278 { Color_bottomarea, N_("bottom area"), "bottomarea", grey40, "bottomarea" },
279 { Color_newpage, N_("new page"), "newpage", "Blue", "newpage" },
280 { Color_pagebreak, N_("page break / line break"), "pagebreak", "RoyalBlue", "pagebreak" },
281 { Color_buttonframe, N_("frame of button"), "buttonframe", "#dcd2c8", "buttonframe" },
282 { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
283 { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
284 { Color_paragraphmarker, N_("paragraph marker"), "paragraphmarker", grey80, "paragraphmarker"},
285 { Color_previewframe, N_("preview frame"), "previewframe", "black", "previewframe"},
286 { Color_inherit, N_("inherit"), "inherit", "black", "inherit" },
287 { Color_regexpframe, N_("regexp frame"), "green", "green", "green" },
288 { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
289 { Color_ignore, 0, 0, 0, 0 }
292 for (int i = 0; items[i].guiname; ++i)
297 /// initialise a color entry
298 void ColorSet::fill(ColorEntry const & entry)
301 in.lyxname = entry.lyxname;
302 in.latexname = entry.latexname;
303 in.x11name = entry.x11name;
304 in.guiname = entry.guiname;
305 infotab[entry.lcolor] = in;
306 lyxcolors[entry.lyxname] = entry.lcolor;
307 latexcolors[entry.latexname] = entry.lcolor;
311 docstring const ColorSet::getGUIName(ColorCode c) const
313 InfoTab::const_iterator it = infotab.find(c);
314 if (it != infotab.end())
315 return _(it->second.guiname);
316 return from_ascii("none");
320 string const ColorSet::getX11Name(ColorCode c) const
322 InfoTab::const_iterator it = infotab.find(c);
323 if (it != infotab.end())
324 return it->second.x11name;
326 lyxerr << "LyX internal error: Missing color"
327 " entry in Color.cpp for " << c << '\n'
328 << "Using black." << endl;
333 string const ColorSet::getLaTeXName(ColorCode c) const
335 InfoTab::const_iterator it = infotab.find(c);
336 if (it != infotab.end())
337 return it->second.latexname;
342 string const ColorSet::getLyXName(ColorCode c) const
344 InfoTab::const_iterator it = infotab.find(c);
345 if (it != infotab.end())
346 return it->second.lyxname;
351 bool ColorSet::setColor(ColorCode col, string const & x11name)
353 InfoTab::iterator it = infotab.find(col);
354 if (it == infotab.end()) {
355 LYXERR0("Color " << col << " not found in database.");
359 // "inherit" is returned for colors not in the database
360 // (and anyway should not be redefined)
361 if (col == Color_none || col == Color_inherit || col == Color_ignore) {
362 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
366 it->second.x11name = x11name;
371 bool ColorSet::setColor(string const & lyxname, string const &x11name)
373 string const lcname = ascii_lowercase(lyxname);
374 if (lyxcolors.find(lcname) == lyxcolors.end()) {
375 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
377 addColor(static_cast<ColorCode>(infotab.size()), lcname);
380 return setColor(lyxcolors[lcname], x11name);
384 void ColorSet::addColor(ColorCode c, string const & lyxname)
386 ColorEntry ce = { c, "", "", "", lyxname.c_str() };
391 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
393 string const lcname = ascii_lowercase(lyxname);
394 Transform::const_iterator it = lyxcolors.find(lcname);
395 if (it == lyxcolors.end()) {
396 LYXERR0("ColorSet::getFromLyXName: Unknown color \""
405 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
407 Transform::const_iterator it = latexcolors.find(latexname);
408 if (it == latexcolors.end()) {
409 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
410 << latexname << '"' << endl;
418 // The evil global Color instance
420 // An equally evil global system Color instance
421 ColorSet system_lcolor;